plot two graphs with different y-axis in ggplot - r

I'm trying to plot a scatterplot with errorbars together with a barplot. Both variables have a very different range (scatterplot 0 - 20000+; bar chart 0-1). So I want to create a plot with both graphs in it, with two different y-axes. This is what I've got so far:
ggplot(data, aes(x=seaname, y=chao2)) + geom_bar(stat="identity",
aes(x=seaname, y=compl, colour=major_sea)) + geom_point(aes(colour=major_sea))
+ geom_errorbar(aes(ymin=chao2_lower, ymax=chao2_upper,
colour=major_sea)) + theme(axis.text.x=element_text(angle=90,
vjust=0.5, size=9))
Just to clarify: I don't want two graphs represented below each other. I would like them in the same, single graph.
Anyone who has experience with this kind of problem?
Cheers!!

Related

GGPlot annotation gets pushed off page scale when combining multiple plots within grid.draw

I have 5 plots for 5 different groups. I want to indicate a statistically significant difference a specific time points. I used annotate() to place asterisks in individual plots above the time points. However, when I combine all the plots together to make one figure, the asterisks get pushed off the plots. It looks like it is a problem with the y scales not being fixed. I'm providing as much data as I feel comfortable with. The first bit of code is for one of the groups. The plots all look relatively similar for the 5 groups. The second bit is the data frame I am using to combine the plots. Pictures attached of one plot by itself, then all plots combined. There should be multiple asterisks on multiple plots
ggplot(data,aes(X,Y,group=Group,color=Group))+
theme_bw()+
theme(panel.grid.major=element_line(color="white",size=.1))+
theme(panel.grid.minor=element_line(color="white",size=.1))+
geom_point(stat="summary")+
geom_errorbar(stat="summary",fun.data=mean_se,width=0.25)+
geom_line(stat="summary")+
scale_color_manual(labels = c("C", "T"),values=c("black", "red"))+
theme(axis.title.y = element_text(vjust=2.5))+
annotate("text", x=5, y=3, label= "*",size=10)
grid.newpage()
grid.draw(rbind(ggplotGrob(plotanimal1),
ggplotGrob(plotanimal2),
ggplotGrob(plotanimal3),
ggplotGrob(plotanimal4),
ggplotGrob(plotanimal5)))
You can make the asterisks by using geom_point with shape = 42. That way, ggplot will automatically fix the y axis values itself. You need to set the aesthetics at the same values you would have with annotate. So instead of
annotate("text", x=5, y=3, label= "*",size=10)
You can do
geom_point(aes(x=5, y=3), shape = 42, size = 2)
Have you tried using the package patchwork to organize the plots? It typically works better than grid.draw

Making line plot with discrete x-axis in ggplot2

I am building a ggplot2 figure with a facet grid. On my Y-axis are percentages, and my X-axis is the concentration (in numbers). Each facet has 3 groups (0, 24 and 48 hours)
ggplot(data=MasterTable, aes(x=Concentration, y=Percentage, group=Time)) +
geom_point() +
geom_line() +
facet_grid(Chemicals ~ Treatments)
This generates a continuous x-axis. Since the values are not evenly spread out, I would prefer a discrete axis to better visualize my data. I followed the following tutorial with no luck. The first figure is exactly what I am trying to do.
I also tried formatting the axis:
scale_x_discrete(labels("0", "0.1", "2", "50"))
and formatting the line:
geom_line(aes(Time))
and following this tutorial.
I think this problem is that the values on the x-axis are integers rather than strings. This makes the default axis continuous. How can I change this?? I am sure the solution is simple, I just can't figure it out.
Thanks in advance!
On this page they make the following modification df2$dose<-as.factor(df2$dose). You can try to modify your x-axis as df2$Concentration<-as.factor(df2$Concentration)
or like this:
ggplot(data=MasterTable, aes(x=factor(Concentration), y=Percentage, group=Time)) +
geom_point() +
geom_line() +
facet_grid(Chemicals ~ Treatments)

Readjusting one plot from a two-plots graph

I have two plots in the same graph. I need to adjust the first plot so that the y axis is in function of the number of cases (which is lower than for the second plot). As for now the first plot is difficult to read because it is much smaller than the second one. I would want it to be proportional.
all.scores = rbind(UK_Together.scores, YesScotland.scores)
ggplot(data=all.scores) + # ggplot works on data.frames, always
geom_bar(mapping=aes(x=score, fill=Parti), binwidth=1) +
facet_grid(Parti~.) + # make a separate plot for each hashtag
theme_bw() + scale_fill_brewer() # plain display, nicer colors
Anyone can help?

Plotting histogram using ggplot2 for 2 numbers

I would like to create a plot from two values:
results<-data.frame(name=c("A","B"), values=c("0.8639503","0.7870299"))
qplot(name, data=results, geom="bar")
This gives me a plot, where the difference between the two bars is invisible. Can someone help how to create this plot so that the differences are visible?
Using:
ggplot(results, aes(x=name, y=values)) +
geom_bar(stat="identity")
will give you the desired result

Trying to keep filled bars in a faceted plot

Not sure what I'm doing wrong here. I have this plot:
ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge")
which produces:
Then I want to facet by a factor, creating two stacked plots w/ dodged, colored bars
ggplot(data.PE5, aes(ybands,fill=factor(decide))) + geom_bar(position="dodge") +
facet_grid(~group_label)
However, I lose the factor-based coloring, which I want to keep:
If you move the fill into the geom_bar it should work. As:
ggplot(data.PE5, aes(ybands)) + geom_bar(aes(fill=factor(decide)),position="dodge") + facet_grid(~group_label)
The reason is the way ggplot2 builds plots as a grammar (I think).

Resources