Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connected points in ggplot boxplot

I'm trying to create a simple boxplot with connected lines similar to the one described in this question: Connect ggplot boxplots using lines and multiple factor. However, the interaction term in that example produces an error:

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

I would like to connect each point using the index variable. Here is the code:

group <- c("A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B")
session <- c("one","two","one","two","one","two","one","two","one","two","one","two","one","two","one","two","one","two","one","two")
value <- c(1.02375,1.01425,1.00505,0.98105,1.09345,1.09495,0.98255,0.90240,0.99185,0.99855,0.88135,0.72685,0.94275,0.84775,1.01010,0.96825,0.85215,0.84175,0.89145,0.86985)
index <- c(1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10)
df <- data.frame(group,session,value,index)

# Graph plots
p <- ggplot(df, aes(x=group, y=value, fill=session))
p <- p + geom_boxplot(color="grey40", outlier.alpha=0.0) #alpha=0.6
p <- p + stat_summary(fun.y=mean,geom="point",pch="-",color="white",size=8, position = position_dodge(width=0.75)) # size=2 color="black"
p <- p + geom_point(size=2, alpha=0.6, aes(group=session), data=df, position = position_dodge(width=0.75))
p <- p + geom_line(aes(group = index), alpha = 0.6, colour = "black", position = position_dodge(width=0.75), data=df) #
p <- p + scale_fill_manual(values=c("#969696","#74c476"))
p <- p + theme(
       axis.text.x = element_text(colour = "black"), #angle = 60, hjust = 1
       axis.text.y = element_text(colour = "black"),
       axis.title.x = element_blank(), #element_text(colour = "black"),
       axis.title.y = element_text(colour = "black"),
       legend.position = "none"
       #panel.background = element_blank(), #element_rect(fill="white", colour="black", size=2),
       #panel.grid.major = element_blank(),
       #panel.grid.minor = element_blank(),
       #panel.border = element_blank(),
       #axis.line = element_line(size=1.5, colour = "black")
       #panel.grid.major = element_line(size = .5, colour = "grey")
       )
ggsave("~/Desktop/test.pdf", width=4, height=6, units=c("in"), plot=p)

However, that produces only vertical lines as in this image: enter image description here

like image 736
Jon Avatar asked Jan 01 '26 13:01

Jon


1 Answers

Some changes analogous as in my other answer:

df <- data.frame(group, session, value, index, U = interaction(session, group))
p <- ggplot(df, aes(x = U, y = value, fill = session)) + 
  scale_x_discrete(labels = rep(unique(group), each = 2))
p <- p + geom_line(aes(group = index), alpha = 0.6, colour = "black", data = df) 
                                                             # no need for dodge

The rest is the same as in your code.

enter image description here

(The remaining vertical lines are from the boxplot.)

like image 120
Julius Vainora Avatar answered Jan 03 '26 01:01

Julius Vainora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!