R 在绘图图表中创建阴影区域
我的数据帧称为cpu:R 在绘图图表中创建阴影区域,r,ggplot2,R,Ggplot2,我的数据帧称为cpu: structure(list(Date = structure(c(15229, 15236, 15243, 15250, 15257, 15264, 15271, 15278, 15285, 15292, 15299, 15306, 15313, 15320, 15327, 15334, NA, 15341, 15348, 15355, 15362, 15369, 15376, 15383, 15390, 15397, 15404, 15411, 15418, 1
structure(list(Date = structure(c(15229, 15236, 15243, 15250,
15257, 15264, 15271, 15278, 15285, 15292, 15299, 15306, 15313,
15320, 15327, 15334, NA, 15341, 15348, 15355, 15362, 15369, 15376,
15383, 15390, 15397, 15404, 15411, 15418, 15425), class = "Date"),
Pc = c(13.28, 8.96, 11.68, 9.76, 9.6, 9.92, 8.48, 10.56,
12, 14.08, 10.72, 12, 11.04, 11.68, 11.52, 12.64, 5.6, 11.84,
11.84, 11.36, 11.84, 13.28, 12.16, 12.64, 12.48, 13.28, 11.36,
11.2, 11.04, 12.16)), .Names = c("Date", "Pc"), row.names = c(4974L,
5009L, 5044L, 5068L, 5203L, 5238L, 5093L, 5103L, 5138L, 5173L,
5446L, 5481L, 5516L, 5551L, 5586L, 5298L, 5328L, 5333L, 5368L,
5403L, 5596L, 5631L, 5666L, 5821L, 5856L, 5691L, 5726L, 5756L,
5791L, 6066L), class = "data.frame")
我需要在图表中使用geom_!rect创建阴影区域。我有以下资料:
ggplot(cpu, aes(Date, Pc, group=1)) +
geom_point() +
geom_smooth(method="loess", size=1, colour="blue") +
theme_bw() +
scale_x_date(breaks = "3 weeks",minor_breaks="1 weeks",labels=date_format("%m/%d/%y")) +
theme(axis.text.x = element_text(angle=60, hjust=1)) +
geom_rect(data=cpu,aes(xmin=as.Date(c("2011-10-10")), xmax=as.Date(c("2013-08-11")), ymin=0, ymax=Inf))
阴影区域与点重叠,我看不到点。我如何才能做到这一点,使我也能看到几何矩形区域后面的几何点?正如我在最初的评论中所说,这对我很有用:
ggplot(cpu, aes(Date, Pc, group=1)) +
geom_rect(data=cpu,xmin=as.numeric(as.Date(c("2011-10-10"))),
xmax=as.numeric(as.Date(c("2013-08-11"))), ymin=0, ymax=Inf,fill = "blue",alpha = 0.01) +
geom_point() +
geom_smooth(method="loess", size=1, colour="blue") +
theme_bw() +
scale_x_date(breaks = "3 weeks",minor_breaks="1 weeks",labels=date_format("%m/%d/%y")) +
theme(axis.text.x = element_text(angle=60, hjust=1))
我认为
geom_rect
层可能会出现其他问题,因为我不需要那么小的alpha级别,所以我担心会出现意外的过涂。但我现在得赶去开会。如果有人发现问题,请随意评论/编辑。为什么要将用作.numeric
?设置美学时,不要将其放入aes
。你可能还想先移动那一层,这样它就可以在所有东西下面画出来,当然也可以设置一个相当低的alpha值。@joran,我有这个:ggplot(cpu)+geom(data=cpu,aes(xmin=as.Date(c(“2011-10-10”)),xmax=as.Date(c(“2011-08-11”)),ymin=0,ymax=Inf),fill=“orange”)+geom(data=cpu,aes)(Date,Cpu)+geom_smooth(method=“leash”,size=1,color=“blue”)+theme_bw()+scale_x_Date(breaks=“3周”,minor_breaks=“1周”,labels=日期格式(“%m/%d/%y”)+theme(axis.text.x=元素文本(angle=60,hjust=1)),不工作您没有从geom\u rect
中删除aes
,并且有几个打字错误。我也不认为您需要删除as.numeric
。