您好,我有一个更大的数据帧,但示例虚拟df如下所示:
set.seed(23)
df = data.frame(name = c(rep("Bob",8),rep("Tom",8)),
topic = c(rep(c("Reading","Writing"),8)),
subject = c(rep(c("English","English","Spanish","Spanish"),4)),
exam = c(rep("First",4),rep("Second",4),rep("First",4),rep("Second",4)),
score = sample(1:100,16))
我必须用下图所示的方式绘制它(对于我的原始数据框),但用线连接了Exam变量中第一和第二类之间每个名称对应的分数,我try 了geom_line(aes(group=name))
,但这些线的连接方式不正确.有没有办法连接同样尊重fill
变量分组的点,就像position_dodge()
帮助分隔点的填充分组一样?非常感谢!
library(ggplot2)
df %>% ggplot(aes(x=topic,y=score,fill=exam)) +
geom_boxplot(outlier.shape = NA) +
geom_point(size=1.75,position = position_dodge(width = 0.75)) +
facet_grid(~subject,switch = "y")