Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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
按x轴上的特定日期更改ggplot2-R中的颜色背景_R_Ggplot2 - Fatal编程技术网

按x轴上的特定日期更改ggplot2-R中的颜色背景

按x轴上的特定日期更改ggplot2-R中的颜色背景,r,ggplot2,R,Ggplot2,我想根据特定日期(在X轴上)更改散点图中背景的颜色。Ma日期为2017年6月23日至2017年12月6日。我想要6月23日至8月31日的背景为绿色,其余的为红色 我在这里尝试过这个脚本,但它不起作用(老实说,我以前从未使用过ggplot2)。 日期变量为POSIXct格式。这是我使用的脚本,带有R给我的错误: > ggplot() + geom_point() + geom_rect(aes(xmin = as.Date("2017-06-23"),xmax = as.Date("201

我想根据特定日期(在X轴上)更改散点图中背景的颜色。Ma日期为2017年6月23日至2017年12月6日。我想要6月23日至8月31日的背景为绿色,其余的为红色

我在这里尝试过这个脚本,但它不起作用(老实说,我以前从未使用过ggplot2)。 日期变量为POSIXct格式。这是我使用的脚本,带有R给我的错误:

> ggplot() + geom_point() + 
geom_rect(aes(xmin = as.Date("2017-06-23"),xmax = as.Date("2017-08-31"),ymin = 0, ymax = Inf),
          fill="green", 
          alpha = .2)+
geom_rect(aes(xmin = as.Date("2017-09-01"),xmax = as.Date("2017-12-06"),ymin = 0, ymax = Inf),
          fill="red", 
          alpha = .2)
Errore: Invalid input: time_trans works with objects of class POSIXct only
这个脚本有什么错误(或遗漏)

如果有用,这是我的数据集
数据的
str()

str(data)
'data.frame':   420 obs. of  2 variables:
 $ UTC.Date        : POSIXct, format: "2017-07-01" "2017-08-01" "2017-09-01" "2017-10-01" ...
 $ Mean.elevation  : num  1353 1098 905 747 1082 ...
如果您想尝试,这里有一些数据(我的数据集的前30行):


我不认为需要在年轴上添加颜色的部件有问题。下面的代码适用于我的系统

ggplot() + geom_point() + 
  geom_rect(aes(xmin = as.Date("2017-06-23"),xmax = as.Date("2017-08-31"),ymin = 0, ymax = Inf),
            fill="green", 
            alpha = .2)+
  geom_rect(aes(xmin = as.Date("2017-09-01"),xmax = as.Date("2017-12-06"),ymin = 0, ymax = Inf),
            fill="red", 
            alpha = .2)
我相信,绘制实际数据时会出现错误(您提供的代码中没有包含这些数据)
你能检查一下上面的答案是否对你有帮助吗?

几何体中的xmin,xmax输入必须与数据框中的输入类型相同,现在数据框中有POSIXct,几何体中有日期。一种解决方案是,向geom___rect提供POSIX格式的数据:

# your data frame based on first 5 values
df = data.frame(
UTC.Date = as.POSIXct(c("2017-07-01","2017-08-01","2017-09-01","2017-10-01","2017-11-01")),
Mean.elevation=c(1353,1098,905,747,1082))

RECT = data.frame(
       xmin=as.POSIXct(c("2017-06-23","2017-09-01")),
       xmax=as.POSIXct(c("2017-08-31","2017-12-06")),
       ymin=0,
       ymax=Inf,
       fill=c("green","red")
)

ggplot(df,aes(x=UTC.Date,y=Mean.elevation)) + geom_point()+
geom_rect(data=RECT,inherit.aes=FALSE,aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),
fill=RECT$fill,alpha=0.2)
或将原始数据帧时间转换为日期:

df$UTC.Date = as.Date(df$UTC.Date)
ggplot(df,aes(x=UTC.Date,y=Mean.elevation)) + geom_point() + 
geom_rect(aes(xmin = as.Date("2017-06-23"),xmax = as.Date("2017-08-31"),ymin = 0, ymax = Inf),
          fill="green", 
          alpha = .2)+
geom_rect(aes(xmin = as.Date("2017-09-01"),xmax = as.Date("2017-12-06"),ymin = 0, ymax = Inf),
          fill="red", 
          alpha = .2)
第一个解决方案给出如下内容:


您可能需要在绘图中添加一些数据,例如在
geom_点
内,例如
geom_点(aes(x=,y=)
通常最好提供示例数据,以便我们提供帮助。是的,它可以工作。谢谢还有一个问题:现在我如何在绿色和红色矩形前绘制我的平均高程值?嘿,没问题。。我可以看到是否还有其他方法,但现在,它只是降低了geom_Rective中调用的alpha值,我给你纠正了@Franza,你想让点更突出吗?是的,用你的解决方案,点似乎在红色和绿色框后面(应该是背景),因此,如果您想将其更改为蓝色(例如),则最好让它们保持领先,这样它们的颜色就不会被修改。如果您将alpha值更改为较低的值,效果会更好吗?
df$UTC.Date = as.Date(df$UTC.Date)
ggplot(df,aes(x=UTC.Date,y=Mean.elevation)) + geom_point() + 
geom_rect(aes(xmin = as.Date("2017-06-23"),xmax = as.Date("2017-08-31"),ymin = 0, ymax = Inf),
          fill="green", 
          alpha = .2)+
geom_rect(aes(xmin = as.Date("2017-09-01"),xmax = as.Date("2017-12-06"),ymin = 0, ymax = Inf),
          fill="red", 
          alpha = .2)