首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >具有二进制变量和x轴日期以及长数据格式的ggplot geom_vline

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

Stack Overflow用户
提问于 2019-11-25 20:53:21
回答 2查看 231关注 0票数 1

我正在尝试创建一个曲线图,其中我有一个对应于危机季度的vline (危机变量是二进制1(危机)-0(无危机)。这段代码

代码语言:javascript
运行
复制
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时,应该允许我有一条直线。

下面是一个有效的示例:

代码语言:javascript
运行
复制
# 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

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-25 21:19:27

如果我没记错,"2016-01-01“是AT的意思,"2017-01-01”是BE的意思。因此,您需要在geom_vline中指定该组,以便将其分配到正确的面板:

使用国家/地区创建危机年份的数据框架:

代码语言:javascript
运行
复制
crisis_year=df3[df3$value == 1,c("country","date")]

绘图,确保将以下代码传递给geom_vline:

代码语言:javascript
运行
复制
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

票数 1
EN

Stack Overflow用户

发布于 2019-11-25 21:18:33

它有点乱,但在调用geom_vline时必须过滤数据。尝试:

代码语言:javascript
运行
复制
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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59032304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档