我正在尝试创建一个曲线图,其中我有一个对应于危机季度的vline (危机变量是二进制1(危机)-0(无危机)。这段代码
geom_vline(xintercept = as.yearqtr(c("2016-01-
01","2017-01-01")), linetype=4)+ #I should have a line only
in the date where che crisis is 1 (different per each
country)当危机变量为1时,应该允许我有一条直线。
下面是一个有效的示例:
# Load Packages
library(ggplot2)
library(zoo)
date <- as.yearqtr(c("2015-01-01","2016-03-01","2017-04-06","2015-01-01","2016-03-01","2017-04-06","2015-01-01","2016-03-01","2017-04-06"))
variable <- c('var1','var1','var1','var2','var2','var2','crisis','crisis','crisis')
value <- c(12,15,18,120,155,175,0,0,1)
specification <- c(1,1,1,1,1,1,1,1,1)
country <- c("AT","AT","AT","AT","AT","AT","AT","AT","AT")
df1 <- data.frame(country, date, variable, specification, value)
View(df1)
date <- as.yearqtr(c("2015-01-01","2016-03-01","2017-04-06","2015-01-01","2016-03-01","2017-04-06","2015-01-01","2016-03-01","2017-04-06"))
variable <- c('var1','var1','var1','var2','var2','var2','crisis','crisis','crisis')
value <- c(15,17,221,150,135,155,0,1,0)
specification <- c(1,1,1,1,1,1,1,1,1)
country <- c("BE","BE","BE","BE","BE","BE","BE","BE","BE")
df2 <- data.frame(country, date, variable, specification, value)
View(df2)
df3 <- rbind(df1,df2)
View(df3)
ch_1 <- ggplot()+
geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var1" ,],
aes(x = date,
y = value,
#colour = specification ##in the actual code this is uncommented
))+
geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var2" ,],
aes(x = date,
y = value,
#colour = specification ##in the actual code this is uncommented
))+
#should change here ---
geom_vline(xintercept = as.yearqtr(c("2016-01-01","2017-01-01")), linetype=4)+ #I should have a line only in the date where che crisis is 1 (different per each country)
# ---------------------
facet_wrap(~country, scales = 'free_y')+
theme(axis.text.x = element_text(angle = 45, hjust = 1,face="bold",size=9))
ch_1谢谢
发布于 2019-11-25 21:19:27
如果我没记错,"2016-01-01“是AT的意思,"2017-01-01”是BE的意思。因此,您需要在geom_vline中指定该组,以便将其分配到正确的面板:
使用国家/地区创建危机年份的数据框架:
crisis_year=df3[df3$value == 1,c("country","date")]绘图,确保将以下代码传递给geom_vline:
ch_1 <- ggplot()+
geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var1" ,],
aes(x = date,
y = value
#colour = specification ##in the actual code this is uncommented
))+
geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var2" ,],
aes(x = date,
y = value
))+
geom_vline(data=crisis_year,aes(xintercept = date), linetype=4)+
facet_wrap(~country, scales = 'free_y')+
theme(axis.text.x = element_text(angle = 45, hjust = 1,face="bold",size=9))
ch_1

发布于 2019-11-25 21:18:33
它有点乱,但在调用geom_vline时必须过滤数据。尝试:
ggplot()+
geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var1" ,],
aes(x = date,
y = value,
#colour = specification ##in the actual code this is uncommented
))+
geom_line(data = df3[df3$specification == levels(factor(df3$specification))[1] & df3$variable == "var2" ,],
aes(x = date,
y = value,
#colour = specification ##in the actual code this is uncommented
)) +
geom_vline(data = df3[df3$variable == "crisis" &
df3$value == 1 & df3$country == "AT",], aes(xintercept = date), linetype=4) +
geom_vline(data = df3[df3$variable == "crisis" &
df3$value == 1 & df3$country == "BE",], aes(xintercept = date), linetype=4) +
facet_wrap(~country, scales = 'free_y')+
theme(axis.text.x = element_text(angle = 45, hjust = 1,face="bold",size=9))https://stackoverflow.com/questions/59032304
复制相似问题