Reducing size of error bar caps in ggplot2 - r

I am trying to plot a graph with the following code:
p<-ggplot(averagedf, aes(x=Time, y=average,col=Strain)) +
geom_line() +
geom_point()+
geom_errorbar(aes(ymin=average-sem,ymax=average+sem))+
theme_classic()+
theme(legend.position = "none")
And the graph looks like this- which is all fine, except that the caps(?) of the error bars are too wide:First plot
In order to reduce the width of the caps, I set width to 2, but now the caps are not centred around the the vertical line of the error bar. Does anyone have any idea on how to change the size of the caps without messing up its position?
p<-ggplot(averagedf, aes(x=Time, y=average,col=Strain)) +
geom_line() +
geom_point()+
geom_errorbar(aes(ymin=average-sem,ymax=average+sem,width=2))+
theme_classic()+
theme(legend.position = "none")
Second plot

I wonder if that's just an artifact of the resolution of the graphics render. Without your exact data, it's hard to reproduce your exact plot, but with a similar example using an included R dataset, I don't have this issue.
library(tidyverse)
ChickWeight %>%
ggplot(aes(x = Time, y = weight, color = factor(Diet))) +
stat_summary(fun = mean, geom = "line") +
stat_summary(fun = mean, geom = "point") +
stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.5) +
theme_classic() +
theme(legend.position = "none")
Created on 2021-03-15 by the reprex package (v1.0.0)

Related

Ggplot2 fill by one condition, but shade by another

I am having trouble with ggplot2. I am trying to plot this boxplot that has several boxes for every x-axis value. Each set of boxplots is divided into two sets, features, and resolutions, with every feature set having boxes for every resolution. My issue is that I want to color(and fill) by feature set, but have different shades of that color for every resolution. I have only been able to color by feature and fill by resolution so far, and this is my code:
df %>%
ggplot( aes(x=partition, y=value, fill=resolution,color=feature_set)) +
scale_color_manual(values = c('black','blue3','darkred')) +
geom_boxplot(aes(color=feature_set,fill=resolution),lwd=0.7) +
scale_fill_brewer(palette = "Pastel1") +
theme(
legend.position="right",
plot.title = element_text(size=11)
) +
theme_bw() +
geom_text(data = df,aes(y=1.1,label=max_clusters,color=feature_set),size = 2.5,
position = position_dodge(width = 0.85),check_overlap = TRUE) +
xlab('label') +
ylab('label') +
ggtitle("title") +
ylim(0,1.1)
Thank you so much in advance.

How to scale a bar plot using ggplot2?

I want my bar plot to take up less space, currently there is a lot of white space which is not neccessary. I would like the bins to be a lot closer than they currently are. And since there are only two categories in the X axis I do not see why there is so much space between them. - increasing the bin width would make white space go away, but then the bins become unnaturally large.
Code:
# Creates plot to show response-rate
hubspot_ordering %>%
ggplot(aes(x = Replied)) +
geom_bar(color = "black",
fill = "steelblue",
stat = "count",
width = 0.1,
position = position_dodge(0.9)) +
xlab("Kommuners respons på e-post og oppringing") +
scale_x_discrete(labels = c("Besvart",
"Ingen respons")) +
ylab("Antall kommuner") +
theme(element_line(size = 0.5)) +
theme_bw() -> plot_response_rate
Output:
You can change the aspect.ratio in your theme using the following code:
df <- data.frame(x=factor(LETTERS[1:2]), y=sample(1:100, 2))
library(ggplot2)
ggplot(data=df, aes(x=x, y=y, width=.1)) +
geom_bar(stat="identity", position="identity") +
theme(aspect.ratio = 2/1) +
labs(x="", y="")
Output aspect.ratio=2:
Output aspect.ratio=1:

Add mean to grouped box plot in R with ggplot2

I created a grouped box plot in R with ggplot2. However, when I want to add the mean, the dots appear between the two boxes in a group. How can I change it such that the dots are within each box?
Here my code:
ggplot(results, aes(x=treatment, y=effect, fill=sex)) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=20, size=3, color="red")`
You can use position_dodge2. Because points and boxplots have differing widths, you will need to trial and error with the width argument to centralise the dots.
ggplot(mtcars, aes(x=factor(gear), y=hp, fill=factor(vs))) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=20, size=3, color="red",
position = position_dodge2(width = 0.75,
preserve = "single"))
In most cases, you will not be able to place the points inside each grouped box as they overlap with each other through the axes. One alternative is to use facet_wrap.
Here is one example with iris data:
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, fill=Species)) +
geom_point(aes(color = Species), shape = 20, size = 3) +
geom_boxplot(alpha = 0.8) +
facet_wrap(~Species)
If you don't want the color of the points to be the same as the color of the boxplots, you have to remove the grouping variable from the aes inside the geom_point. Again, with the iris example,
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, fill=Species)) +
geom_boxplot(alpha = 0.8) +
geom_point(shape = 20, size = 3, color = 'red') +
facet_wrap(~Species)
Note that the ggplot2 package works in layers. Therefore, if you add the geom_point layer after the geom_boxplot layer, the points will be on the top of the boxplot. If you add the geom_point layer before the geom_boxplot layer, the points will be in the background.
Edit:
If what you want is to add a single point in your boxplot to indicate the mean, you can do something like:
iris %>%
group_by(Species) %>%
mutate(mean.y = mean(Sepal.Width),
mean.x = mean(Sepal.Length)) %>%
ggplot(aes(x=Sepal.Length, y=Sepal.Width, fill=Species)) +
geom_boxplot(alpha = 0.8) +
geom_point(aes(y = mean.y, x = mean.x), shape = 20, size = 3, color = 'red')
But be aware that it would probably require some calibration on the x axis to make it exactly in the middle of each box.

geom_path is not drawing a line

I'm making depth profiles with ggplot. Some of the lines are drawn between the variable points using geom_path but some are not, even when I try adding "group=1" (which was the only solution I've found for this problem). I'm doing multiple plots for different lakes and for each lake there is one or multiple variables not getting a line by using geom_path. For the code below only the Chl.a variable is not drawing a line, all the others do. What could this depend on?
I also tried geom_line instead but this only worked for some variables since the it draws the line following the x-axis, but I want the line to go vertically following the y-axis. Can I achieve this using geom_line since geom_path doesn't seem to work for all variables?
gs <- ggplot(goodspirit, aes(y=goodspirit$Depth.m)) +
geom_point(aes(x=Temp, colour= "Temp")) +
geom_path(aes(x=Temp, color = "Temp"), size=1.5) +
geom_point(aes(x=zDOmg, color ="z(DO mg/L)")) +
geom_path(aes(x=zDOmg, color ="z(DO mg/L)"), size=1.5) +
geom_point(aes(x=Chl.a, color ="Chl.a"), na.rm = TRUE) +
geom_path(aes(x=Chl.a, color ="Chl.a"), na.rm = TRUE, size=1.5) +
geom_point(aes(x=zN2O, color ="z(N2O.nM)"), na.rm = TRUE) +
geom_line(aes(x=zN2O, color ="z(N2O.nM)"), na.rm = TRUE, size=1.5) +
geom_point(aes(x=Sal.ppt, color ="Salinity.ppt"), na.rm = TRUE) +
geom_line(aes(x=Sal.ppt, color ="Salinity.ppt"), na.rm = TRUE, size=1.5)+
geom_point(aes(x=zph, color ="z(pH)")) +
geom_path(aes(x=zph, color ="z(pH)"), size=1.5) +
scale_x_continuous(position = "top", limits=c(-3,5), expand = c(0,0))+
scale_y_reverse(expand = c(0.05,0))+
ylab("Depth (m)") + xlab("x") + ggtitle("Good spirit lake") + labs(colour
= "Parameters") +
theme(plot.title = element_text(hjust = 0.5)) + theme_light()
gs
enter image description here

R plot errorbars with outliers

I'm trying to get the same aesthetic as below where the error bars look the same and have outliers shown. geom_errorbar and stat_summary is somewhat similar, but doesn't provide outliers. geom_boxplot provide outliers, but the box takes up too much space and I would prefer the slimmed down appearance below. Does anyone know how to achieve this with ggplot or without?
We can set the width of the boxplot to 0 then use stat_boxplot & stat_summary to produce the rest of the plot in the picture you added
library(ggplot2)
p1 <- ggplot(data = iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot(width = 0,
outlier.colour = "red") +
stat_boxplot(geom = "errorbar", width = 0.5) +
stat_summary(fun.y = mean, geom = "point", size = 2) +
stat_summary(fun.y = mean, geom = "line", aes(group = 1)) +
theme_bw()
p1
Created on 2018-03-18 by the reprex package (v0.2.0).

Resources