我试图创建一个简单的箱线图,其连线与此问题中描述的相似:Connect ggplot boxplots using lines and multiple factor。 但是,该示例中的交互项会产生错误:
geom_path:每组只包含一个观察。你需要吗? 调整群体审美?
我想使用索引变量连接每个点。这是代码:
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)
答案 0 :(得分:5)
一些类似于我的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
其余部分与您的代码相同。
(其余的垂直线来自箱线图。)