R 在ggplot中绘制两组多条垂直线
我想使用以下R 在ggplot中绘制两组多条垂直线,r,ggplot2,R,Ggplot2,我想使用以下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") +
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
组分开。我该怎么做?非常感谢您的建议。这就是您想要的吗
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)
库(“dplyr”)
myline=data.frame(vv=c(25,28,52,53),
xx=c(“a1”、“a2”、“b1”、“b2”))
麦林%
突变(组=substr(xx,1,1))
种子集(100)
d=数据帧(y=c(rnorm(100,5,1),rnorm(100,2,4)),x=1:200)
ggplot(数据=d)+几何线(aes(x,y),color=“steelblue”)+
几何线(数据=myline,aes(xintercept=as.numeric(vv),col=group),大小=0.8)
要获得不同颜色的垂直线,请在调用geom\u vline
时使用vv
作为颜色。然后使用scale\u color\u manual
设置所选颜色还要注意的是,当设置
xintercept
美学的值时,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"))