Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R GG绘制两个阴影区域geom_区域_R_Ggplot2_Area - Fatal编程技术网

R GG绘制两个阴影区域geom_区域

R GG绘制两个阴影区域geom_区域,r,ggplot2,area,R,Ggplot2,Area,到目前为止,我已经制作了这个图表: 我想更改的是变量计数的阴影区域(左y轴)。当计数等于或大于12.5时,该区域应为黑色阴影。否则,该区域应为灰色阴影 当前此图表的代码为: ggplot(df_shadded_graph, aes(x=Date, y=Count)) + geom_area(fill="black", alpha=.99) + geom_line(aes(y = Underpricing*100, colour = "Underpricing"), colour = "

到目前为止,我已经制作了这个图表:

我想更改的是变量计数的阴影区域(左y轴)。当计数等于或大于12.5时,该区域应为黑色阴影。否则,该区域应为灰色阴影

当前此图表的代码为:

ggplot(df_shadded_graph, aes(x=Date, y=Count)) +
  geom_area(fill="black", alpha=.99) +
  geom_line(aes(y = Underpricing*100, colour = "Underpricing"), colour = "grey", linetype="longdash") +
  scale_y_continuous(sec.axis = sec_axis(~. / 100, name = "Underpricing")) +
  geom_hline(yintercept = 12.5) +
  theme_minimal()
我尝试使用以下子集,但没有成功:

geom_area(data = subset(df_shadded_graph, zero_one == 1, fill="black", alpha=.99))
实现两种颜色着色的最佳方法是什么?这在geom_区域是否可行

以下代码是简化的数据集:

df_shadded_graph <- structure(list(Date = structure(c(1025481600, 1028160000, 1030838400, 
1033430400, 1036108800, 1038700800, 1041379200, 1044057600, 1046476800, 
1049155200, 1051747200, 1054425600, 1057017600, 1059696000, 1062374400, 
1064966400, 1067644800, 1070236800, 1072915200, 1075593600, 1078099200, 
1080777600, 1083369600, 1086048000, 1088640000, 1091318400), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Count = c(5.33333333333333, 2.33333333333333, 
3.66666666666667, 6.66666666666667, 8.66666666666667, 5.33333333333333, 
3.33333333333333, 1.66666666666667, 1.66666666666667, 1, 1.66666666666667, 
4, 5.66666666666667, 6.66666666666667, 8.33333333333333, 11.3333333333333, 
16.6666666666667, 14.3333333333333, 15.3333333333333, 13.3333333333333, 
15.6666666666667, 14.3333333333333, 18.3333333333333, 23.6666666666667, 
24.3333333333333, 20), Underpricing = c(0.0281112960367963, 0.00954323052149139, 
0.0129313986013986, 0.0296151983272675, 0.0542382142002834, 0.0413068155988848, 
0.0178396825396825, -0.00869082021936099, -0.00869082021936099, 
0.050384179780639, 0.0962599206349206, 0.1782458943856, 0.16362838849032, 
0.155155878713463, 0.110998297207745, 0.106699976872014, 0.113896570632104, 
0.162480658578345, 0.176346448849018, 0.157754181912185, 0.148675489256975, 
0.130041460117814, 0.136243778466198, 0.0743238088052681, 0.0478918997193753, 
0.0596987903377546), zero_one = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("Date", 
"Count", "Underpricing", "zero_one"), row.names = c(NA, -26L), class = c("tbl_df", 
"tbl", "data.frame"))

df_shadded_graph至少对于区域颜色,您可以执行以下操作:

df=df_shadded_graph
df$Flag="High"
df$Flag[df$Count<=12.5]="Low"
ggplot() +geom_area(aes(x=df$Date[df$Flag=="High"], y=df$Count[df$Flag=="High"]),fill="black") +
  geom_area(aes(x=df$Date[df$Flag=="Low"], y=df$Count[df$Flag=="Low"]),fill="grey")+xlab("Date")+ylab("Count")
df=df\u着色图
df$Flag=“高”

df$Flag[df$Count您可以使用
geom_bar
而不是
geom_area
来做您需要做的事情,并避免在绘图中出现间隙。请注意,您必须使用
y
而不是
weight

ggplot(df_shadded_graph, aes(x=Date, weight=Count)) +
  geom_bar(mapping = aes(fill = factor(zero_one))) +
  geom_line(aes(x = Date, y = Underpricing*100, colour = "Underpricing"), colour = "grey", linetype="longdash") +
  scale_y_continuous(sec.axis = sec_axis(~. / 100, name = "Underpricing")) +
  scale_fill_grey(start = 0.7, end = 0.2) +
  theme(legend.position = "none")
  geom_hline(yintercept = 12.5) +
  theme_minimal()
这将生成如下图:
我不确定这是否回答了您的问题:

df_shadded_graph$Color=""
for(i in 1:nrow(df_shadded_graph)){
  if (i==1) df_shadded_graph$Color[i]<-"grey"
  else if (df_shadded_graph$zero_one[i-1]+df_shadded_graph$zero_one[i]==0 | df_shadded_graph$zero_one[i-1]+df_shadded_graph$zero_one[i]==1) df_shadded_graph$Color[i]<-"grey"
  else df_shadded_graph$Color[i]<-"black"
}

ggplot(df_shadded_graph, aes(x=Date, y=Count)) +
  geom_area(aes(y = ifelse(Color == "grey", Count, 12.5)), fill = "lightgrey", alpha=.99) +
  geom_area(aes(y = ifelse(zero_one == 1, Count, 0)),fill="black", alpha=.99) +
  geom_line(aes(y = Underpricing*100, colour = "Underpricing"), colour = "darkgrey", linetype="longdash") +
  scale_y_continuous(sec.axis = sec_axis(~. / 100, name = "Underpricing")) +
  geom_hline(yintercept = 12.5) +
  theme_minimal()
df_shadded_图形$Color=“”
对于(i in 1:nrow(df_shadded_图)){
如果(i==1)df_着色图$Color[i]重复?