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

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!

Related

How to have geom_point and geom_bar side by side and with facet_wrap

I am trying to combine a scatterplot with a bar plot. The bar plot should be on the right side of the scatterplot of each corresponding group specified in facet wrap. The two plots have the same unit on the y axis but the scaling of the bar might need to be adjusted so the barplot it's not too tall.
Here's an example.
data(mpg)
So far I have tried this, but I would the bar should be outside the scatterplot area. Additionally, in my data the x axis in the barplot is a factor with two levels, so it has a discrete scale and my scatterplot has a continuous x scale.
ggplot()+geom_bar(data=mpg,aes(x=10,y=displ,fill=trans),stat='identity',position='stack')
+geom_point(data=mpg,aes(x=displ,y=displ))+facet_wrap(~cyl)
Can I achieve to plot these two plot types side-by-side or do I have to use something like cowplot? I think that if I have to combine plots outside ggplot I will not be able to use factor_wrap, so I guess I have to do a for loop for each factor level and then combine everything?

How to make a Common legend for multiple plots in R?

How can I make a Common legend for multiple plots? As I have plotted multiple plots but each plot is showing a single legend for itself as if I want to remove it and show a command legend.
And I also want to rename xlim to lon and ylim = lat. How can it be possible in image.plot?
This is my code
set.panel()
par(oma=c( 0,0,0,4)) # margin of 4 spaces width at right hand side
set.panel( 2,2) # 2X2 matrix of plots
# now draw all your plots using usual image command
for ( k in 1:4){
image.plot(lon, lat, pr2)
plot(shape,add=TRUE)
image.plot(lon, lat, pr1)
plot(shape,add=TRUE)
}
par(oma=c( 0,0,0,1))# reset margin to be much smaller.
image.plot( legend.only=TRUE, zlim=c(0,2000),horizontal = TRUE)
image.plot tricked into plotting in margin of old setting
set.panel() #
This is my image showing the plot:

Equalize size of plots placed in juxtaposition by grid.arrange

I gathered three geom_bar() plots in one plot by grid.arrange:
I removed the y axis of the two right plots (Q2,Q3) and kept the y axis of Q1 as a common axis. Subsequently, I change plot.margin a little bit to obtain a continuous x axis. Although this worked fine, it bugs me that the leftmost plot is smaller than the remaining two plots. I highlighted the difference in size by plotting the y axis again. I tried to fix this by changing plot.margin but without success. Is there any way to equalize the size of the three plots?
The package patchwork (https://github.com/thomasp85/patchwork) automatically balances the size of the plots while aggregating the plots

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.

Modify plot sizes in ggplot2

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!

Resources