Resizing plots in the Julia kernel for Jupyter notebooks - julia

I want to know if there is any way that I can specify the size of the inline-plots in Jupyter. At the moment, I use Vega library and width and height don't work. There is any workaround.

I don't know about Vega, though I think there is an environmental variable for ijulia. Using Plots, you can just default(size = (1000, 300)).

To change the plot size using Vega.jl within Jupyter Notebook, you modify the width and height properties:
using Vega
#defaults are width = 450 and height = 450
b = barplot(x = [1,2,3], y = [1,2,3])
#change to whatever values you want
b.width = 200
b.height = 50
#re-render
b

This should work with Plots.jl:
using Plots
plot(1:5, size=(1000,1000))

Related

How to resize an R plot in the report generated by compile report (Not in R Markdown)?

In R Studio, there is an option to compile a report without writing codes in the R Markdown. The keyboard combination on Mac is Shift + Command + K. When I use it on the following code:
par(mfrow = c(2,1))
plot(pressure$pressure~pressure$temperature)
curve((0.168 + 0.007*x)^(20/3), from = 0, to = 400, add = TRUE)
mtext("Temperature vs. Pressure (Unadjusted)", side = 3, cex = 1.2)
plot(pressure$pressure^(3/20)~pressure$temperature)
abline(0.168,0.007)
mtext("Temperature vs. Pressure (Adjusted)", side = 3, cex = 1.2)
it gives a report like this:
Is there a way to resize the plot in the report? For example, how to set it to width 800, and height 1000 as in the following image?
You can set the figure width and height using special comments (similar to chunk options) e.g.
#+ fig.height = 10.4, fig.width = 8.3
above where you create the plot. Note that here the figure sizes are always given in inches (not pixels as in your example, so you would need to convert from pixels to inches).

How do I save SVG plots from Databricks in R?

I have the following R code which I can run in R Studio to generate a CDH dendogram plot and save it locally as a SVG.
c1 <- hclust(as.dist(subs_matrix), method = "ward.D2") #ward.D2
#scale the branches of the tree, only changes the aesthetics the dendrogram for interpretation
min_height<-min(c1$height) - 0.01 #Ensure tree has some height at its lowest point
max_height<-max(c1$height)
c1$height <- (c1$height-min_height)/(max_height-min_height)
#plot the tree, cex is the label font size and hang = -1 roots the branches
plot(c1,cex = 0.6, hang = -1, width=20, height = 8)
#Outputs the tree image to a file
dev.print(svg, file = "C:/Users/Users/c3523186/Downloads/plot.svg", width = 20, height = 8)
dev.off()
When running in Databricks however, I don't know if/how I'm able to save the svg, either locally or in the Filestore. The plot produced by Databricks is shown as an image, which is too low quality / not scalable.
Can anyone help? Some research suggests it might have something to do with display_HTML(), but I have no idea how.

Copy R plot to clipboard with custom size

Is there a way to get R / RStudio to copy a plot to the clipboard with a custom size?
RStudio has this function, but you have to define the size everytime and there is some extra clicking which I am sure is avoidable.
I tried my best with saving as jpeg or else with file="clipboard" and then - after plotting - dev.off(). No error messages, but also nothing in the clipboard.
Here is an example:
data(mtcars)
jpeg(file = "clipboard",width = 800, height = 600, units = "px", pointsize = 12,
quality = 100,
bg = "white", res = NA, family = "", restoreConsole = T)
hist(mtcars$mpg)
dev.off()
Any ideas on how this can be achieved?
The best way would be to be able to control the size in Rstudio, but as you have found out yourself from the Rstudio-website, Rstudio doesn't support that. The following code saves your plot to wmf. There is also a workaround to a save to bitmap, which involves some clicking, but at least you don't have to specify the size any more:
data(mtcars)
windows(800, 600, pointsize = 12) #opens a separate window with the size you want
hist(mtcars$mpg) #draw to this (active) window
savePlot("clipboard", type="wmf") #saves plot to WMF
Unfortunately, it seems to be impossible to save to jpg format to the clipboard. You can copy it to a bitmap by going to this window, click CTRL-C and the graph is on the clipboard as bitmap with 800:600.
EDIT:
The windows command only works on Windows.
For Mac, it should be replaced by: quartz(width=8,height=6,pointsize=12,dpi=100) (width/height in inches!)
For linux try x11(width=8,height=6,pointsize=12,dpi=100) (untested).
I know this is an old post, but recently looking to do the same I came across this Gist which worked well:
https://gist.github.com/dpashouwer/4223903d3ed5783158b7e63992155649
library(tidyverse)
gg_to_clipboard <- function(plot = last_plot(), width = 1000, height = 600, pointsize = 40){
win.graph(width = width, height = height, pointsize = pointsize)
plot %>% print()
savePlot("clipboard", type = "wmf")
dev.off()
}
ggplot(data = mtcars, aes(x = mpg)) + geom_histogram()
gg_to_clipboard()
Making the function makes it very simple.
With Windows and RStudio, you click Export, click Copy Plot to Clipboard, and Copy Plot.
Then, paste into Word or PowerPoint or whatever.
No need to change sizes unless you want to.
This is not command line, but hardly seems onerous.

Plot Size - Using ggplot2 in IPython Notebook (via rmagic)

I have started integrating R usage into Notebook to get, from my perspective, the best of both worlds (data management in python while exploiting the comparative analytical/graphical advantages of R). Unfortunately I am hung up on a seemingly easy element, adjusting plot size for ggplot2 graphics. Adjusting plot sizes is pretty straightforward with pandas, and in a purely R environment (like RStudio), I can adjust plots with dev.new() or PNG(), etc. However, attempting to do this is Notebook makes my computer go nuts (I am running Ubuntu 13.04 on an ASUS U46E). Furthermore, this is crucial, I want to keep the graphics inline so that I can pass the script in its entirety to my colleagues.
When trying dev.new(), my computer locked up and I had to switch to a different virtual terminal to reboot. I tried to adjust x11() options, my browser became temporarily unresponsive while the graphics went a bit haywire. Ultimately, I was granted control again, but I have no idea why this happened.
Does anyone know why this may have occurred? Additionally, does anyone know how to adjust the plot size of ggplot2 objects rendered from within IPython Notebook? I am afraid I can't share the data, but I can tell you that I was attempting to plot three numeric variables faceted by fund center (a categorical variable). These plots did execute as written ... until I tried to adjust the size. Here is my example code:
%%R
#x11(width=500,height=300) << didn't work
#dev.new() << tried before setting size parameters, and it locked up my laptop
#Plot total expenses by unit
print('*****Expenses by Unit*****')
print(smu)
print(ggplot(smu,aes(x=fy,y=as.numeric(as.character(totexp)),group=fund,colour=fund))+geom_line(size=2)+
ggtitle('Total Expenses'))
#Plot expense components
print(ggplot(smu,aes(fy))+
geom_line(aes(y=as.numeric(as.character(fixed)),colour='fixed',group=fund,colour=fund))+
geom_line(aes(y=as.numeric(as.character(var)),colour='variable',group=fund,colour=fund))+
geom_bar(aes(y=as.numeric(as.character(incadj)),group=1),stat='identity')+
facet_grid(.~fund)+
ggtitle('Components of Expenditure'))
The rmagic command has optional arguments to specify the size of the plot. The default is a width and height of 480 pixels. Thus, the code below replicates the default settings:
%%R -w 480 -h 480 -u px
library(ggplot2)
dat <- data.frame(x = rnorm(10), y = rnorm(10),
lab = sample(c('A', 'B'), 10, replace = TRUE))
x <- ggplot(dat, aes(x = x, y = y, color = lab)) + geom_point()
print(x)
And this code below creates a plot with a width of 50 cm and a height of 25 cm:
%%R -w 50 -h 25 -u cm
library(ggplot2)
dat <- data.frame(x = rnorm(10), y = rnorm(10),
lab = sample(c('A', 'B'), 10, replace = TRUE))
x <- ggplot(dat, aes(x = x, y = y, color = lab)) + geom_point()
print(x)
You can also specify the size in inches or millimeters.

heatmap in R how to resize columns labels?

I have a data.matrix that is approximately 4000 rows and 100 columns. I am doing a heatmap of the data like:
data<-heatmap(data_matrix,Rowv=NA,Colv=NA,col=cm.colors(256),scale="column",margins=c(5,10))
But the problem that I got is that the labels that appear in the column are too grouped, so it is impossible to visualize them correctly. How I can resize the heatmap so I can see the values of the labels of the column? I tried to print it in pdf, but it only appears a black stripe.
Thanks
I am including a figure of the heatmap, the portion that I want to see are the labels that are in the right part, but they are too close together.
First of all it's better to put your output directly to a PDF file - you may use other image formats but PDF is the best because it is a vector output and you can zoom as much as you want:
pdf("Your-file.pdf", paper="a4", width=8, height=8)
Then it's better to use pheatmap( = pretty heatmap) package. It makes really better heatmaps with a color key besides your heatmap. Finally although the pheatmap() function tries to reduce the label size while you have many rows, but it fails for really large number of rows. So I use the code below for really high - but not too high - number of rows:
library(pheatmap)
library(gplots)
if (nrow(table) > 100) stop("Too many rows for heatmap, who can read?!")
fontsize_row = 10 - nrow(table) / 15
pheatmap(table, col=greenred(256), main="My Heatmap", cluster_cols=F,
fontsize_row=fontsize_row, border_color=NA)
You may change fontsize_col for the column labels. You have many interesting options like display_numbers to have the values inside the cells of your heatmap. Just read ?pheatmap.
This is an example generated by the default parameters of pheatmap() command:
Finally note that too many rows are easy to read on a display, but useless for print.
In Rstudio you can easily resize the graphic window, same holds for Rgui. Alternatively, if you save the plot to file you can use a bigger size for your graphics, e.g. bigger width and height when calling pdf or png.
You can use cexRow = and cexCol =.
You can get more information into ??heatmap.2
# Row/Column Labeling
margins = c(5, 5),
ColSideColors,
RowSideColors,
cexRow = 0.2 + 1/log10(nr),
cexCol = 0.2 + 1/log10(nc),
labRow = NULL,
labCol = NULL,
srtRow = NULL,
srtCol = NULL,
adjRow = c(0,NA),
adjCol = c(NA,0),
offsetRow = 0.5,
offsetCol = 0.5,
colRow = NULL,
colCol = NULL
If you use pheatmap (https://www.rdocumentation.org/packages/COMPASS/versions/1.10.2/topics/pheatmap) you can spread out those labels by adjusting the cellheight parameter.
If you are doing this in R notebook, even though the entire heat map will not display in your output window when you run the code, when you save the heat map to your computer using the filename parameter, pheatmap will automatically calculate the optimal size for the output file so that your entire heatmap will be displayed in your output file. If this size is not to your liking you can adjust using width and height parameters, but it is unlikely you will want to do this.

Resources