【问题标题】:Default linetypes in ggplot2?ggplot2中的默认线型?
【发布时间】:2014-02-12 20:51:28
【问题描述】:

我在这里努力找出 ggplot2 中使用的默认线型。我有一个如下的数据框:

test <- structure(list(time = c(151.04167175293, 151.125015258789, 151.20832824707, 
151.29167175293, 151.375030517578, 151.45832824707, 151.54167175293, 
151.625015258789, 151.70832824707, 151.79167175293, 151.875030517578, 
151.95832824707, 151.08332824707, 151.166687011719, 151.250030517578, 
151.333343505859, 151.41667175293, 151.5, 151.58332824707, 151.666687011719, 
151.750030517578, 151.833343505859, 151.91667175293, 151.04167175293, 
151.125015258789, 151.20832824707, 151.29167175293, 151.375030517578, 
151.45832824707, 151.54167175293, 151.625015258789, 151.70832824707, 
151.79167175293, 151.875030517578, 151.95832824707, 151.08332824707, 
151.166687011719, 151.250030517578, 151.333343505859, 151.41667175293, 
151.5, 151.58332824707, 151.666687011719, 151.750030517578, 151.833343505859, 
151.91667175293, 151.04167175293, 151.125015258789, 151.20832824707, 
151.29167175293, 151.375030517578, 151.45832824707, 151.54167175293, 
151.625015258789, 151.70832824707, 151.79167175293, 151.875030517578, 
151.95832824707, 151.08332824707, 151.166687011719, 151.250030517578, 
151.333343505859, 151.41667175293, 151.5, 151.58332824707, 151.666687011719, 
151.750030517578, 151.833343505859, 151.91667175293, 151.04167175293, 
151.125015258789, 151.20832824707, 151.29167175293, 151.375030517578, 
151.45832824707, 151.54167175293, 151.625015258789, 151.70832824707, 
151.79167175293, 151.875030517578, 151.95832824707, 151.08332824707, 
151.166687011719, 151.250030517578, 151.333343505859, 151.41667175293, 
151.5, 151.58332824707, 151.666687011719, 151.750030517578, 151.833343505859, 
151.91667175293), value = c(0, 0, 5.61033323376278e-29, 1.54657131948427e-17, 
9.66035151178657e-12, 5.10066877623672e-09, 5.13016118475207e-08, 
5.63439357392781e-08, 2.30620536001425e-08, 9.08235708863003e-09, 
8.37633162831253e-09, 1.55146210545354e-07, 0, 0, 2.25358956126054e-21, 
2.08607626615495e-13, 1.98955163455139e-09, 9.66092130738616e-08, 
1.91719223607834e-07, 1.09957049687637e-07, 3.104455004177e-08, 
1.31108741641128e-08, 6.10190298289126e-08, 0, 0, 0, 0, 3.2789574202628e-30, 
6.73093043024932e-24, 7.93898871359986e-21, 1.51219769988266e-19, 
3.67432930162413e-19, 7.19692007469365e-19, 3.20756611951269e-18, 
4.69377782154479e-17, 0, 0, 0, 5.28026380068047e-35, 6.81666051928849e-28, 
2.74272295486945e-23, 3.82383180911782e-21, 1.74006456826736e-20, 
4.48768165153377e-20, 1.60533888309098e-19, 1.53676095741939e-18, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 8.71725244784488e-30, 1.04201812373392e-23, 
2.36111112593732e-21, 5.04498618818828e-20, 1.39487078953905e-19, 
9.53610723085965e-19, 2.04732743716978e-17, 2.28480170675856e-15, 
0, 0, 0, 1.1415638533226e-32, 3.46081290989027e-27, 5.11190859597535e-23, 
2.60199456175554e-21, 8.88056339654886e-21, 2.0802860544737e-20, 
3.12119181655233e-19, 3.69406751642014e-17), id = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("a", 
"b", "c", "d", "e", "f"), class = c("ordered", "factor")), dis = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("50", 
"100", "250", "50(nothing)"), class = c("ordered", "factor"))), .Names = c("time", 
"value", "id", "dis"), row.names = c("1", "3", "5", "7", "9", 
"11", "13", "15", "17", "19", "21", "23", "722", "724", "726", 
"728", "730", "732", "734", "736", "738", "740", "742", "4321", 
"4323", "4325", "4327", "4329", "4331", "4333", "4335", "4337", 
"4339", "4341", "4343", "5042", "5044", "5046", "5048", "5050", 
"5052", "5054", "5056", "5058", "5060", "5062", "8641", "8643", 
"8645", "8647", "8649", "8651", "8653", "8655", "8657", "8659", 
"8661", "8663", "9362", "9364", "9366", "9368", "9370", "9372", 
"9374", "9376", "9378", "9380", "9382", "12", "32", "52", "72", 
"92", "112", "132", "152", "172", "192", "212", "232", "7221", 
"7241", "7261", "7281", "7301", "7321", "7341", "7361", "7381", 
"7401", "7421"), class = "data.frame")

现在,我将上面的数据框绘制如下:

library(ggplot2)
ggplot(test)+
  geom_line(aes(time,value,linetype=dis))+
  facet_wrap(~id,ncol=2)+theme_bw()+
  ylab("concentration ")+coord_cartesian(xlim=c(151,181),ylim=c(-0.15,0.75))+ labs(linetype="")+
  theme(panel.margin = unit(1, "lines"))+ xlab("")+
  scale_x_continuous(limits=c(151,181),breaks=seq(151,181,5))+
  scale_y_continuous(limits=c(-0.15,1.75),breaks=seq(-0.15,1.75,0.15))+
  theme(legend.position="top",legend.key =element_blank())

输出如下:

我想知道的是默认使用什么线型?因为当我尝试使用scale_linetype_manual("", values=c(1,2,3,4)) 时,它不会产生与上面相同的linetypes

上面指定线型得到的图是

有人能澄清这个问题吗?是否可以通过指定 linetype 来达到相同的效果?我尝试这样做的原因是在我使用默认值之前,现在我需要更改一种线型。

我尝试了几个linetypes,但是当我们不手动指定线型时,我无法弄清楚linetypes 使用的是什么。谢谢

我这里没有 dis = "100" 和 "250" 的数据,但应该可以有相同的线型。

编辑

完整数据集的链接如下: https://dl.dropboxusercontent.com/u/9267938/dputtest.rda

【问题讨论】:

  • 你发布的数据集时间在(151,152),所以图基本上是空的。
  • 查看this question 的答案,了解如何使scale_linetype_manual(...) 为您工作。
  • @jlhoward 我添加了指向完整数据集的链接。我们只需要使用命令:test &lt;- read.table("https://dl.dropboxusercontent.com/u/9267938/dputtest.rda")
  • 非常有帮助。已发布回复。

标签: r ggplot2


【解决方案1】:

这是找出默认linetypes 的一种方法,ggplot 使用它们的顺序以及它们的名称。

# some data
df <- data.frame(x = 1:2, y = rep(20:1, each = 2), grp = factor(rep(1:20, each = 2)))
df

# plot
p <- ggplot(data = df, aes(x = x, y = y, linetype = grp)) +
  geom_line() +
  geom_text(aes(x = 0.95, label = grp)) +
  theme_classic() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.line = element_blank(),
        legend.position = "none")
p

显然,ggplot 中有 13 种默认线型。如果您查看绘图对象中的数据,您会发现不同 linetypes 的相应“名称”。

g <- ggplot_build(p)
g$data[[1]]

   linetype x  y PANEL group
1     solid 1  1     1     1
2     solid 2  1     1     1
3        22 1  2     1     2
4        22 2  2     1     2
5        42 1  3     1     3
6        42 2  3     1     3
7        44 1  4     1     4
8        44 2  4     1     4
9        13 1  5     1     5
10       13 2  5     1     5
11     1343 1  6     1     6
12     1343 2  6     1     6
13       73 1  7     1     7
14       73 2  7     1     7
15     2262 1  8     1     8
16     2262 2  8     1     8
17 12223242 1  9     1     9
18 12223242 2  9     1     9
19     F282 1 10     1    10
20     F282 2 10     1    10
21 F4448444 1 11     1    11
22 F4448444 2 11     1    11
23 224282F2 1 12     1    12
24 224282F2 2 12     1    12
25       F1 1 13     1    13
26       F1 2 13     1    13
27    blank 1 14     1    14
28    blank 2 14     1    14
...more blanks
39    blank 1 20     1    20
40    blank 2 20     1    20

请参阅?aes_linetype_size_shape,了解如何解释“数字名称”以及如何使用“整数、名称或偶数(最多八个)字符的字符串”指定linetype。类似的描述也可以在?par:lty和“线型规范”中找到); "五种标准点划线类型 (lty = 2:6) 对应于c("44", "13", "1343", "73", "2262")

【讨论】:

  • +1 用于显示详细信息。我可以为不同的线型分配不同的线宽吗?
  • 是的。在我的示例中,正如linetype 映射到“grp”一样,您也可以将线宽(size)映射到“grp”:geom_line(aes(size = grp))
【解决方案2】:

this answer 中所述,要让scale_linetype_manual(...) 做你想做的事,你必须将values=... 设置为一个命名向量,如下所示:

test <- read.table("https://dl.dropboxusercontent.com/u/9267938/dputtest.rda")

lt.names <- c("50(nothing)","50","100","250")  # this sets the order
lt <- setNames(c(1,2,3,4),lt.names)
library(ggplot2)
ggplot(test)+
  geom_line(aes(time,value,linetype=dis))+
  facet_wrap(~id,ncol=2)+theme_bw()+
  ylab("concentration ")+coord_cartesian(xlim=c(151,181),ylim=c(-0.15,0.75))+ labs(linetype="")+
  theme(panel.margin = unit(1, "lines"))+ xlab("")+
  scale_x_continuous(limits=c(151,181),breaks=seq(151,181,5))+
  scale_y_continuous(limits=c(-0.15,1.75),breaks=seq(-0.15,1.75,0.15))+
  theme(legend.position="top",legend.key =element_blank())+
  scale_linetype_manual(values=lt, breaks=lt.names)

将此与设置lt 时的结果进行比较,如下所示:

lt <- setNames(c(4,3,2,1),lt.names)

【讨论】:

    【解决方案3】:

    看看this overview,你可以看到哪些数字对应哪些线型。因此,如果您想手动分配默认值,您可能必须使用:scale_linetype_manual(values=c(1,3,2,5))

    【讨论】:

    • 我不知道为什么,但这些数值看起来也不相似。
    猜你喜欢
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 2016-04-09
    • 2012-01-02
    • 2021-04-22
    • 2018-09-12
    • 1970-01-01
    相关资源
    最近更新 更多