具有二进制变量和 x 轴日期和长数据格式的 ggplot geom_vline

Posted

技术标签:

【中文标题】具有二进制变量和 x 轴日期和长数据格式的 ggplot geom_vline【英文标题】:ggplot geom_vline with binary variable and x-axis dates and long data format 【发布时间】:2020-03-20 18:32:38 【问题描述】:

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

谢谢

【问题讨论】:

【参考方案1】:

有点乱,但调用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))

【讨论】:

很好的解决方案,我有 19 个国家,不可能在情节中引用所有国家,太混乱了。【参考方案2】:

如果我没记错的话,“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

【讨论】:

感谢您的出色解决方案。在第一段代码中,我可能需要一些东西来从头开始创建它。我的意思不是手动输入有关国家和危机日期的信息,而是从现有数据集开始。你有什么意见吗? 可以试一试.. 你是怎么从 df3 得到这个的? 如果你写 df3$date[df3$value == 1] 而不是 ggplot 中的日期,你会得到正确的日期。但问题是它们没有提到国家 @A_Bed,见上文,对吗?危机年与您在问题中提供的不同。

以上是关于具有二进制变量和 x 轴日期和长数据格式的 ggplot geom_vline的主要内容,如果未能解决你的问题,请参考以下文章

在Matplotlib x轴标签格式的日期时间

使用文件中的数据绘制日期和时间(x 轴)与值(y 轴)

使用文件中的数据绘制日期和时间(x 轴)与值(y 轴)

Powershell 中的 Excel 到 .csv 不断更改日期和长数字的格式

了解 nvd3 x 轴日期格式

时间轴上刻度子集的日期格式