r - ggplot2:组合组、颜色和线型

标签 r ggplot2

我有一个包含 3 个因子( conditionmeasuretime )的数据库,并且想使用 x 轴、颜色/组和线型绘制它们。

例如,我的数据如下所示:

DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
                 measure = rep(c("A", "A", "B", "B"), 2),
                 time = rep(c("pre-test", "post-test"), 4),
                 score = 1:8)

> DT
     condition measure      time score
1      control       A  pre-test     1
2      control       A post-test     2
3      control       B  pre-test     3
4      control       B post-test     4
5 experimental       A  pre-test     5
6 experimental       A post-test     6
7 experimental       B  pre-test     7
8 experimental       B post-test     8

我的目标是绘制这样的图形:

enter image description here

我试过:
ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
    geom_line() +
    geom_point()

但它返回此错误:
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

我错过了什么?

最佳答案

你想用

ggplot(DT, aes(time, score, group = interaction(measure, condition), 
               color = measure, linetype = condition)) +
  geom_line() + geom_point()

enter image description here

因为实际的分组不仅仅是按 measure也来自 condition .按 measure 分组时独自一人,我想它要求的是平行四边形而不是直线。

关于r - ggplot2:组合组、颜色和线型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53581787/

相关文章:

python - 在 Pandas Dataframe 的排序列中查找缺失的数字

r - ggplot误差线问题

r - facet_zoom 正在放大到不同的 x 轴值

r - 密度图矩阵,每个图覆盖两个分布

r - 在ggplot中设置日期范围

r - Knitr:使用 scales 包在 ggplot 中获取计数

r - 函数调用向量 v(f(0), f(1), ..., f(2))

r - r 中带有 coplot 的特定部分

r - 使用grid.arrange时出现错误 "Breaks and labels are different lengths"

r - 同轴限制垂直/水平、固定方面、小平面中的自由缩放