是否可以根据二进制变量为ggplot2中的线图下的特定区域着色?

问题描述

我正在制作一个折线图,随着时间的推移,该图应该显示出某些问题的可能性趋势。 我的目标是突出显示线下的两个区域(2008-2010年和2015-2017年),而不突出线下的区域。我尝试使用geom_area(),但没有成功,因为定义突出显示组的变量是二进制的,即2008、2009、2010、2015、2016、2017分别编码为1和0。 / p>

正如您在图表上看到的那样,我现在只能创建彩色条,但要避免使用它们,因为它们看上去并不十分直观:

以下是生成该图的代码

# Plot mean theta for Economy of the EU
ggplot(means,aes(x=as.numeric(Year),y=`Economy of the EU`)) + 
  #geom_label(aes(label=Response,fill=Response),fontface = "bold",colour = "grey15") +
  geom_line(color = "grey15",size=1.1,alpha=0.6) +
  theme_tufte() +
  ylab("Mean Posterior Probability per Year") +
  xlab("Submission Year")+
  labs(title = "Model prediction for the topic 'Economy of the European Union'") + 
  theme_tufte() + 
  scale_x_continuous(breaks = seq(2000,2017,by = 1)) +
  theme(axis.text.x = element_text( 
    size=9,angle=45,hjust = 1,color = "grey15"),axis.title = element_text(size = 10,color = "grey15")) +
  theme(axis.text.y = element_text(size=9,color = "grey15")) +
  theme(axis.line = element_line(colour = 'grey15',size = 0.5)) +
  geom_rect(aes(xmin = 2008,xmax = 2010,ymin = 0,ymax = Inf),fill = "grey15",alpha = 0.005)+
  
  geom_rect(aes(xmin = 2015,xmax = 2017,alpha = 0.005)+
    geom_text(aes(label ="Global economic crisis"),y=0.017,x = 2008.6,angle = 90,hjust = 0,size = 4)+
  geom_text(aes(label = "European refugee crisis"),x = 2016,size = 4)+

  theme(axis.title.y= element_text(margin = margin(t = 0,r = 10,b = 0,l = 0)),axis.title.y.right = element_text(margin = margin(t = 0,r = 0,l = 10)
        ))   

数据结构如下:

Year      Economy of the EU    X        Y             Z  Response

1   2000    0.027733525 0.13407536  0.060126541 0.030515047 0
2   2001    0.009785368 0.09069352  0.045551195 0.036031561 0
3   2002    0.014343537 0.09829514  0.043757775 0.038343551 0
4   2003    0.028534253 0.09245992  0.100776232 0.041664380 0
5   2004    0.029453570 0.09755516  0.115880728 0.021007489 0
6   2005    0.019728747 0.08945665  0.070050617 0.080241471 0
7   2006    0.009816122 0.11857852  0.034646992 0.075113106 0
8   2007    0.015449088 0.09218845  0.048940357 0.015437598 0
9   2008    0.005629083 0.13172009  0.047490240 0.180294545 1
10  2009    0.016832789 0.11154000  0.030691807 0.090695709 1
11  2010    0.108598687 0.06898694  0.029462945 0.041829649 1
12  2011    0.082072062 0.09820219  0.150685004 0.065741991 0
13  2012    0.097722542 0.05812513  0.093679897 0.044311032 0
14  2013    0.087020362 0.07605144  0.114518255 0.062448333 0
15  2014    0.048143041 0.06783845  0.038510355 0.018508783 0
16  2015    0.028227368 0.06425895  0.056478657 0.011985935 1
17  2016    0.124701763 0.13341157  0.048756972 0.069039444 1
18  2017    0.118745565 0.09740252  0.002971395 0.006389235 1

所需的输出将是:

在此先感谢您的帮助!

解决方法

尝试使用geom_bar()的这种方法来模仿设置alpha因子的阴影行为。您可以使用aes()geom_bar()内部的二进制变量,以便根据数据帧中的最大值为钢筋指定上限。这里的代码:

#Code
ggplot(means,aes(x=as.numeric(Year),y=`Economy of the EU`)) + 
  #geom_label(aes(label=Response,fill=Response),fontface = "bold",colour = "grey15") +
  geom_line(color = "grey15",size=1.1,alpha=0.6) +
  theme_tufte() +
  geom_bar(stat='identity',aes(y=ifelse(Response==1,max(means$`Economy of the EU`),NA)),width = 1,fill='grey15',alpha=0.2)+
  ylab("Mean Posterior Probability per Year") +
  xlab("Submission Year")+
  labs(title = "Model prediction for the topic 'Economy of the European Union'") + 
  theme_tufte() + 
  scale_x_continuous(breaks = seq(2000,2017,by = 1)) +
  theme(axis.text.x = element_text( 
    size=9,angle=45,hjust = 1,color = "grey15"),axis.title = element_text(size = 10,color = "grey15")) +
  theme(axis.text.y = element_text(size=9,color = "grey15")) +
  theme(axis.line = element_line(colour = 'grey15',size = 0.5)) +
  geom_text(aes(label ="Global economic crisis"),y=0.017,x = 2008.6,angle = 90,hjust = 0,size = 4)+
  geom_text(aes(label = "European refugee crisis"),x = 2016,size = 4)+
  theme(axis.title.y= element_text(margin = margin(t = 0,r = 10,b = 0,l = 0)),axis.title.y.right = element_text(margin = margin(t = 0,r = 0,l = 10)))

输出:

enter image description here

使用了一些数据:

#Data
means <- structure(list(Year = 2000:2017,`Economy of the EU` = c(0.027733525,0.009785368,0.014343537,0.028534253,0.02945357,0.019728747,0.009816122,0.015449088,0.005629083,0.016832789,0.108598687,0.082072062,0.097722542,0.087020362,0.048143041,0.028227368,0.124701763,0.118745565),X = c(0.13407536,0.09069352,0.09829514,0.09245992,0.09755516,0.08945665,0.11857852,0.09218845,0.13172009,0.11154,0.06898694,0.09820219,0.05812513,0.07605144,0.06783845,0.06425895,0.13341157,0.09740252),Y = c(0.060126541,0.045551195,0.043757775,0.100776232,0.115880728,0.070050617,0.034646992,0.048940357,0.04749024,0.030691807,0.029462945,0.150685004,0.093679897,0.114518255,0.038510355,0.056478657,0.048756972,0.002971395),Z = c(0.030515047,0.036031561,0.038343551,0.04166438,0.021007489,0.080241471,0.075113106,0.015437598,0.180294545,0.090695709,0.041829649,0.065741991,0.044311032,0.062448333,0.018508783,0.011985935,0.069039444,0.006389235),Response = c(0L,0L,1L,1L)),row.names = c(NA,-18L),class = "data.frame")
,

如果我对您的理解正确,那么您只希望突出显示的区域位于该行下方。在这种情况下,您正在寻找geom_area,但需要绘制两个单独的geom_area区域,这些区域是通过对数据进行子集定义的:

library(ggplot2)
library(ggthemes)
library(dplyr)

ggplot(means,y=`Economy of the EU`)) + 
  geom_line(color = "grey15",size = 1.1,alpha = 0.6) +
  geom_area(data = means %>% filter(Year > 2007 & Year < 2012),alpha = 0.1) +
  geom_area(data = means %>% filter(Year > 2014),alpha = 0.1) +
  geom_text(aes(label ="Global economic crisis"),y = 0.017,size = 4,check_overlap = TRUE) +
  geom_text(aes(label = "European refugee crisis"),check_overlap = TRUE) +
  scale_x_continuous(breaks = seq(2000,by = 1)) +
  labs(x = "Submission Year",y = "Mean Posterior Probability per Year",title = "Model prediction for the topic 'Economy of the European Union'") + 
  theme_tufte() + 
  theme(axis.text.x        = element_text(size=9,angle = 45,axis.title         = element_text(size = 10,axis.text.y        = element_text(size = 9,axis.line          = element_line(colour = 'grey15',size = 0.5),axis.title.y       = element_text(margin = 
                                            margin(t = 0,axis.title.y.right = element_text(margin = 
                                            margin(t = 0,l = 10)))

enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...