[英]Draw two groups of multiple vertical lines in ggplot
我想使用以下r
函數繪制ggplot中垂直線的a1, a2
和b1, b2
這兩個組。
myline = data.frame(vv = c(a1 = 25, a2 = 28, b1 = 52, b2 = 53))
set.seed(100)
d = data.frame(y = c(rnorm(100,5,1), rnorm(100, 2,4)), x = 1:200)
ggplot(data = d) + geom_line(aes(x, y), color = "steelblue") +
geom_vline(data = myline, aes(xintercept=as.numeric(vv)), col= 'red', size = 0.8)
我試圖將a
和b
組分成不同的顏色。 我怎樣才能做到這一點? 非常感謝你的建議。
要使用不同顏色的垂直線,請使用vv
作為geom_vline
調用中的geom_vline
。 然后使用scale_color_manual
設置選擇的顏色。
另請注意,在設置xintercept
美學的值時不需要as.numeric(vv)
, str(myline)
將顯示vv
已經是數字。
ggplot(data = d, aes(x, y)) +
geom_line(color = "steelblue") +
geom_vline(data = myline,
aes(xintercept = vv, color = factor(vv)),
size = 0.4) +
scale_color_manual(values = c("coral", "coral4", "orange", "orange4"))
這就是你追求的嗎?
library("dplyr")
myline = data.frame(vv = c(25, 28, 52, 53),
xx = c("a1", "a2", "b1", "b2"))
myline <- as_tibble(myline) %>%
mutate(group = substr(xx,1,1))
set.seed(100)
d = data.frame(y = c(rnorm(100,5,1), rnorm(100, 2,4)), x = 1:200)
ggplot(data = d) + geom_line(aes(x, y), color = "steelblue") +
geom_vline(data = myline, aes(xintercept=as.numeric(vv), col=group), size = 0.8)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.