ggplot geom_vline 具有二进制变量和 x 轴日期和长数据格式
ggplot geom_vline with binary variable and x-axis dates and long data format
我正在尝试创建一个情节,其中我有一个对应于危机季度的 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
谢谢
有点乱,但是调用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))
如果我没听错,“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
我正在尝试创建一个情节,其中我有一个对应于危机季度的 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
谢谢
有点乱,但是调用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))
如果我没听错,“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