阴影geom_line和x轴之间的区域 [英] Shading an area between geom_line and the x-axis
问题描述
我有两个图表显示供给和需求,还有一个图表是我从供给中减去需求以显示不对称结果。我想遮蔽x轴和不对称负面部分之间的区域,以显示赤字的程度。
我目前使用以下代码:
plot.asymmetry< ; - ggplot(data = df.overview.month,
aes(x = Date.Time,y = Asymmetry))+
geom_area(data = subset(df.overview.month,Asymmetry <0 ),
aes(x = Date.Time,y =不对称))
然而 - 正如可以预料的那样 - 这不会影响geom_line和x轴之间的区域,而只会影响非对称数据的负值,这是完全不同的情况,如结果图所示:
/编辑:一些示例数据:
time.initial< - as.POSIXct(2010-12-31 23:00:00,tz =GMT)
Date.Time< -vector()
for(i in 1:24){
Date.Time [i] < - time.initial + i * 3600
}
Demand< -vector()
for(i in 0:23){
Demand [i + 1] < - 155 + 20 * sin((pi / 12)* i - (pi / 2))+ 10 * sin((pi / 4380)* i +(pi / 2))
}
供应< -vector()
for(i in 0:23){
Supply [i + 1] <-165 + 5 * sin((pi / 4380)* i-(pi / 2))+ rnorm(1,mean = 0,sd = 0.20 * data_frame(Date.Time,Demand,Supply,Asymmetry = Supply-Demand)
$ c * 165)
}
df.overview.month $ c>
这是怎么样的灵感。现在,只需要在非对称性等于零的情况下添加其他数据点(如@baptiste建议的那样)。当非对称性大于零时,我创建了一个新的列 NA
,这样就不会在那里绘制geom_ribbon。只是对数据进行子集化并不会导致所需的情节。
df.overview.month $ Assym_ribbon = ifelse(df.overview。月$不对称> 0,
NA,
df.overview.month $不对称)
ggplot(aes(x = Date.Time,y =不对称),
data = df.overview.month)+
geom_line()+
geom_ribbon(aes(ymin = 0,ymax = Assym_ribbon),
data =,fill =red)
关于您构建示例的方式的一些附加说明。最重要的是R是矢量化的。例如:
set.seed(1)
供应< -vector()
for(i在0:23){
Supply [i + 1] < - 165 +
5 * sin((pi / 4380)* i -
(pi / 2))+
rnorm(1,mean = 0,sd = 0.20 * 165)
}
相当于:
set.seed(1)
i = 0:23
Supply_vec < -
(n = 165 + 5 * sin((pi / 4380)* i -
(pi / 2))+
rnorm(length(i),mean = 0,sd = 0.20 * 165)
> all.equal(Supply_vec,Supply)
[1] TRUE
在这种情况下,在代码中是适度的,但在其他(更现实的)设置使用向量化将节省你几十行代码。
I have two plots displaying supply and demand, and one plot in which I have subtracted the demand from the supply to show the resulting asymmetry. I would like to shade the area between the x-axis and the negative part of the asymmetry, to show the extent of the deficit.
I currently use the following code:
plot.asymmetry <- ggplot(data=df.overview.month,
aes(x=Date.Time, y=Asymmetry)) +
geom_area(data=subset(df.overview.month, Asymmetry < 0),
aes(x=Date.Time, y=Asymmetry))
However - as could be expected - this does not shade the area between geom_line and the x-axis, but only between negative values of the asymmetry data, which is something else entirely, as shown in the resulting graph:
Is there any way to overcome this problem?
/Edit: some example data:
time.initial <- as.POSIXct("2010-12-31 23:00:00", tz="GMT")
Date.Time<-vector()
for(i in 1:24) {
Date.Time[i] <- time.initial + i*3600
}
Demand<-vector()
for(i in 0:23) {
Demand[i+1] <- 155 + 20*sin((pi/12)*i - (pi/2)) + 10*sin((pi/4380)*i + (pi/2))
}
Supply<-vector()
for(i in 0:23) {
Supply[i+1] <- 165 + 5*sin((pi/4380)*i - (pi/2)) + rnorm(1, mean=0, sd=0.20*165)
}
df.overview.month <- data.frame(Date.Time, Demand, Supply, Asymmetry=Supply-Demand)
What about this as inspiration. Now you only need to add additional data points where the asymmetry is equal to zero (like @baptiste suggested). I create a new column which is NA
when the asymmetry is above zero, in this way no geom_ribbon will be drawn there. Just subsetting the data will not lead to the required plot.
df.overview.month$Assym_ribbon = ifelse(df.overview.month$Asymmetry > 0,
NA,
df.overview.month$Asymmetry)
ggplot(aes(x = Date.Time, y = Asymmetry),
data = df.overview.month) +
geom_line() +
geom_ribbon(aes(ymin = 0, ymax = Assym_ribbon),
data = , fill = "red")
Some additional notes about the way you constructed your example. The most important one is that R is vectorized. For example:
set.seed(1)
Supply<-vector()
for(i in 0:23) {
Supply[i+1] <- 165 +
5*sin((pi/4380)*i -
(pi/2)) +
rnorm(1, mean=0, sd=0.20*165)
}
is equivalent to:
set.seed(1)
i = 0:23
Supply_vec <- 165 + 5*sin((pi/4380)*i -
(pi/2)) +
rnorm(length(i), mean=0, sd=0.20*165)
> all.equal(Supply_vec, Supply)
[1] TRUE
In this case the reduction in code is modest, but in other (more realistic) settings using vectorization will save you dozens of lines of code.
这篇关于阴影geom_line和x轴之间的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!