I would like to add the mean of each condition at the base of my bar chart in R. The final product looks something like this in excel (note the means are displayed at the base of each bar):
My current code is as follows:
pmrtBar <- ggplot(reslagdfClean,aes(x=Condition, y=PMMissProp*100)) + stat_summary(fun.y = mean, geom = "bar", fill = cbPalette) +
theme(axis.title=element_text(size=12)) +
stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width=.1, size = .25) +
coord_cartesian(ylim=c(0,50)) +
labs(x = "Condition", y="Mean Mean Miss Proportion (%)") +
apatheme
pmrtBar
I am new the R environment. Any feedback on the code above is also appreciated.
It's always good to add a reproducible example to your question.
Converting my comment to an answer with the use of some example data:
# example data
dat <- data.frame(id = c("ACT","Blank","None"),
mn = c(0.3833,0.38,0.4033),
se = c(0.1,0.15,0.12))
# creating the plot
ggplot(dat, aes(x=id, y=mn, fill=id)) +
geom_bar(stat="identity", width=0.7) +
geom_errorbar(aes(ymax = mn + se, ymin = mn - se), width=0.25) +
geom_text(aes(y = 0.2, label = paste(mn*100, "%"))) +
labs(x = "\nCondition", y = "Proportion (%)\n") +
scale_y_continuous(breaks = seq(0.15, 0.55, 0.05), labels = scales::percent) +
coord_cartesian(ylim = c(0.15,0.55)) +
theme_minimal(base_size = 14) +
theme(panel.grid.major.y = element_line(linetype = 2, color = "grey80", size = 1))
which results in:
Related
I'm trying to create a plot like this image below where the individual data lines are in between the box plots. Image to create in R ggplot2
The closest I am getting is something like this:
Image using ggplot2 but it looks a bit cluttered with the lines/points behind.
data1 %>%
ggplot(aes(Time,Trait)) +
geom_line(aes(group=ID), position = "identity")+
geom_point(aes(group=ID), shape=21, colour="black", size=2, position = "identity")+
geom_boxplot(width=.5,position = position_dodge(width=0.9), fill="white") +
stat_summary(fun.data= mean_cl_boot, geom = "errorbar", width = 0.1, position = position_dodge(width = .9)) +
stat_summary(fun = mean, geom = "point", shape = 18, size=3, position = "identity")+
facet_wrap(~Cond) +
theme_classic()
Any tips would be greatly appreciated!
One option to achieve your desired result would be to make use of continuous x scale. Doing so makes it possible to shift the box plots to the left or to right and vice versa for the points and lines:
Making use of some random data to mimic your real data set.
data1$Time1 <- as.numeric(factor(data1$Time, levels = c("Pre", "Post")))
data1$Time_box <- data1$Time1 + .1 * ifelse(data1$Time == "Pre", -1, 1)
data1$Time_lp <- data1$Time1 + .1 * ifelse(data1$Time == "Pre", 1, -1)
library(ggplot2)
ggplot(data1, aes(x = Time_box, y = Trait)) +
geom_line(aes(x = Time_lp, group=ID), position = "identity")+
geom_point(aes(x = Time_lp, group=ID), shape=21, colour="black", size=2, position = "identity")+
geom_boxplot(aes(x = Time_box, group=Time1), width=.25, fill="white") +
stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = 0.1) +
stat_summary(fun = mean, geom = "point", shape = 18, size=3, position = "identity") +
scale_x_continuous(breaks = c(1, 2), labels = c("Pre", "Post")) +
facet_wrap(~Cond) +
theme_classic()
DATA
set.seed(42)
data1 <- data.frame(
ID = rep(1:10, 4),
Time = rep(c("Pre", "Post"), each = 10),
Trait = runif(40),
Cond = rep(c("MBSR", "SME"), each = 20)
)
EDIT If you want to two boxplots side by side it's basically the same. However in that case you have to map the interaction of Time1 and the variable mapped on fill on the group aesthetic in geom_boxplot (and probably the error bars as well):
library(ggplot2)
set.seed(42)
data1 <- data.frame(
ID = rep(1:10, 4),
Time = rep(c("Pre", "Post"), each = 10),
Fill = rep(c("Fill1", "Fill2"), each = 5),
Trait = runif(40),
Cond = rep(c("MBSR", "SME"), each = 20)
)
ggplot(data1, aes(x = Time_box, y = Trait)) +
geom_line(aes(x = Time_lp, group=ID, color = Fill), position = "identity")+
geom_point(aes(x = Time_lp, group=ID, fill = Fill), shape=21, colour="black", size=2, position = "identity")+
geom_boxplot(aes(x = Time_box, group=interaction(Time1, Fill) , fill = Fill), width=.25) +
stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = 0.1) +
stat_summary(fun = mean, geom = "point", shape = 18, size=3, position = "identity") +
scale_x_continuous(breaks = c(1, 2), labels = c("Pre", "Post")) +
facet_wrap(~Cond) +
theme_classic()
I wrote some code to make a graph (both below)
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) +
stat_summary(fun = "mean", geom = "bar", position = "dodge") +
theme_classic() +
scale_fill_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
scale_color_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
labs(x = 'Condition', y = 'Conflict (AUC)') +
scale_x_discrete(labels = c('Animal','Smoking')) +
coord_cartesian(ylim=c(0,1.5)) +
scale_y_continuous(expand = c(0,0))
p +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))
However, I recently read about 'ggpattern' and wondered if anyone could help me add some diagonal black lines to the yellow bars in my plot (e.g. ex-smokers conflict). I have tried multiple ways, but adding 'geom_col_pattern' to the code seems to mess up the Y axis and provide overall conflict for each condition (animal, smoking) rather than separately for smokers and ex-smokers. I think the 'geom_col_pattern' perhaps is not compatible with the 'stat_summary' I have in my code. Does anyone have any suggestions?
Thank you
Instead of adding a geom_col_pattern on top of your plot, just update the geom argument of stat_summary.
#replicate of your dataframe
for_plots <- data.frame(matrix(nrow = 100, ncol=0))
for_plots$condition <- sample(rep(c("Animal", "Smoking"), 100), 100)
for_plots$smoking_status <- sample(rep(c("Smokers", "Ex"), 100), 100)
n_smoking <- length(which(for_plots$condition == "Smoking"))
for_plots$conflict[for_plots$condition=="Smoking"] <- sample(seq(0.8, 1.3, length.out = n_smoking), n_smoking)
n_animal <- length(which(for_plots$condition == "Animal"))
for_plots$conflict[for_plots$condition=="Animal"] <- sample(seq(0.5, 1, length.out = n_animal), n_animal)
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) +
stat_summary(aes(pattern=smoking_status),
fun = "mean", position = "dodge",
geom = "bar_pattern", pattern_fill="black", colour="black") + #edited part
theme_classic() +
scale_fill_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
scale_color_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
labs(x = 'Condition', y = 'Conflict (AUC)') +
scale_pattern_manual(values=c("none", "stripe"))+ #edited part
scale_x_discrete(labels = c('Animal','Smoking')) +
coord_cartesian(ylim=c(0,1.5)) +
scale_y_continuous(expand = c(0,0))
p +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))
Sorry my bad english, for not pasting the code, and for asking questions because I am not very familiar with R. I am a beginner. There's my notice and the graph I must find:
I read the R documentation to solve this problem but I was unable to figure out the solution.
Actually I found this with this script I used. But i've got not clue for adding errorbar I tried geom_errorbar(aes(ymin = mean-se, ymax = mean+se)) but surely I've mistaken myself
`rm(list=ls() )
library(dplyr)
library(ggplot2)
library(ggpubr)
Sparrows <- read.delim("C:/Users/detar/Downloads/Sparrows.txt")
View(Sparrows)
str(Sparrows)
jitter<-filter(Sparrows,day == 4)
x<-ggplot(jitter,
aes (x = rank_name,
y = logit.motility,)) + geom_point(colour = "cyan") +
xlab("Social Rank") +
ylab("Logit(Proportion of motile sperm") +
labs(title =("Ejaculate quality covaries with social rank
of male House Sparrows")) +
theme(plot.title = element_text(hjust = 0.5)) +
scale_x_discrete(breaks=c("D","S1","S2","S3"), labels=c("Dominant", "Subordinate 1", "Subordinate 2", "Subordinate 3"))
x2<-x + theme_classic() + theme(plot.title = element_text(hjust = 0.5, size = 14))
Thanks for your help
Benjamin
So I added
table <- jitter %>%
group_by(rank_name) %>%
summarize(Mean = mean(logit.motility, na.rm=TRUE),
SEM = sd( logit.motility, na.rm=TRUE) / sqrt(15)
) %>% as.data.frame()
x2<-x + theme_classic() + theme(plot.title = element_text(hjust = 0.5, size = 14)) + geom_errorbar(data = summary_table,
aes(x =rank_name,
y =logit.motility,
ymin =Mean - SEM ,
ymax =Mean + SEM ,
colour = "black",
width = 1 ))
But an error occured
Warning: Ignoring unknown aesthetics: y So am i mistaken in those arguments
And a new time, i thank you
The solution to this question is identical to the solution here but all possible duplicates I have found are about bar plots, so here is one answer to a question about scatter plots.
First, the data, since the question has none.
df1 <- iris[4:5]
Now the graph. Any of geom_errorbar or stat:summary with geom = "errorbar" could be used.
library(ggplot2)
ggplot(df1, aes(x = Species, y = Petal.Width)) +
geom_point(aes(colour = "lightblue")) +
scale_color_manual(values = "lightblue") +
stat_summary(geom = "point", fun.y = mean) +
stat_summary(geom = "errorbar", fun.data = mean_se,
position = "dodge", width = 0.2)
I am trying to overlay a bar chart with a line graph on a single plot with ggplot in R. My line graph works fine but the data are much larger than the data for the bar chart component.
How could I use an additional scale for this bar chart or do something that will get this to look nice all in one graph.
Here is my plot code thus far:
chart <- data.frame("QuantileName" = 1:5, "AvgLoss" = c(100, 500, 1000, 2500, 3000), "AvgFactor" = c(1.0, 1.1, 1.3, 1.4, 1.5))
Plot <- ggplot(chart, aes(x = 1:5)) +
scale_x_continuous(name = "Quintile", limits = c(0, 5 + .5), breaks = seq(1, 5)) +
geom_line(aes(y = AvgLoss, colour = "AvgLoss")) +
geom_bar(aes(y = AvgFactor, colour = "AvgFactor" ), stat = "identity") +
geom_text(aes(y = AvgLoss, label = round(AvgLoss)), position = position_nudge(x = .3)) +
geom_point(aes(y = AvgLoss)) +
ylab("AvgLoss") +
scale_colour_manual("",breaks = c("AvgLoss","AvgFactor"), values = c("AvgLoss" = "red", "AvgFactor" = "grey")) +
ggtitle("Quintile Plot") +
theme(plot.title = element_text(hjust=0.5))
Plot
Thank you for any help!
Essentialy, multiply your AvgFactor variable by a number
+ geom_bar(aes(y = AvgFactor*1000, colour = "AvgFactor" ), stat = "identity")
and set
+ scale_y_continuous(sec.axis = sec_axis(~ ./1000, name = "AvgFactor"))
so your plot code would look like
Plot <- ggplot(chart, aes(x = 1:5)) +
scale_x_continuous(name = "Quintile", limits = c(0, 5 + .5),
breaks = seq(1, 5)) +
geom_bar(aes(y = AvgFactor*1000, colour = "AvgFactor" ),
stat = "identity") +
geom_line(aes(y = AvgLoss, colour = "AvgLoss")) +
geom_text(aes(y = AvgLoss,
label = round(AvgLoss)),
position = position_nudge(x = .3)) +
geom_point(aes(y = AvgLoss)) +
ylab("AvgLoss") +
scale_colour_manual("",breaks = c("AvgLoss","AvgFactor"),
values = c("AvgLoss" = "red", "AvgFactor" = "grey")) +
ggtitle("Quintile Plot") +
theme(plot.title = element_text(hjust=0.5)) +
scale_y_continuous(sec.axis = sec_axis(~ ./1000, name = "AvgFactor"))
However, I think it is probably more elegant to avoid secondary axes whenever possible.
It may be useful to know that geom_col(...) is shorthand for geom_bar(..., stat = 'identity')
I need to be able to show the mean value in ggplot box plot. Below works for a point but I need the white dashed lines? Any body help?
x
Team Value
A 10
B 5
C 29
D 35
ggplot(aes(x = Team , y = Value), data = x)
+ geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9))
+ stat_summary(fun.y=mean, colour="red", geom="point")
Here's my way of adding mean to boxplots:
ggplot(RQA, aes(x = Type, y = engagementPercent)) +
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) +
scale_fill_brewer(palette = "Set2") +
stat_summary(fun.y = "mean", geom = "text", label="----", size= 10, color= "white") +
ggtitle("Participation distribution by type") +
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank())
ggplot(df, aes(x = Type, y = scorepercent)) +
geom_boxplot(aes(fill = Type),alpha = .6,size = 1) +
scale_fill_brewer(palette = "Set2") +
stat_summary(fun.y = "mean", geom = "point", shape= 23, size= 3, fill= "white") +
ggtitle("score distribution by type") +
theme(axis.title.y=element_blank()) + theme(axis.title.x=element_blank())
I would caution against using text to this and do geom_line instead as text is offset slightly and gives the wrong portrayal of the mean.
Hey user1471980, I think people are more inclined to help if you have a unique user name but then again you have a lot of points :)
this is a hack but does this help:
Value<-c(1,2,3,4,5,6)
Team<-c("a","a","a","b","b","b")
x<-data.frame(Team,Value) #note means for a=2, mean for b=5
ggplot(aes(x = Team , y = Value), data = x) + geom_boxplot (aes(fill=Team), alpha=.25, width=0.5, position = position_dodge(width = .9)) +
annotate(geom="text", x=1, y=2, label="----", colour="white", size=7, fontface="bold", angle=0) +
annotate(geom="text", x=2, y=5, label="----", colour="white", size=7, fontface="bold", angle=0)