Modify plot sizes in ggplot2 - r

I am using multiplot to plot 2 pie charts that share a legend on one page.
plot1<-ggplot(df,aes(x=factor(1),y=values,fill=names))
+geom_bar(stat="identity",width="1")
+coord_polar(theta="y")
+theme(legend.position="none")
plot1<-ggplot(df,aes(x=factor(1),y=values2,fill=names))
+geom_bar(stat="identity",width="1")
+coord_polar(theta="y")
multiplot(plot1,plot2,cols=2)
When I do this, the plot without the legend is much bigger than the plot with a legend.
1) Is there a way to control this directly through multiplot()?
2) How do I control the width and length of my original plots?
Thank you all!

Related

Align two ggplots when one of them has aspect.ratio = 1

I would like to align a volcano plot and a bar plot vertically (same length of y axis but different scales). However I am unable to align them while keeping the aspect ratio of 1 for the volcano plot. I tried the ggpubr, egg, cowplot and gridExtra packages with no success. Am I missing some parameter? I will appreciate your help!

R plot and barplot how to fix ylim not alike?

I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.

Multiple panel plots with pheatmap

I am trying to do multipanel plots one panel being a heatmap using layout to place plots. I've been drawing heatmaps with pheatmap which provides a very convenient color scheme among other things.
The code for pheatmap is available here.
When I try using pheatmap in this way it always plots on a new page. I imagine this is because of its use of the grid package? Is there a way I can do this with pheatmap?
Example code to produce a heatmap next to a barplot but which doesn't since the heatmap gets plotted on a new page below:
xlay=layout( matrix(c(2,2,1),nrow=1) )
layout.show(xlay)
barplot(rnorm(8),horiz=T)
pheatmap(matrix(rnorm(80),nrow=8))
Make your bar plot in ggplot
bar <- ggplot()
Assign both the barplots and heatmap to a variable
heat <- pheatmap(matrix(rnorm(80),nrow=8))
then use gridExtra package to make panel plot the heatmap is saved as an object and you can plot it again by assessing the 4th item in the object
grid.arrange(bar, heat[[4]], nrow = 1)

spplot() - ploting two graphs with the same color scale and correcting the position of tick marks

I'm having a problem with the color bar (or color ramp palette) in spplot (adehabitatHR package). I'm want to plot two graphs in the same window using spplot and I also want that the color bar has the same scale for both graphs (just one bar for both graphs). If that is not possible - or too complicated, since I'm new in R language - I wanna set an equal number of tick marks in the bar for both graphs. I had setted this already, but they are in different positions in the color bar - it would make more sense if i have at least one tick mark at lowest value of the bar, one in the middle, and other in the highest valeu. But when I use the colorkey=list(tick.number=3) argument it doesn't allow me to choose where I want the tick marks. How can I do that?
Here is part of my code.
####loading packages....
library("adehabitatHR")
library(latticeExtra)
###... and data
area.total.mari<- read.table("mari.total.txt")
area.total.mari2<- SpatialPoints(area.total.mari)
area.total.mari.mpc=mcp(area.total.mari2)
dry.mari<- read.table("pontos meses seca.txt")
dry.mari1<- SpatialPoints(dry.mari)
dry.mari1.mpc=mcp(dry.mari1)
###color used in the graphs
mycolor2=gray(16:0/16)
##generating kernels and spplot's
ks<-kernelUD(seca.mari1, grid=300, extent=0.2)
ps<-spplot(ks, col.regions=mycolor2,colorkey=list(tick.number=3),
scales=list(draw=T))+
layer(sp.polygons(area.total.mari.mpc))
kc<-kernelUD(chuva.mari1, grid=300, extent=0.35)
pc<-spplot(kc,col.regions=mycolor2,colorkey=list(tick.number=3),
scales=list(draw=T)))+
layer(sp.polygons(area.total.mari.mpc))
###printing graphs
######## Tick marks are messed up :/
print(ps, position=c(0,0,.5,1),more=T)
print(pc, position=c(0.5,0,1,1))
Here is the graph generated with this code:
https://www.flickr.com/photos/129526227#N02/16316924041/
Note the problem with the tick marks: the lowest value ("0") is ok, but I can't place the another value in the highest!
This is an example with the "meuse" data set:
library(sp)
library(lattice)
library(adehabitatHR)
library(latticeExtra)
data(meuse)
coordenadas<-data.frame(meuse$x,meuse$y)
attach(coordenadas)
coord.sp.pt<- SpatialPoints(coordenadas)
coord.sp.pt.mpc=mcp(coord.sp.pt)
mycolor=gray(0:12/12)
coord.sp.ud<-kernelUD(coord.sp.pt, grid=300, extent=0.1)
coord.printing<-spplot(coord.sp.ud, col.regions=colorRampPalette(c("white","gray","black"))
,colorkey=list(tick.number=2),scales=list(draw=T))+
layer(sp.polygons(coord.sp.pt.mpc))
print(coord.printing)
Thanks in advance folkes

Adding a legend to the outside of a multiple graph plot in R

I am trying to add a legend to the outside of a plot in R.
What I am using is:
png(height=400,width=842,"./rainfall.png")
par(family="serif",mar=c(4,6,4,1),oma=c(1,1,1,6),mfrow=c(1,2))
I create my plot, then:
par(xpd=TRUE)
legend(x="topright",inset=c(-0.2,0),c("4 year moving average","Simple linear trend"),lty=1,col=c("black","red"),cex=1.2)
legend("topleft",c("Annual total"),pch="*",col="blue",cex=1.2)
dev.off()
When i do this though the legend is cut off on the right, as shown in the image below. How can I get the legend to be visible outside the plot?
http://imgur.com/rpgVyrA
Just to let you know, I have been trying the suggestions in this thread, ut they are not working for me: Plot a legend outside of the plotting area in base graphics?
Any help would be appreciated,
Ciara
?par, look for xpd:
A logical value or NA. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region, and if NA, all plotting is clipped to the device region. See also clip.
Use xpd=NA so the legend is not cut off by the plot or figure region.
legend(x="topright",inset=c(-0.2,0),c("4 year moving average",
"Simple linear trend"),lty=1,col=c("black","red"),cex=1.2, xpd=NA)
Results:

Resources