I am trying to create a grouped bar chart with the points displayed in the respective bars along with error bars showing the standard deviation. The following R Script produces the graph shown in the image below. The points on the bar appear scattered within a group instead of appearing in distinct bars.
May someone point me to what am I missing?
library(dplyr)
library(ggplot2)
library(plotly)
library(ggpubr)
df <- data.frame(Sample = c("0.5-1 DMSO (24) CD3-28","0.5-1 DMSO (24) CD3-28","0.5-1 DMSO (24)",
"0.5-1 DMSO (24)","1-1 DMSO (24) CD3-28","1-1 DMSO (24) CD3-28",
"1-1 DMSO CD3-28","1-1 DMSO","1-1 DMSO (24)","1-1 DMSO (24)","3-1 DMSO",
"3-1 DMSO (24)","3-1 DMSO (24)","3-1 DMSO (24) BR","3-1 DMSO (24) BR CD3-28",
"3-1 DMSO CD3-28","3-1 DMSO (24) CD3-28","3-1 DMSO (24) CD3-28",
"0.5-1 0.1uM (24) CD3-28","0.5-1 0.1uM (24) CD3-28","0.5-1 0.1uM (24)",
"0.5-1 0.1uM (24)","1-1 0.1uM (24) CD3-28","1-1 0.1uM (24) CD3-28","1-1 0.1uM CD3-28",
"1-1 0.1uM","1-1 0.1uM (24)","1-1 0.1uM (24)","3-1 0.1uM","3-1 0.1uM (24)",
"3-1 0.1uM (24)","3-1 0.1uM (24) BR","3-1 0.1uM (24) BR CD3-28","3-1 0.1uM CD3-28",
"3-1 0.1uM (24) CD3-28","3-1 0.1uM (24) CD3-28","0.5-1 1uM (24) CD3-28",
"0.5-1 1uM (24) CD3-28","0.5-1 1uM (24)","0.5-1 1uM (24)","1-1 1uM (24) CD3-28",
"1-1 1uM (24) CD3-28","1-1 1uM CD3-28","1-1 1uM (24)","1-1 1uM (24)","1-1 1uM",
"3-1 1uM","3-1 1uM (24)","3-1 1uM (24)","3-1 1uM (24) BR","3-1 1uM (24) BR CD3-28",
"3-1 1uM CD3-28","3-1 1uM (24) CD3-28","3-1 1uM (24) CD3-28"),
Group = c("0.5_1 Stim","0.5_1 Stim","0.5_1","0.5_1","1_1 Stim","1_1 Stim","1_1 Stim","1_1","1_1","1_1",
"3_1","3_1","3_1","3_1 BR","3_1 BR","3_1 Stim","3_1 Stim","3_1 Stim","0.5_1 Stim","0.5_1 Stim",
"0.5_1","0.5_1","1_1 Stim","1_1 Stim","1_1 Stim","1_1","1_1","1_1","3_1","3_1","3_1","3_1 BR",
"3_1 BR","3_1 Stim","3_1 Stim","3_1 Stim","0.5_1 Stim","0.5_1 Stim","0.5_1","0.5_1","1_1 Stim",
"1_1 Stim","1_1 Stim","1_1","1_1","1_1","3_1","3_1","3_1","3_1 BR","3_1 BR","3_1 Stim","3_1 Stim","3_1 Stim"),
Concentration = c("DMSO","DMSO","DMSO","DMSO","DMSO","DMSO","DMSO","DMSO","DMSO","DMSO","DMSO","DMSO","DMSO","DMSO",
"DMSO","DMSO","DMSO","DMSO","0.1uM","0.1uM","0.1uM","0.1uM","0.1uM","0.1uM","0.1uM","0.1uM","0.1uM",
"0.1uM","0.1uM","0.1uM","0.1uM","0.1uM","0.1uM","0.1uM","0.1uM","0.1uM","1uM","1uM","1uM","1uM",
"1uM","1uM","1uM","1uM","1uM","1uM","1uM","1uM","1uM","1uM","1uM","1uM","1uM","1uM"),
Marker = c(29.1,35.5,20,11.8,48.9,41.2,19.4,2.49,21.6,17.7,5.52,51.5,42.5,8.5,45.2,17.1,
67,58.5,42.7,36.4,16,22.4,42.2,50.3,33.4,5.88,27.9,21.5,22.9,62.5,35.3,17,50.4,
49,65.2,65.3,52.9,37.3,19.1,16.1,52.7,46.5,45.1,30.9,28.9,6.62,13.1,87.4,52.1,19.8,63.7,55.3,88.4,66.4))
df.summary <- df %>%
dplyr::group_by(Sample, Group, Concentration) %>%
dplyr::summarize(sd = sd(Marker, na.rm = TRUE), Marker = mean(Marker))
Concn <- factor(df.summary$Concentration, levels=c("DMSO","0.1uM","1uM"))
p <- ggplot(data = df.summary, aes(x = Group, y = Marker, fill = Concn))
geom_bar(stat="identity", position=position_dodge(.9))
geom_jitter( position = position_jitter(0.5), color = "black")
geom_errorbar(data = df.summary,aes(ymin = as.double(df.summary$Marker) - as.double(df.summary$sd),
ymax = as.double(df.summary$Marker) as.double(df.summary$sd)),
width = 0.2, position=position_dodge(0.9))
scale_fill_manual(values = c("DMSO" = '#619CFF',"0.1uM" = '#F8766D',"1uM" = '#00BA38')) theme_bw()
p <- ggpar(p, x.text.angle = 45)
ggplotly(p)
CodePudding user response:
Instead of position = position_jitter(0.5)
, use position = position_jitterdodge(0.5)
. Also, you don't need to write like df.summary$sd
in geom_errorbar
part, because you already set data = df.summary
at the front of code
ggplot(data = df.summary, aes(x = Group, y = Marker, fill = Concn))
geom_bar(stat="identity", position=position_dodge(.9))
geom_jitter( position = position_jitterdodge(0.5), color = "black")
geom_errorbar(aes(ymin = as.double(Marker) - as.double(sd),
ymax = as.double(Marker) as.double(sd)),
width = 0.2, position=position_dodge(0.9))
scale_fill_manual(values = c("DMSO" = '#619CFF',"0.1uM" = '#F8766D',"1uM" = '#00BA38')) theme_bw()