设置geom_vline的线型?

时间:2016-03-23 13:41:00

标签: r ggplot2

对于以下情节:

df.plot <-structure(list(color = structure(c(2L, 2L, 3L, 1L, 3L, 4L, 3L, 
    1L, 4L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 
    3L, 3L, 3L, 3L), .Label = c("54", "55", "61", "69"), class = "factor"), 
    date = structure(c(16687, 16687, 16687, 16687, 16687, 16687, 
    16688, 16688, 16688, 16689, 16689, 16690, 16693, 16693, 16693, 
    16694, 16694, 16695, 16695, 16695, 16695, 16696, 16696, 16696, 
    16696, 16696, 16696), class = "Date"), facet = c("A", 
     "A", "A", "A", "A", "B", 
     "B", "A", "B", "B", "B", "B", 
     "B", "B", "B", "B", "A", "B", 
     "A", "B", "A", "C", "B", "C", 
     "C", "B", "C")), class = "data.frame", row.names = c(NA, -27L), 
     .Names = c("color", "date", "facet"))

vlines <- data.frame(date = as.Date(c("2015-09-10", "2015-09-13")), 
          LType=(c("AA", "AB")))
ggplot(df.plot, aes(x=date, fill=color)) + 
  geom_dotplot(binwidth=1, stackgroups=TRUE, binpositions="all") +
  coord_fixed(ratio=1) + 
  ylim(0,7) +
  geom_vline(data=vlines, aes(xintercept = as.numeric(date), linetype=LType))  + 
  facet_grid(facet ~ .)

我想为&#34; AB&#34;制作线型。 be&#34; dotdash&#34; ,以及&#34; AA&#34; &#34; longdash&#34 ;.我该如何指定?

1 个答案:

答案 0 :(得分:9)

使用scale_linetype_manual

library("ggplot2")
g0 <- ggplot(df.plot, aes(x=date, fill=color)) + 
  geom_dotplot(binwidth=1, stackgroups=TRUE, binpositions="all") +
  coord_fixed(ratio=1) + 
  ylim(0,7) +
  geom_vline(data=vlines, aes(xintercept = as.numeric(date), linetype=LType))  + 
  facet_grid(facet ~ .)

来自?par

  

'lty'线型。线型可以指定为             整数(0 =空白,1 =实心(默认),2 =虚线,3 =虚线,             4 = dotdash,5 = longdash,6 = twodash)或作为角色之一             字符串'&#34;空白&#34;','&#34;固定&#34;','&#34;虚线&#34;','&#34;点缀&#34;',             '&#34; dotdash&#34;','&#34; longdash&#34;'或'&#34; twodash&#34;',其中'&#34;空白&#34;'             使用'隐形线'(即不绘制它们)。

所以

g0 + scale_linetype_manual(values=c(5,4))

或(可能更好!)

g0 + scale_linetype_manual(values=c("longdash","dotdash"))