我创建了一个刻面的箱形图。现在我需要在情节上添加线条。该行未显示下面的代码。请帮助我一些帮助!谢谢!
数据:
Year |variable |value
2001 |A |39.605
2001 |A |28.50759
2001 |A |24.8132
2002 |A |10.70765357
2002 |A |7.8676
2002 |A |16.05294712
2003 |A |19.7847
2003 |A |20.21635
2003 |A |29.15491667
2001 |B |50
2001 |B |78
2001 |B |90
2002 |B |35
2002 |B |62
2002 |B |82.5
2003 |B |49.5
2003 |B |60
2003 |B |84
代码:
pp <- ggplot(dta, aes(x=factor(Year),y=value)) +
geom_boxplot() +
facet_grid(variable~.,scales="free_y") +
theme_bw()
pp + geom_vline(xintercept = 2002) #The line didn't show.
pp + geom_vline(xintercept = as.numeric(2002)) #The line didn't show.
pp + geom_vline(xintercept = which(levels(dta$Year) =="2002")) #The line didn't show.
答案 0 :(得分:0)
知道了! pp + geom_vline(xintercept = 2)
答案 1 :(得分:0)
请尝试
pp + geom_vline(xintercept = which(levels(factor(dta$Year)) =="2002"))
您已指定aes(x=factor(Year),...
,因此您需要找到因子级别的整数。 levels(dta$Year)
会返回NULL
,因为Year
不是原始数据框dta
中的一个因素。因此,您需要在factor(dta$Year)
:
geom_vline()
levels(factor(dta$Year))
#[1] "2001" "2002" "2003"
请注意,即使您更改输入数据,这也会有效,而不是像your own answer中那样对特定因子级别进行硬编码。