【发布时间】:2022-01-22 05:56:35
【问题描述】:
我做了以下数据集:
before <- c(100, 120, 140, 130, 120, 100, 100)
after <- c(140, 100, 160, 120, 90, 70, 70)
pain_group <- c(1, 0, 1, 0, 0, 0, 0)
d <- data.frame(before=before, after=after, pain_group=pain_group)
d$id <- 1:nrow(d)
d <- tidyr::gather(d, Measurement, quantity, -id)
我已经用单独的点和连接线将数据绘制在箱线图中:
ggplot(d2, aes(Measurement, quantity_cluster2)) +
geom_boxplot() +
geom_point() +
geom_line(aes(group = id), color = 'grey') +
scale_x_discrete(limits = c('before', 'after'))
但是我希望pain_group 用不同的颜色线(和点)分隔。我怎样才能做到这一点?
提前致谢!
【问题讨论】:
-
你能让这个可重现,以便代码可以按原样运行吗? (例如,您引用 d2 和一个名为 quantity_cluster2 的变量)
-
将
color = factor(pain_group)放入您的aes()。从geom_line层中删除color = 'grey'。
标签: r ggplot2 data-visualization boxplot