对于以下情节:
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 ~ .)
我想将“ AB”的线型设置为“ dotdash”,将“ AA”的线型设置为“ longdash”。我该如何指定?
最佳答案
使用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 =点破折号,5 =长破折号,6 =破折号)或作为字符之一
字符串“空白”,“固定”,“虚线”,“虚线”,
‘dotdash’’,‘longdash’’或‘twodash’’,其中‘blank’’
使用“看不见的线”(即不画线)。
所以
g0 + scale_linetype_manual(values=c(5,4))
或(可能更好!)
g0 + scale_linetype_manual(values=c("longdash","dotdash"))
如果要绘制单个vline,或者已经使用
scale_linetype_manual()
为geom_line()
指定线型,则使用linetype
作为参数更容易/更有意义:geom_vline(xintercept = <something>, linetype="dotdash")