0

I want to plot a figure with 2 lines (inner bay and outer bay), but I have 2 columns of data for inner bay. I need to set the date limits to switch columns on a certain date. In the data below, I want to plot IB.y from 2015-09-08 to 2015-09-23 and IB.x from 2015-09-24 to 2015-10-07. And then I want to plot all of OB.

The code below plots both IB.x and IB.y for the entire date range rather than split at 2015-09-24 as desired:

AllDailyMean = ggplot(AllMean, aes(x=Date)) + geom_line(aes(y=IB.x, 
color = "Inner Bay"), size = 0.5) + geom_ribbon(aes(ymin=IBMin.x, 
ymax = IBMax.x), fill = "coral2", alpha = 0.2, linetype = 3) +
scale_x_date(limits = as.Date(c("2015-09-08", "2015-09-23"))) + 
geom_line(aes(y=IB.y, color = "Inner Bay"), size = 0.5) +
geom_line(aes(y=OB, color = "Outer Bay"), size = 0.5) +
geom_ribbon(aes(ymin=IBMin.y, ymax=IBMax.y), fill = "coral2", alpha 
= 0.2, linetype = 3) + geom_ribbon(aes(ymin=OBMin, ymax=OBMax), 
fill = "skyblue4", alpha = 0.2, linetype = 3) +
scale_x_date(labels = date_format("%b '%y"), date_breaks = "2 
months") + labs(y = expression(atop("Mean Daily Temp", 
paste(("°C"%+-%"Max/Min")))), x = "Date")

structure(list(Date = structure(c(16686, 16687, 16688, 16689, 
16690, 16691, 16692, 16693, 16694, 16695, 16696, 16697, 16698, 
16699, 16700, 16701, 16702, 16703, 16704, 16705, 16706, 16707, 
16708, 16709, 16710, 16711, 16712, 16713, 16714, 16715, 16716
), class = "Date"), IB.x = c(29.7916666666667, 30.0166666666667, 
30.075, 30.0875, 29.3666666666667, 29.2291666666667, 28.8875, 
28.6826086956522, 28.6041666666667, 28.7125, 28.7416666666667, 
28.5166666666667, 28.525, 28.525, 28.5166666666667 
28.3916666666667, 28.3, 28.0875, 27.9541666666667, 27.475, 
27.1458333333333, 26.9166666666667, 26.85, 26.9625, 
26.4041666666667, 25.95, 25.7416666666667, 25.85, 
25.6875, 25.7, 25.7958333333333), IBMax.x = c(30.1, 30.3, 30.4, 
30.6, 29.7, 29.4, 29.2, 29, 28.9, 29, 29.1, 28.9, 28.8, 28.7, 
28.7, 28.5, 28.5, 28.4, 28.1, 27.9, 27.6, 27.1, 27.1, 27.3, 27.1, 
26.3, 26.2, 26.1, 25.9, 26.2, 26.1), IBMin.x = c(29.1, 29.7, 
29.8, 29.7, 29, 29, 28.4, 28.2, 28.4, 28.5, 28.6, 27.9, 28.2, 
28.3, 28.2, 28.2, 28.1, 27.7, 27.8, 27, 26.8, 26.7, 26.5, 26.7, 
25.7, 25.5, 25.4, 25.4, 25.2, 25.2, 25.5), IB.y = c(NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 27.671, 
27.6027395833333, 27.25271875, 26.7719895833333, 26.3682604166667, 
26.3313229166667, 26.4141875, 26.2628020833333, 26.14065625, 
26.1491041666667, 26.2293541666667, 25.7827604166667, 25.44615625, 
25.6583854166667, 26.0718645833333), IBMax.y = c(NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 28.177, 28.151, 
27.632, 27.187, 26.917, 26.843, 27.237, 26.77, 26.573, 26.622, 
26.671, 26.059, 25.913, 26.279, 26.328), IBMin.y = c(NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 27.41, 26.917, 
26.77, 26.23, 25.766, 25.717, 25.644, 25.352, 25.255, 25.231, 
25.498, 25.523, 25.084, 25.036, 25.766), OB = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 27.794625, 27.5463125, 
27.0850208333333, 26.8009375, 26.7057083333333, 26.65728125, 
26.535375, 26.46721875, 26.4802604166667, 26.7571145833333, 
26.3706145833333, 26.0067395833333, 25.9274166666667, 
25.8764895833333, 25.9058333333333), OBMax = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 28.147, 27.998, 27.358, 
27.014, 27.136, 27.014, 27.186, 26.965, 27.038, 27.308, 26.646, 
26.231, 26.256, 26.329, 26.207), OBMin = c(NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 27.628, 27.21, 26.793, 
26.524, 26.304, 26.28, 26.158, 26.158, 25.939, 26.329, 25.939, 
25.768, 25.671, 25.574, 25.501)), row.names = 70:100, class = 
"data.frame")
  • What I'd like to do is plot the IB and OB data (temp) across a range of dates. There are 2 IB columns (x and y). I want to plot IB.x from 9/8-9/23 and IB.y from 9/23-10/7. So ideally I'd have 2 lines on the plot, one for IB and one for OB. –  Aug 19 '19 at 20:18
  • 1
    You should probably reshape the data into a long format so you can assign a variable to color, instead of hard-coding colors into repeat geom layers. That's the way ggplot is meant to be used; lots of SO posts on this, including [this one](https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph) – camille Aug 19 '19 at 20:55

1 Answers1

0

One option is to use a simple ifelse within your y aesthetic. I've removed a bunch of your code as it's superfluous to the problem at hand (it's helpful to provide a minimal reproducible example, removing any irrelevant details).

ggplot(df, aes(x=Date)) +
    geom_line(aes(y=ifelse(Date <= "2015-09-23", IB.x, NA), color = "Inner Bay"), size = 0.5) + 
    geom_line(aes(y=ifelse(Date > "2015-09-23", IB.x, NA), color = "Outer Bay"), size = 0.5) +
    labs(y = expression(atop("Mean Daily Temp", paste(("°C"%+-%"Max/Min")))), x = "Date")

output

heds1
  • 3,203
  • 2
  • 17
  • 32