ggplot2中曲线下方的阴影区域

时间:2019-06-13 15:10:42

标签: r ggplot2 scatter-plot

我将直接解决我的问题。到目前为止,我已经使用以下代码块成功获得了以下图表:

test.dat %>%
  ggplot(aes(Day, Time2)) + 
  geom_line(aes(Day, Sunrise2, group=1)) +
  geom_line(aes(Day, Sunset2, group=1)) +
  geom_point() + 
  facet_grid(Mooring ~ Month) + 
  scale_x_discrete(breaks = c("05", "10", "15", "20", "25", "30"), 
                   labels=c("5", "10", "15", "20", "25", "30")) +
  scale_y_continuous(labels = c("00:00", "06:00", "12:00", "18:00", "23:59"))

Plot obtained

现在,下一步是有问题的。我需要实现以下目标:我需要对可见的下部曲线下方下的区域以及最高曲线上方的区域进行阴影处理(仅供参考)曲线代表一段时间内的日出和日落时间)。

我尝试了 geom_ribbon geom_area ,但到目前为止没有成功。可以肯定,我做错了什么,但我不知道是什么。我已经花了几个小时了,我有点绝望了。在您问之前,我已经研究了很多类似的话题,并运用了所讲的内容,但对我没有任何帮助...

希望您能提供帮助!
在下面,您将找到生成该图所需的数据样本。

非常感谢!

structure(list(Dates_Times = structure(c(1436821980, 1437706320, 
1436918940, 1438257840, 1436797020, 1439793660, 1441020000, 1433947260, 
1433944380, 1435737360, 1436997180, 1434830160, 1438705680, 1436845680, 
1433960220, 1434364080, 1436392380, 1438398960, 1436990460, 1439238300, 
1436405040, 1433954940, 1435109820, 1440025500, 1438718160, 1436780880, 
1438822320, 1437325020, 1436871600, 1439760240, 1434100860, 1436397180, 
1434304380, 1438706160, 1439437680, 1437565680, 1436055120, 1433885820, 
1439497500, 1434318000), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Mooring = c("M2", "M1", "M2", "M1", "M2", "M2", "M2", "M2", 
    "M2", "M1", "M2", "M1", "M1", "M1", "M2", "M1", "M2", "M1", 
    "M2", "M2", "M1", "M2", "M2", "M2", "M1", "M1", "M1", "M2", 
    "M1", "M1", "M2", "M2", "M2", "M1", "M1", "M1", "M1", "M2", 
    "M2", "M1"), Month = structure(c(3L, 3L, 3L, 3L, 3L, 4L, 
    4L, 2L, 2L, 3L, 3L, 2L, 4L, 3L, 2L, 2L, 3L, 4L, 3L, 4L, 3L, 
    2L, 2L, 4L, 4L, 3L, 4L, 3L, 3L, 4L, 2L, 3L, 2L, 4L, 4L, 3L, 
    3L, 2L, 4L, 2L), .Label = c("May", "June", "July", "August", 
    "September", "October", "November", "April", "December", 
    "March"), class = "factor"), Day = c("13", "24", "15", "30", 
    "13", "17", "31", "10", "10", "01", "15", "20", "04", "14", 
    "10", "15", "08", "01", "15", "10", "09", "10", "24", "19", 
    "04", "13", "06", "19", "14", "16", "12", "08", "14", "04", 
    "13", "22", "05", "09", "13", "14"), Time2 = c(0.884027777777778, 
    0.119444444444444, 0.00625, 0.502777777777778, 0.595138888888889, 
    0.278472222222222, 0.472222222222222, 0.611805555555556, 
    0.578472222222222, 0.330555555555556, 0.911805555555556, 
    0.830555555555556, 0.686111111111111, 0.158333333333333, 
    0.761805555555556, 0.436111111111111, 0.911805555555556, 
    0.136111111111111, 0.834027777777778, 0.850694444444444, 
    0.0583333333333333, 0.700694444444444, 0.0673611111111111, 
    0.961805555555556, 0.830555555555556, 0.408333333333333, 
    0.0361111111111111, 0.70625, 0.458333333333333, 0.891666666666667, 
    0.389583333333333, 0.967361111111111, 0.745138888888889, 
    0.691666666666667, 0.158333333333333, 0.491666666666667, 
    0.00833333333333333, 0.900694444444444, 0.850694444444444, 
    0.902777777777778), Sunrise2 = c(0.178703703703704, 0.187395833333333, 
    0.180613425925926, 0.194699074074074, 0.178703703703704, 
    0.220636574074074, 0.239085648148148, 0.167592592592593, 
    0.167592592592593, 0.166493055555556, 0.180613425925926, 
    0.162962962962963, 0.201064814814815, 0.176574074074074, 
    0.167592592592593, 0.163101851851852, 0.174409722222222, 
    0.197222222222222, 0.180613425925926, 0.211458333333333, 
    0.172048611111111, 0.167592592592593, 0.16693287037037, 0.223275462962963, 
    0.201064814814815, 0.175613425925926, 0.203668981481481, 
    0.184722222222222, 0.176574074074074, 0.216921296296296, 
    0.166979166666667, 0.174409722222222, 0.166527777777778, 
    0.201064814814815, 0.212916666666667, 0.185081018518519, 
    0.168993055555556, 0.167962962962963, 0.215381944444444, 
    0.163275462962963), Sunset2 = c(0.885914351851852, 0.874895833333333, 
    0.884270833333333, 0.86744212962963, 0.885914351851852, 0.841446759259259, 
    0.817835648148148, 0.889166666666667, 0.889166666666667, 
    0.892546296296296, 0.884270833333333, 0.893229166666667, 
    0.860590277777778, 0.884953703703704, 0.889166666666667, 
    0.891736111111111, 0.889363425925926, 0.864768518518519, 
    0.884270833333333, 0.852395833333333, 0.888680555555556, 
    0.889166666666667, 0.893541666666667, 0.838194444444444, 
    0.860590277777778, 0.885775462962963, 0.857696759259259, 
    0.88056712962963, 0.884953703703704, 0.842222222222222, 0.890289351851852, 
    0.889363425925926, 0.891261574074074, 0.860590277777778, 
    0.847025462962963, 0.877152777777778, 0.890949074074074, 
    0.888553240740741, 0.847789351851852, 0.89130787037037)), .Names = c("Dates_Times", 
"Mooring", "Month", "Day", "Time2", "Sunrise2", "Sunset2"), row.names = c(5044L, 
2151L, 5146L, 2515L, 5037L, 6092L, 6394L, 3895L, 3891L, 976L, 
5226L, 716L, 2591L, 1658L, 3906L, 631L, 4840L, 2549L, 5214L, 
5882L, 1365L, 3901L, 4444L, 6327L, 2607L, 1572L, 2701L, 5384L, 
1707L, 3229L, 4156L, 4848L, 4221L, 2592L, 3045L, 2124L, 1130L, 
3782L, 5911L, 545L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

看起来geom_ribbon可以做到...

test.dat %>%
  ggplot(aes(Day, Time2)) + 
  geom_ribbon(aes(Day, ymin=0, ymax=Sunrise2, group=1)) +
  geom_ribbon(aes(Day, ymin=Sunset2, ymax=1, group=1)) +
  #geom_line(aes(Day, Sunrise2, group=1)) +
  #geom_line(aes(Day, Sunset2, group=1)) +
  geom_point() + 
  facet_grid(Mooring ~ Month) + 
  scale_x_discrete(breaks = c("05", "10", "15", "20", "25", "30"), 
                   labels=c("5", "10", "15", "20", "25", "30")) +
  scale_y_continuous(labels = c("00:00", "06:00", "12:00", "18:00", "23:59"))

enter image description here