geom_rect背景会引入缺失值,并且不会绘制彩色背景

时间:2019-02-23 01:25:23

标签: r ggplot2

我同时收集了三个不同区域的时间数据,我需要突出显示夜间数据。我想要一个背景灰色的条高亮显示夜间数据。这是我的数据

> dput(head(gra))
 structure(list(Day = c("Day", "Day", "Night", "Night", "Day", 
 "Day"), x = 1:6, Zones = c("Zone C", "Zone C", "Zone C", "Zone C", 
 "Zone C", "Zone C"), Values = c(4.648410153, 0.187158871, -14.39132531, 
 -16.29031506, -5.211906368, 0.372469736)), .Names = c("Day", 
  "x", "Zones", "Values"), row.names = c(NA, 6L), class = "data.frame")

这是我的代码

g <- ggplot(gra, aes(x = x, y = Values)) + 
  geom_line(aes(color = Zones), size = 0.5) + 
  geom_point() +
  xlim(0, 30) + 
  ylim(-50, 30) +
  scale_color_grey() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black")) +
  theme(axis.text.x=element_text(angle = -90, hjust = 0))

g + geom_rect(aes(xmin = as.integer(factor(Values, 
                                           levels = Values, 
                                           ordered = TRUE)) - .5,
                  xmax = as.integer(factor(Values, 
                                           levels = Values, 
                                           ordered = TRUE)) + .5,
                  ymin = -Inf, 
                  ymax = Inf, 
                  fill = Day, group = Day), 
                alpha = 0.2)
g

但不会突出显示夜间数据

0 个答案:

没有答案