1

我有以下名为“finaldata”的数据框

 structure(list(Dates = structure(c(325213200, 325216800, 325220400, 
325224000, 325227600, 325231200, 325234800, 325238400, 325242000, 
325245600, 325249200, 325252800, 325256400, 325260000, 325263600, 
325267200), tzone = "UTC", class = c("POSIXct", "POSIXt")), Qobs = c(0.7679669, 
0.7679669, 0.7679669, 0.7679669, 0.7679669, 0.7679669, 0.7679669, 
0.7679669, 0.7679669, 0.7679669, 0.7679669, 0.7679669, 0.7679669, 
0.7679669, 0.7679669, 0.7679669), LB1 = c(6.856253e-14, 6.856253e-14, 
6.856253e-14, 6.856253e-14, 6.856253e-14, 6.856253e-14, 6.856253e-14, 
-8.273674e-07, -4.344798e-06, -7.625017e-06, -1.004544e-05, -1.155634e-05, 
1.842427e-10, 1.813103e-10, 1.74717e-10, 1.658628e-10), UB1 = c(36.84034, 
36.72503, 36.61008, 36.49549, 36.38126, 36.26738, 36.15387, 36.04071, 
35.9279, 35.81546, 35.70336, 35.59162, 35.48023, 35.36925, 35.25862, 
35.14833), LB2 = c(0.3167181, 0.3142281, 0.3117576, 0.3093065, 
0.3068748, 0.3044625, 0.3020694, 0.3076093, 0.3253525, 0.3389268, 
0.3457364, 0.3467479, 0.3533797, 0.3724299, 0.3848245, 0.3882089
), UB2 = c(35.31174, 35.17496, 35.03872, 34.903, 34.7678, 34.63314, 
34.49901, 34.36992, 34.24928, 34.12819, 34.00447, 33.878, 33.75473, 
33.63942, 33.53223, 33.5228)), class = "data.frame", row.names = c(NA, 
-16L), .Names = c("Dates", "Qobs", "LB1", "UB1", "LB2", "UB2"
))

我使用以下代码绘制了 1 条曲线和 2 条重叠的色带:

xlabel <- "Time"
ylabel <- "Q"

p <- ggplot(data=finaldata, aes(x = Dates)) +
geom_ribbon(aes(ymin=LB1,ymax=UB1, fill = 'Res1'), alpha = 0.5) +
geom_ribbon(aes(ymin=LB2,ymax=UB2, fill = 'Res2'), alpha = 0.5) +
geom_line(aes(y=Qobs, color=factor(1))) +   
xlab(" ") +
scale_y_continuous("Q", lim=c(0,175)) +
scale_fill_manual("", values = c('Res1' = 'mistyrose3', 'Res2' = 'mistyrose'),labels = c(label1,label2)) +
scale_colour_manual(breaks = 1, values = "lightskyblue4",  labels = "Obs") +
theme(legend.position=c(0.85,0.85), legend.title = element_blank(), legend.background = element_blank())

结果是下图,它在第一个功能区中显示了一个空的垂直带。

在此处输入图像描述

当 LB1 为负时,似乎没有绘制色带。我究竟做错了什么?

4

1 回答 1

0

正如@BenBolker 指出的那样,scale_y_continuous删除所有超出限制的数据,coord_cartesian将图形缩放到您设置的限制而不更改数据。有关该图的简化示例,请参见下面的代码。

ggplot(data=finaldata, aes(x = Dates)) +
geom_ribbon(aes(ymin=LB1,ymax=UB1, fill = 'Res1'), alpha = 0.5) +
geom_ribbon(aes(ymin=LB2,ymax=UB2, fill = 'Res2'), alpha = 0.5) +
geom_line(aes(y=Qobs))+ geom_line(aes(y=LB1))+
coord_cartesian(ylim=c(0,175)) 
于 2016-09-21T13:46:54.257 回答