this is my first post here, I'm having issues with getting the labels on the x axis of my second plot to show up when using ggarange, but they show up fine when I run them separately in R. Would anyone be able to give me some advice as to how I can fix this? Thanks in advance.
This is my code:
Plot 1
species_bias<- ggplot(Obs_Bias,aes(x=Species,y=Observations, fill=Species))+
geom_boxplot() +
stat_summary(fun = mean)+
theme_classic()+
ggtitle("Species Bias")+
theme(legend.position = "none")+
ylab("Relative Abundance")
species_bias
Plot 2
Abundace_PLOT<- ggplot(Abundance_Errorbar, aes(x=Species, y=Mean,fill=Method)) +
geom_bar(stat="identity", position=position_dodge()) +
geom_errorbar(aes(ymin=Mean-SE, ymax=Mean+SE),width=.2, position=position_dodge(0.9)) +
labs(x="Species", y="Relative Abundnace") +
theme_classic() +
ggtitle("Relative Abundance in Plastifauna grouped by Survey Method")
Abundace_PLOT
Combined plots on page
Species_Bias_ggarange<-ggarrange(species_bias,Abundace_PLOT + rremove("x.text"), labels = c("A","B"), ncol = 2, nrow = 1)
annotate_figure(Species_Bias_ggarange, top=text_grob("Visualizing Species Bias", face="bold",size=14))
Species_Bias_ggarange
Related
I have made a bar graph and would like it to have a log-scaled y axis. However, when I try to add the code for this, it for some reason inverses most of the bars... Can anyone explain why this may be and how to remedy this?
The code I use is as follows:
Graph1 <- ggplot(Data, aes(x=Temp, y=Mean, fill=Exposure)) +
geom_bar(position=position_dodge(), stat='identity', color="black",) +
xlab("Temperature (°C)") +
ylab("Cd concentration (µg/g)") +
facet_wrap(.~Day, scales="free_y", labeller=labeller(Day=supp.labsDAY), nrow = 1, ncol = 4)+
geom_errorbar( aes(x=Temp, ymin=Mean-SEM, ymax=Mean+SEM), width = 0.2, position=position_dodge(.9))
Graph1+ scale_y_log10()
I have a grid of plots, all with the same y and x-axis scale. The plots represent time in the x-axe and mean values in the y-axe with their standard errors. My problem is that some errorbars are not entirely within the plot margins, and I wonder if there is some way to represent the part of the errorlines that are within the plot margins. Below I give a fake example and code to play with:
df <- data.frame(time=seq(-15,15,1),
mean=c(0.49,0.5,0.53,0.55,0.57,0.59,0.61,0.63,0.65,0.67,0.69,0.71,0.73,0.75,0.77,0.79,0.77,0.75,0.73,0.71,0.69,0.67,0.65,0.63,0.61,0.59,0.57,0.55,0.53,0.51,0.49),
sd=c(0.09,0.087,0.082,0.08,0.023,0.011,0.010,0.009,0.008,0.007,0.006,0.005,0.004,0.003,0.002,0.001,0.002,0.003,0.004,0.005,0.006,0.007,0.008,0.009,0.010,0.011,0.023,0.08,0.084,0.087,0.09))
Plot <- ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(limits = c(0.49, 0.85), breaks = c(0.5, 0.65,0.8))
Plot
You need to set coord_cartesian limits rather than scale_y_continuous limits:
ggplot(df, aes(x=time, y=mean)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.3) +
geom_point(size=1) +
geom_line () +
theme_bw() +
scale_y_continuous(breaks = c(0.5, 0.65,0.8)) +
coord_cartesian(ylim = c(0.49, 0.85))
I am simply trying to show the breaks on the x axis of a plot (5,4,3,2,1,.5) but it will not show the .5. When I tried to code below, it resulted in not showing any x marks whatsoever and I don't know why.
labels <- c(5,4,3,2,1,.5)
ggplot(kobe_vs_kawhi, aes(desc(Time_Left), FG_Percentage, color = Player)) +
geom_point() +
geom_smooth() +
scale_x_continuous(breaks = labels) +
scale_color_manual(values = c("red4", "gold2"))
Difficult to answer without data but if you want all of those x-axis values displayed and in reverse try:
ggplot(kobe_vs_kawhi, aes(Time_Left, FG_Percentage, color = Player)) +
geom_point() +
geom_smooth() +
scale_x_reverse(breaks = labels) +
expand_limits(x = c(0, 5) +
scale_color_manual(values = c("red4", "gold2"))
Note that there is no need to sort Time_Left or for labels to be in reverse order using this approach.
I'm trying to show both the geom_line and the geom_point in the legend, however I can't seem to get the geom_line to appear.
graph <- ggplot(mar, aes(x=long, y=lat)) + xlab("Longitude") + ylab("Latitude") + labs(size = "Distance from predicted \n to known Roman road (m)")
graph + theme_light() + geom_point(aes(size=distance$NEAR_DIST)) + geom_line(color="white", size=0.5)
This generates a graph with geom_point (size of points are based on distance from the line in geom_line and another line), but I can't get the geom_line to appear in the legend. Any ideas on how to do this?
Answered by Craig:
graph <- ggplot(mar, aes(x=long, y=lat)) + xlab("Longitude") + ylab("Latitude") + labs(size = "Distance from predicted \n to known Roman road (m)")
graph + theme_dark() + geom_point(aes(size=distance$NEAR_DIST)) + geom_line(aes(color = "LCP Roman Road"), size = 0.5) + scale_color_manual(name = "tt", values = "white")
assigned color via an aesthetic.
I'm trying to assign a color to a ggplot bar graph based on whether the value is above or below 0.5.
Here is reproducible code below and graph without the color assigned.
dnow <- data.frame(x=rep(c("protected areas","wildnerness areas","private lands","multi-use lands"), each=25), y=runif(100))
ggplot(dnow,aes(x=x, y=y)) + stat_summary(fun.y=mean, geom="bar", position=position_dodge(1)) +
stat_summary(fun.data = mean_se,geom="errorbar", color="grey40",position=position_dodge(1), width=.2) +
geom_hline(yintercept = 0.5) + labs(y="Mean Agreement") + theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.title.y=element_blank()) +
coord_flip()
Based on the following stackoverflow question (Setting a conditional color by stat_summary in ggplot) I tried to manually assign colors based on the threshold value of 0.5 using aes in the stat_summary so that bars with values over 0.5 are green and bars with values under 0.5 are red.
The code and output are below. The graph however does not look correct. It created two bars with a "true" or "false" instead of coloring the single bar based on the threshold value. Not sure how to resolve this.
ggplot(dnow,aes(x=x, y=y)) + stat_summary(fun.y=mean, geom="bar", aes(fill = y > 0.5), position=position_dodge(1)) +
stat_summary(fun.data = mean_se,geom="errorbar", color="grey40",position=position_dodge(1), width=.2) +
geom_hline(yintercept = 0.5) + labs(y="Mean Agreement") + theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.title.y=element_blank()) +
scale_fill_manual(values = c('red', 'green')) + coord_flip()
You could simply change your y to ..y.. in your aes. Although it is probably best to aggregate your data before hand and use geom_bar similar to the post you linked. This should work:
ggplot(dnow,aes(x=x, y=y)) + stat_summary(fun.y=mean, geom="bar", aes(fill = ..y.. > 0.5), position=position_dodge(1)) +
stat_summary(fun.data = mean_se,geom="errorbar", color="grey40",position=position_dodge(1), width=.2) +
geom_hline(yintercept = 0.5) + labs(y="Mean Agreement") + theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.title.y=element_blank()) +
scale_fill_manual(values = c('red', 'green')) + coord_flip()
..y.. refers to the computed mean from fun.y.
The reason is that you use the original values in your data frame for coloring, not the computed summary statistics. By default grouping will happen on every discrete variable: in your case the x and the value > 0.5. So the stat summary function will compute means for 8 groups, and color the bars based on this. I do not know of any way to color directly based on the computed means. However you could precompute the means in each group, and color based on this pre-computed mean: thus you would have one mean for every x.