Horizontal lines in errorbars disappearing - r

I'm having some troubles with errorbars in ggplot (R) similar to this problem in Python.
My horizontal errobars are disappearing when using the scale_y function. Can you help me find a solution? The data is here.
My code is:
data_sac_ggplot <- ggplot(yeast_sac, aes(x=factor(day), y=mean_count, colour=yeastsample, group=c("low","high"))) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x))) +
geom_line(size=0.8) +
geom_point(size=2, shape=21, fill="white") +
theme_bw()
data_sac_ggplot + geom_errorbar(aes(ymin=mean_count-low, ymax=mean_count+high), width=0.1, position=pd)
data_sac_ggplot + scale_y_log10(breaks=c(1000,10000,100000,1000000,10000000,100000000,1000000000))
Thanks!
Picture of plot:

Related

Labels not appearing in x-axis of second graph when using ggarange

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

How to show the part of the errorbar lines which are within the plot margins using `ggplot2`?

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))

Bar chart with error bar using log10 trans

I'm new using R and ggplot library, and I'm trying to build a bar chart with an error bar, like this:
pd <- position_dodge(0.80)
ggplot(aqp1) + geom_bar( aes(x=Type, y=Error, fill=Set, colour=Set, group=Set), width=0.75, stat="identity", alpha=1, position=pd)
+ geom_errorbar( aes(x=Type, ymin=Error,ymax=Max.Error, colour=NULL, group=Set), position=pd, width=0.1, colour="black", alpha=1, size=0.1)
+ theme_light() + labs(title="", x="Sizes", y="Relative error (%)")
The only thing is that I would like logscale the y-axis, so I tried using the scale_y_log10 function:
ggplot(aqp1) + geom_bar( aes(x=Type, y=Error, fill=Set, colour=Set, group=Set), width=0.75, stat="identity", alpha=1, position=pd)
+ geom_errorbar( aes(x=Type, ymin=Error,ymax=Max.Error, colour=NULL, group=Set), position=pd, width=0.1, colour="black", alpha=1, size=0.1)
+ theme_light() + labs(title="", x="Sizes", y="Relative error (%)")
+ scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x)))
But I have a bizarre result, where the errorbar has another scale, different from the bar chart.
How can I fix this? I tried using + ylim(10^-2, 10^1) but it doesn't work.
I just want to quote #eipi10
A bar chart doesn't work well on a log scale, because the bars' baseline is set to 1 (10^0) instead of zero
This explains, why the images go wrong.

Shift text in ggplot up

Using the this code gives the plot printed below. As you can see the percentages are printed on the border of the bars. I would like to have them above the bars. Is there a way to achieve this?
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +geom_text(aes(y = (..count..),label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen") + theme(legend.position="none")
Just add an arbitrary value to y.
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +geom_text(aes(y = (..count..) + 10,label = scales::percent((..count..)/sum(..count..))), stat="bin",colour="darkgreen") + theme(legend.position="none")
Or, as per Heroka's comment, use vjust, which is a better solution
p <- ggplot(data=iris, aes(x=factor(Species), fill=factor(Species)))
p + geom_bar() + scale_fill_discrete(name="Species") + labs(x="") +
geom_text(aes(y = (..count..),
label = scales::percent((..count..)/sum(..count..))),
stat="bin",
colour="darkgreen", vjust = -0.5) +
theme(legend.position="none")
But as this makes things quite cramped at the top you might want to add + expand_limits(y = c(0, 60)) to give you a bit more space for the labels.

Change color median line ggplot geom_boxplot()

I would like to change the color of the median line in geom_boxplot(). I have looked and can't find a way to do it. I have posted the R code here that I am using but I just really need a reference to how to change the color.
ggplot(invitro2) +
geom_boxplot(aes(x = reorder(CANCER_TYPE,tmedian), y = GeoMedian_IC50)) +
xlab("") +
geom_point(aes(x = reorder(CANCER_TYPE,tmedian), y = GeoMedian_IC50)) +
theme_bw() +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
annotation_logticks(sides="l") +
theme(axis.text.x=element_text(angle=45,size=10,hjust=1),
panel.grid.major = element_blank())
You can use the details of the plot, to derive the coordinates of where the median line is, and then add colour to it using geom_segment.
library(ggplot2)
p <- ggplot(mtcars, aes(factor(am), mpg)) + geom_boxplot()
dat <- ggplot_build(p)$data[[1]]
p + geom_segment(data=dat, aes(x=xmin, xend=xmax,
y=middle, yend=middle), colour="red", size=2)
Also had to increase the size of the line so that it covers the original black median
line

Resources