【问题标题】:linetype mapping in ggplot2 using geom_line()使用 geom_line() 在 ggplot2 中进行线型映射
【发布时间】:2016-03-11 13:50:48
【问题描述】:

我有一个如下所示的数据集:

   case prop weight  res
1     A   10    0.1 0.81
2     A   20    0.2 0.78
3     A   30    0.3 0.76
4     A   40    0.4 0.58
5     A   50    0.1 0.62
6     A   10    0.2 0.73
7     A   20    0.3 0.68
8     A   30    0.4 0.70
9     A   40    0.1 0.55
10    A   50    0.2 0.78
11    A   10    0.3 0.64
12    A   20    0.4 0.68
13    A   30    0.1 0.75
14    A   40    0.2 0.67
15    A   50    0.3 0.59
16    A   10    0.4 0.77
17    A   20    0.1 0.57
18    A   30    0.2 0.61
19    A   40    0.3 0.60
20    A   50    0.4 0.72
21    B   10    0.1 0.66
22    B   20    0.2 0.66
23    B   30    0.3 0.76
24    B   40    0.4 0.57
25    B   50    0.1 0.83
26    B   10    0.2 0.68
27    B   20    0.3 0.76
28    B   30    0.4 0.65
29    B   40    0.1 0.70
30    B   50    0.2 0.72
31    B   10    0.3 0.60
32    B   20    0.4 0.82
33    B   30    0.1 0.85
34    B   40    0.2 0.72
35    B   50    0.3 0.74
36    B   10    0.4 0.67
37    B   20    0.1 0.60
38    B   30    0.2 0.62
39    B   40    0.3 0.76
40    B   50    0.4 0.87
41    C   10    0.1 0.48
42    C   20    0.2 0.77
43    C   30    0.3 0.70
44    C   40    0.4 0.65
45    C   50    0.1 0.73
46    C   10    0.2 0.70
47    C   20    0.3 0.80
48    C   30    0.4 0.68
49    C   40    0.1 0.58
50    C   50    0.2 0.63
51    C   10    0.3 0.71
52    C   20    0.4 0.68
53    C   30    0.1 0.84
54    C   40    0.2 0.66
55    C   50    0.3 0.77
56    C   10    0.4 0.67
57    C   20    0.1 0.64
58    C   30    0.2 0.74
59    C   40    0.3 0.81
60    C   50    0.4 0.62

数据可以通过以下代码生成:

 case = rep(c("A", "B", "C"), each=20)
    prop = rep(c("10", "20", "30", "40", "50"), 12)
    weight = as.factor(rep(c(0.1, 0.2, 0.3, 0.4), 15))
    res = round(rnorm(n=60, 0.7, 0.1), 2)

    dat = data.frame(case, prop, weight, res)
    dat

我想要实现的是将“prop”作为x轴,将“res”作为y轴,同时使用不同的颜色来区分“case”,并使用不同的线型来区分“weight”。例如,如果权重=0.1,则使用实线;如果 weight=0.2,使用虚线等。从下面的代码:

ggplot(data = dat, aes(x=prop, y=res, group=case, color=case)) +
  geom_line() +
  geom_point() + 
  theme_bw()

我只能得到以下不想要的情节......

我尝试添加geom_line(aes(linetype=weight)),但显示错误

Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

有没有办法将“权重”映射到 ggplot2 中的线型?谢谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以创建一个新变量来分组并用于指定线型..

    dat$case.weight <- paste0(dat$case, dat$weight)
    

    .. 并添加scale_linetype_manual():

    ggplot(data = dat, aes(x=prop, y=res, group=case.weight, color=case, linetype=case.weight)) +
      scale_linetype_manual(values=rep(c("solid", "dashed", "dotted", "dotdash"),3), 
                            breaks=c("A0.1","A0.2", "A0.3", "A0.4"),
                            labels=c("0.1", "0.2", "0.3", "0.4"),
                            name="weight") +
      geom_line() +
      geom_point() + 
      theme_bw()
    

    【讨论】:

    • 不错的一个。也许也包括方面。这将使情节更加清晰。
    • @beetroot:非常感谢!一个简单的问题:有没有办法添加一个图例来显示不同的线型?我希望读者看到“实心”、“虚线”、“点”、“点破”分别对应于 0.1、0.2、0.3 和 0.4。谢谢!
    • @alittleboy 我编辑了我的答案以包含一个重量图例。
    猜你喜欢
    • 2017-11-01
    • 2016-11-25
    • 2017-03-28
    • 2018-10-12
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    相关资源
    最近更新 更多