如何在刻面图中设置geom_vline的线型?

时间:2015-03-30 16:39:25

标签: r ggplot2 facet-wrap

以下代码就像魅力一样:

foo = data.frame(x=c(1,2,3),
                 y=c(4,5,6),
                 lt=c('dotted', 'dashed', 'dotdash'))
ggplot(foo, aes(x,y)) + geom_point() +
  geom_vline(aes(xintercept=x, linetype=lt))

enter image description here

以下代码会删除linetype。为什么呢?

ggplot(foo, aes(x,y)) + geom_point() +
  geom_vline(xintercept=3, aes(linetype=lt)) +
  facet_wrap(~lt)

enter image description here

我在Rstudio中使用ggplot 0.9.3.1。

1 个答案:

答案 0 :(得分:2)

我可以确认你的问题,并且Sandy的评论为我修复了它,使用R 3.1.2中的ggplot2 1.0:

geom_vline(aes(xintercept = 3, linetype = lt))

但是我仍然认为这是geom_vline中的一个错误(可能只是文档)。我认为xintercept应该被列为必需的美学,默认值为0.值得注意的是,下面的代码“有效” - 这些行在默认的x位置为0时具有正确的散列:

ggplot(foo, aes(x,y)) + 
   geom_point() +
   geom_vline(aes(linetype=lt)) +
   facet_wrap(~lt)

所以问题似乎是当aes()之外提供所需的美学时,aes()内的参数会被忽略。这与其他功能不同。例如,

ggplot(foo) + geom_point(x=1, aes(y=y))

给出错误但

ggplot(foo) + geom_point(aes(x=1, y=y))

没有。就我而言,理想的行为是geom_vline()在这方面的行为与geom_point()相同。