1

我正在尝试创建一个情节,其中我有一个与危机季度相对应的 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

谢谢

4

2 回答 2

1

如果我猜对了,“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-25T13:19:27.467 回答
0

有点乱,但调用时必须过滤数据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))
于 2019-11-25T13:18:32.700 回答