I'm creating plot with R for a power point. I have to create picture with size
width = 4.24 inch, height = 2.55 inch . So I create my plot:
graf<-ggplot(...)
Then I save It
win.metafile (file, width = 4.24, height = 2.55)
plot(graf)
dev.off()
This is the result
My problem is that the plot is too mutch "in the center" of the shape. I'd like to shift it near the bottom. I'd like to keep the legend more near the bottom and more near the plot. I expect that doing so and keeping the height fixed, the bar plots will be more thin and tall( I have fixed the scale y to be "8"). I'd like also reduce the white border in the left and in the right. Somebody know the correct way to do that?
you can save your ggplots by a function
ggsave(plot,filename,width,height.....)
I think decreasing the width should solve your problem. If it doesn't then you can use the
graf<-ggplot(...)
graf <- graf+theme(legend.position = c(x,y))
arguments to place the legend exactly where do you want it to be.
Related
I am generating a heatmap in R with lots of rows.
TL;DR how do I get the real size of a plot in R?
df=data.frame(one=1:100,two=101:200,three=201:300)
names=1:100
names=paste0("Cell",names)
rownames(df)=(names)
pheatmap(df,scale="row")
default image fits in window, but we can't read row names.
pheatmap(df,scale="row",cellheight = 10)
changing the cell height lets us read row names but now the image doesn't fit in the window!
In this example i am using pheatmap, but also run into this stuff with other plot generating packages.
While I've grown to expect frustrating behavior like this from R and by trial and error could make an appropriately size image for the plot, this seems like something that I should be able to get from the program?
Is there a way to get the dimensions of the plot automatically so that I can create correctly size PDF or PNG for it?
The function pheatmap uses grid graphics to draw its plots, and specifies the size of its elements in "bigpts" where 72 "bigpts" == 1 inch. If you have lots of rows and specify a reasonable row height, this will exceed the plotting window.
Because it is specified as a gtree, we can actually access the height and width of the components and use them to set the dimensions of our png or pdf.
This function will harvest the total height and width in inches of a plot, returning them in a named list:
get_plot_dims <- function(heat_map)
{
plot_height <- sum(sapply(heat_map$gtable$heights, grid::convertHeight, "in"))
plot_width <- sum(sapply(heat_map$gtable$widths, grid::convertWidth, "in"))
return(list(height = plot_height, width = plot_width))
}
We can use this to specify the dimensions of our plotting device:
my_plot <- pheatmap(df,scale="row", cellheight = 10)
plot_dims <- get_plot_dims(my_plot)
png("plot.png", height = plot_dims$height, width = plot_dims$width, units = "in", res = 72)
my_plot
dev.off()
Which gives the desired plot
Note that this is not a general solution for R plots, but specific to pheatmap objects.
I would like to increase the size of my margins without altering the size of my plot region. What I'm doing is:
plotting my data, then querying the values of mai (margin size in inches) and fin (figure region dimension in inches).
adjusting mai by one inch at bottom and left.
adjusting fin by one inch for width and one inch for height.
re-plotting data.
However, I always get the error:
Error in plot.new() : figure region too large
Can anyone help? There's obviously something I don't understand about how mai and fin interact.
Here is an example:
x=c(1,2)
y=c(2,4)
plot(x,y)
init_fin <- par("fin") #gives 7 7
init_mai <- par("mai") #gives 1.02 0.82 0.82 0.42
final_fin <- init_fin + 1 #now 8 8
par(mai=c(2.02,1.82,0.82,0.42), fin=final_fin)
plot(x,y)
ERROR:
Error in plot.new() : figure region too large
this error depends on the device you're using, I see it all the time with Rstudio because the plot panel is quite small on my laptop screen, and R thinks there's not enough space to display the graphic. One option is to open a bigger device window explicitly, e.g.
dev.new(width=10, height=10)
just before your last plot.
Use
par(mai = c(bottom, left, top, right)) before the plot. It will create extra space around the plot area. Change values of bottom, left, top and right according to your taste.
I am producing heatmaps with heatmap.2. I know how to controls many of the parameters but still I have not found a way of making the key of color only wider or putting it as a strip in a side or bottom of the plot.
With keysize it modifies both height and width proportionally.
Also when using ColSideColors I am using legend() to put the color labels, but 'topright' is not at the top-right. I know that this is something about the plot area, margins etc, but I have not found yet a good explanatory text of how heatmap.2 plot is structured and how to positioned things by coordinates and how to deal with oma, mar etc. Depending on the margins, samples, tree depth, etc. the legend could be placed in an open area or overlaps a bit of the heatmap. Any point to good texts for understanding theses issues with R graphics would be truly appreciated.
The coded used is:
df<- data.frame( x1=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x2=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x3=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x4=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x5=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x6=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x7=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,x8=rnorm(120,mean=rep(1:3,each=4),sd=0.2)
,y1=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y2=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y3=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y4=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y5=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y6=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y7=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
,y8=rnorm(120,mean=rep(c(1,2,1),each=4),sd=0.2)
)
dataMatrix <- as.matrix(df)[sample(1:120),]
heatmap.2(dataMatrix
, col=rev(brewer.pal(11,"RdBu"))
, density.info="none"
, key=TRUE
, symkey=FALSE
, trace="none"
, cexRow=1
, scale='row'
, margins =c(10,9)
, ColSideColors=c(rep("red", ncol(df)/2), rep("green", ncol(df)/2))
, main="Log2_intensities median centered"
, keysize=0.9)
legend('topright', c("x", "y"),lty=1, col=c("red", "green"), cex=0.8)
There is the way to organise your plot described here: Moving color key in R heatmap.2 (function of gplots package)
When having a ColSideColors, heatmap.2 will draw the plot as
1 ColSideColors
2 heat map
3 ... so on as in the link above
Never tested with the RowSideColors. In the other words you can organise the plot using lmat, lwid, lhei parameters of heatmap.2. this could give you some space for your legend.
You could try adding the 'inset' parameter and playing with the sizes to move the legend further right (first value) and/or further towards the top (second value) e.g:
legend('topright', inset = c(.02,.02), etc...)
I want to plot a regular bar plot in R, but with just one bar. What I don't like is the fact that the bar width gets to be the width of the whole plot. I want the bar to be "thinner", but I don't know how to do it.
Default command is:
barplot(percentage, col=c("brown4"))
where percentage is a fraction. I tried using xlim parameter, but it gets very messy (bar goes completely to the right or left). For example, I tried:
barplot(percentage, col=c("brown4"), xlim=c(0.5,1))
but this stretches the bar even more. I am an R noob.
You may try to play around with the width of the device you plotting to. E.g.:
# plot to a Windows graphic device
windows(height = 10, width = 4)
barplot(0.5)
# plot to PDF
pdf(height = 10, width = 2)
barplot(0.5)
dev.off()
You may also try width together with xlim
barplot(0.5, width = 0.1, xlim = c(0, 1))
The width argument to barplot says:
width optional vector of bar widths. Re-cycled to length the number of bars drawn. Specifying a single value will have no visible effect unless xlim is specified.
So specify both width and xlim. The interaction of these two is not obvious (to me) so you will probably need to play around with them until they look like you want them to.
percentage <- 0.25
barplot(percentage, width=0.2, xlim=c(0,1.2), col="brown4")
Is it possible to shrink y-axis? I mean instead of the plot being square, I want it to be rectangular, with y axis shrinked.
library(ggplot2)
data = data.frame(rnorm(10000))
colnames(data) = "numOfX"
m <- ggplot(data, aes(x=numOfX))
m + geom_histogram(colour = "blue", fill = "white", binwidth = 0.5)
last_plot() + opts(aspect.ratio=1/10)
You can adjust the margins by adding the following to the last line of your code:
+ opts(plot.margin = unit(c(1,1,10,1), "lines"))
The numbers are the number of lines to add to the margin in the order c(top, right, bottom, left).
Update: The methods that baptiste and I discussed will change the size of the plot itself, but not the size of the plot area. Just for completeness, if you want to change the aspect ratio of the plot but still have it fill the whole plot area, then you need to change the size of the plot area itself.
On the Mac you can do quartz(width=w, height=h), with width and height in inches. This will open a plot window of the specified size. Then run your original code (without margin or aspect ratio changes). This will give you whatever plot size you wish and the plot will fill the plotting area. You can use dev.off() to close the Quartz window when you're done with it.
You can do the same thing in Windows using this Stack Overflow answer.
Finally, if you're using RStudio, you can do Export-->Copy-to-Clipboard and then adjust the aspect ratio manually.
Of course, you can use a combination of my or baptiste's original answers along with the methods above to control both the size of the plot area and the size of the margins at the same time.