Making responsive Bokeh plots with gridplot in Django - bokeh

I am trying to embed 6 scatter plots using Bokeh (0.12.6) and Django using gridplot, to show 2 rows and 3 columns. They are 6 identical plots, made with very simple code like this (variable names are different):
plot1 = figure(
x_axis_label="Voltage",
y_axis_label="Time",
responsive= True
)
plot1.line(json["voltage"],
json["time"],
color="red",
legend="One"
)
...
grid = gridplot([[plot1, plot2, plot3],
[plot4, plot5, plot6]])
script, div = components(grid)
However, the resulting plots are not responsive. They extend horizontally beyond the page limit and do not resize with the browser window. I tried using sizing_mode but it didn't have any effect.
Is there a way to force them to fit into the page/container (template uses Bootstrap)?

Related

Subplot does not scale graphs to fit width of the page in HTML markdown R

When using subplot() in R to put two ggplotly graphs side by side on an html markdown document I cant get the two graphs to fill the entire width of the document (the same width as the text).
Image of issue
p1 <- ggplotly(MyggplotGraph)
p2 <- p1
subplot(p1, p2)
I tried messing with the different parameters of the subplot() function and it would not make the subplot wider. There doesn't seem to be anyone online who wanted the width to fit the page before.

Why does Jupyter sometimes put plots in scrollable frames?

Below is a screenshot, in which I create two figures containing 4 plots, with near-identical code. Notably, plots both are the same size, and they are generated by the same line of code:
fig, axes = plt.subplots(2, 2,figsize=(12, 10))
The first is displayed with no sliders, the second is displayed in a frame with sliders.
Why is that?
But i am creating two figures the same size, why is it doing it for
one and not the other?
It must be because you have clicked the rectangle box on the left of the output

R/ggiraph: Plot truncated

Trying to learn ggiraph, experimenting with basic examples.
I'm using R 4.0.2 and RStudio 1.3.1073, both freshly downloaded & updated on my Win10 machine.
library( tidyverse )
library( ggiraph )
somePlot <-
mtcars %>%
mutate( carname = rownames( . ) ) %>%
ggplot( aes( x = wt, y = qsec, color=disp,
tooltip = cyl, data_id=carname ) ) +
geom_point_interactive( ) +
theme_bw()
somePlot # Shows ggplot object as expected, no interactivity
girafe( ggobj = somePlot ) # Interactivity, but viewing window shows only about top 2/3 of plot
Links to images because I don't yet have enough reputation on StackOverflow to qualify for posting images:
ggplot object in zoomed viewing window
girafe object in zoomed viewing window
For each image, I clicked "Zoom" in the RStudio plot-viewing window, then captured the image to a PNG file. Expanding the zoom window has no effect-- the ggiraph plot is still truncated at the same place.
If images don't appear: the somePlot call produces a graph as I expect... I can see the entire y axis as well as the x-axis and its labels.
The girafe... call produces the upper 2/3 or so of the graph... I cannot see the x-axis at all, and I cannot see the full extent of the y-axis. The plotted points have the interactivity I expect when I hover over them.
Any ideas about how I might fix this?
Simple fix as suggested by David Gohel: Resize the viewing window -- either the Viewer panel inside RStudio or the window that's opened with the Zoom button. In particular, change the aspect ratio of the viewing window to reveal more (or less) of the figure.
I guess I was expecting the viewing window to behave similarly to base graphics or ggplot, where the displayed image is sized to fit the viewing window, and resizes when the window is resized.
Instead, ggiraph appears to create the image at the size it likes, and if your viewing window doesn't match the aspect ratio of the figure, then some of the figure will spill out of the viewing window.
Thanks.

How to resize pdf graphics produced in R using Illustrator

I am making plots in R using the pdf() command. Graphs look perfect and resize nicely in Acrobat Reader. My usual workflow includes manipulating labels etc. in Illustrator, saving as .eps for submission to publishers or inserting in Word. All works fine for single graphs.
Now I am trying to combine 4 graphs into one by manually putting them together in an A4 Illustrator document. However, when I resize the standard 7x7 inches pdf graph in Illustrator to fit in one column of an A4 page (ca 3.4 inches wide), all proportions get screwed up, e.g. lines and symbols outlines become way too thick. Using pdf(..., width=3.4, height=3.4) in R messes up all the symbol and font sizes so carefully chosen to produce the original graph. Why can't I resize the graph within Illustrator the same way I can resize the pdf e.g. in Acrobat Reader?
Illustrator is scaling everything including the stroke thickness, used to draw lines and symbols that have been converted to paths, and the font size for any text not converted to a path. (As I don't have Illustrator I can't say whether Illustrator treats "text" as text or as paths when opening the pdf.)
When Adobe Acrobat Reader displays the pdf it is just showing a rasterised view of the current file so just scales everything nicely as you wish.
I see two options; Either create the 2x2 plot directly in R and export that to PDF with the correct dimensions, or reduce the margins and font size used in each plot and export then at the desired width/height using the command you showed.
The first options can be achieved via:
pdf("attempt1.pdf", ....)
layout(matrix(1:4, ncol = 2, byrow = FALSE)) ## byrow = TRUE for fill-by-row
## all 4 plot calls go in here
layout(1)
dev.off()
You may need to tweak the point size used in the pdf() device and somewhat adjust the cex.??? settings for some bits of the plot to tailor this exactly how you want it.
Alternatively, you need to reduce the pointsize and margins and draw each plot on the 3.4 by 3.4 inch device. Something like this will get you started:
pdf("attempt2.pdf", height = 3.4, width = 3.4, pointsize = 10)
op <- par(mar = c(4,3,3,1) + 0.1) ## one line less per marging
## your single plotting call here
par(op)
dev.off()
See ?par for the list of ways to control the plot margins & other parameters you might wish to set to control the quality of the final plot. You may wish to look into the cex.foo parameters to control the relative sizing of the text on the plots, but this is all relative to the base pointsize you set when you create the pdf() device.

layout inside layout in R

I'm using R to create a heatmap from a matrix using heatmap.2 - and i want to group these images into one big image - What i usually use to achieve this is layout() - but this doesn't work, as heatmap.2 uses layout, and apparently layout does not work recursively.
Does anyone have any suggestions on how to group together 2 images without layout, or how to make layout support recursive calls?
mat = matrix(nrow=3,nrow=3,1:9)
layout(matrix(nrow=2,ncol=1))
heatmap.2(mat) ## overrides the layout and produces only one plot that takes whole screen
heatmap.2(mat) ## still only one image
thanks.
What follows is a hack that is almost certainly not a perfect solution, but it may get you started.
Create your own version of the heatmap.2 function called hm3. In the code for hm3, comment out all the lines between:
if (missing(lhei) || is.null(lhei))
and the layout call:
layout(lmat, widths = lwid, heights = lhei, respect = FALSE)
it's a big chunk of code, maybe 30 lines. Now the following code produces two heat maps with dendrograms and keys side by side:
x <- as.matrix(mtcars)
lhei <- c(1.5, 4,1.5,4)
lwid <- c(1.5, 4,1.5,4)
layout(rbind(c(4,3,8,7),c(2,1,6,5)),
widths = lwid, heights = lhei, respect = FALSE)
hm3(x)
hm3(x)
Clearly, this will require considerable tweaks to make it look nice (and a larger plotting area; I've squished everything to be a reasonable size to post here).
This is entirely untested. It is likely that using any of the options in the hm3 function that control the appearance of the plot will cause things to go slightly haywire. But this may be a good starting point for your own experimentation to address those issues.
What are you planning on doing with the results?
If you just want to compare 2 heatmaps side by side on the screen then rather than combine them into one single plot you can open 2 plotting devices and arrange them side by side to compare (much simpler than creating a single graph):
heatmap.2(mat1)
dev.new()
heatmap.2(mat2)
Now drag one to the side of the other using your mouse.
If you want to include the combined graphic in a publication then it may be easiest to create the 2 plots and just set them side by side in whatever program you are using to create the article. If you need them in one file you can still save the 2 heatmaps (or other plots) as 2 files then use tools such as imagemagick, gimp, or inkscape to combine the 2 files into 1 with the graphs side by side.

Resources