I am asking this question which sounds exactly like this post because it did not solve the problem. Here is my question. I have 2 plots that I want to put together with their scale perfectly in line. Here is what I got to :
As you can see the x axis is not exactly in line. Here is the code I used
bp_horiz<-ggplot(df,aes(y=Corg_rate))+
geom_boxplot()+
theme_classic()+
labs(y="Corg annual rate")+
coord_flip()
histo<-ggplot(data=df, aes(Corg_rate))+
geom_histogram(fill="grey",col="black")+
labs(x="Corg annual rate",y="Counts")+
theme_classic()+
labs(x=NULL,y="Counts")
plot_grid(histo,boxplot_horiz,ncol=1,align="v",rel_heights=c(4,1),axis = 'lr')
I tried to change the align parameters from v to h to hv without success.
I tried using egg with the same problem on non alignment. I tried using ggMarginal but it does not work when the center plot is a histogram.
How would you solve this issue?
I do't know how to add data but to make things clearer here is the beginning of it:
Corg_rate
1 -0.0147
2 0.0106
3 0.114
4 -0.00230
5 0.0105
6 -0.0574
7 0.0102
8 -0.00472
9 0.0335
10 -0.00803
I finally found a way: writing the scale for each plot. I have therefore added to the code
for the boxplot (before coord_flip)
+scale_y_continuous(limits=c(min(df$Corg_rate),max(df$Corg_rate)))
and for the histogram
+scale_x_continuous(limits=c(min(df$Corg_rate),max(df$Corg_rate)))
The function plot_grid then works just fine and the two plots have their scales aligned
Related
I am wondering if anyone has found a way to display violin plots through ggplot2 with variables of 1 or 2 samples.
example code:
library(ggplot2)
testData <- data.frame(x=c("a","a","a","b","b"), y=c(1,2,2,1,2))
ggplot(data=testData ) + geom_violin(aes(x=x,y=y))
As you can see the violin plot for a has been drawn as it has 3 samples, the one for b no -> only 2 samples.
I saw geom_violin produces error when all values in a series are the same but no answer has been given, and it's been 7
years.
I know it is possible to display a violin plot with the violplot package, but I'd really prefer to keep to the ggplot package if possible.
Thanks,
HY
Thanks to #MarcoSandri and others.
I was on ggplot2 3.3.3, it now works on 3.3.6.
This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Closed 8 months ago.
I am trying to create a barplot using ggplot2, with the y axis starting at a value greater than zero.
Lets say I have the means and standard errors for hypothetical dataset about carrot length at three different farms:
carrots<-NULL
carrots$Mean<-c(270,250,240)
carrots$SE<-c(3,4,5)
carrots$Farm<-c("Plains","Hill","Valley")
carrots<-data.frame(carrots)
I create a basic plot:
p<-ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_bar(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0)
p
This is nice, but as the scale runs from 0 to it is difficult to see the differences in length. Therefore, I would like to rescale the y axis to something like c(200,300). However, when I try to do this with:
p+scale_y_continuous('Length (mm)', limit=c(200,300))
The bars disappear, although the error bars remain.
My question is: is it possible to plot a barplot with this adjusted axis using ggplot2?
Thank you for any help or suggestions you can offer.
Try this
p + coord_cartesian(ylim=c(200,300))
Setting the limits on the coordinate system performs a visual zoom;
the data is unchanged, and we just view a small portion of the original plot.
If someone is trying to accomplish the same zoom effect for a flipped bar chart, the accepted answer won't work (even though the answer is perfect for the example in the question).
The solution for the flipped bar chart is using the argument ylim of the coord_flip function. I decided to post this answer because my bars were also "disappearing" as in the original question while I was trying to re-scale with other methods, but in my case the chart was a flipped one. This may probably help other people with the same issue.
This is the adapted code, based on the example of the question:
ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_col(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0) +
coord_flip(ylim=c(200,300))
Flipped chart example
I made a bar plot with three slope estimates, including their standard errors. However, one of these estimates are from another model than the other two. Therefore I would like a gap or a line or so in between so that it is clear they are from different models. Can anyone help me with that?
My code and my data look like this:
ggplot(data=data, aes(x=Name, y=Slope))+geom_bar(stat="identity")+geom_errorbar(aes(x=data$Name, ymin=(data$Slope-data$SE_Slope), ymax=(data$Slope+data$SE_Slope), width=.25))
Name Slope SE_Slope
BP 1.72 0.43
AP 1.74 0.51
DIF 1.70 0.46
geom_vline() may be of use to you! It might work funny with the x axis with geom_bar, but this may help:
Add a vertical line with ggplot when x-axis is a factor
Additionally, you could use annotate() if you really want to point it out.
http://ggplot2.tidyverse.org/reference/annotate.html
I have a data
equip ballots freq %Undercounts
1 LEVER 427780 17016 3.98
2 OPTICAL 1436159 39090 2.72
3 PAPER 3454 113 3.27
4 PUNCH 823921 38462 4.67
I want to plot equipment on the X-axis. And ballots should be at 100% for each equip. Freq should be the percentage of the bars and be represented as a percentage of ballots.
I have tried barplot and lattice plots. But I'm getting some error which seems like my data is not in form. Can someone please help me in what I might be missing.
Thanks.
It is very simple to accomplish with lattice
barchart(freq/ballots + (ballots-freq)/ballots ~ equip, tab, stack=TRUE)
Base graphics require more code and some obscure parameter tweaking to look good
attach(tab)
barplot(rbind(freq/ballots, 1), width=.8, space=.25)
mtext(equip, side=1, at=1:nrow(tab)-.4)
I've got a matrix that I've plotted using the bar plot function to create a stacked bar plot of changes in percent of something (alphabets) over time. The matrix looks like:
Year V W X Y Z
1 7.5397 20.6349 11.1111 0.3968 60.3175
2 8.3333 21.4286 11.9048 0.3968 57.9365
3 23.8095 9.5238 9.5238 0.3968 56.7460
4 23.4127 10.7143 10.3175 1.1905 54.3651
5 23.4127 11.1111 10.7143 1.9841 52.7778
6 30.0000 2.4000 19.2000 2.4000 46.0000
All these should be the same height, because they add to 100, but in my plot, all the heights are just a bit different at the top. My bar plot code looks like this:
>barplot(t(as.matrix(mydata[,2:6])),names.arg=unique(mydata$Year),axis.lty=1, ylim=c(0,100),
xlab="Year", ylab="Percent of Categories", etc.
I've tried adjusting the ylim, changing xpd to =FALSE, and making some axis argument adjustments, and I'm not sure what else to try before scratching the whole thing and retrying with a different plotting function. Any help is appreciated.