I have a facet graph of survival over time and I want to underlay monthly precipitation onto those graphs. The problem I am running into is my survival dataset has 8 points and my precipitation dataset has 19 but only 8 points of the precipitation data (those that correspond to the survival dataset) are being graphed.
This is what my prcp data should look like:
Graph code:
pd<-position_dodge(0.2)
merged_data_date %>%
ggplot(aes( x=date, y=mean, ymin=min, ymax=max, color = plot))+
geom_line(aes(x=date, y=monthprcp*80, color = "blue"))+
geom_line(show.legend = TRUE,
size=.75,
position =pd)+
# geom_line(y=monthprcp, color ="lightblue")+
# geom_line(data=mean[!is.na(mean$combo),])+
geom_hline(yintercept=0, color="#333333", size =0.005)+
geom_point(size =2, position =pd)+
geom_errorbar(width = 0.06, position =pd)+
facet_grid(site~oak,
switch ="y",
scales="free",
labeller = as_labeller(c("Cortana" = "CT", "JJT" = "JT",
"Live" = "Live", "Red" = "Red", "Shin"= "Shin")))+
scale_x_date(date_breaks="4 month", date_labels = "%b %Y")+
# scale_y_continuous (limits =c(0, 24), breaks = seq(0,24, by = 4))+
scale_y_continuous(name = "Mean survival", sec.axis =sec_axis (trans=~./80, name = "Precipitation"))+
scale_color_manual(name =NULL,
breaks =c("C", "CS", "O", "W", "ligthblue"),
values=c( "#C16622FF", "#225EA8","#FECC5C", "#006D2C", "lightblue"),
labels =c ("Cage + No shade", "Cage + Shade", "No cage + No shade", "Wooded", "Precipitation"))+
labs (x="Time",
y="Mean seedling survival per subplot",
# title="Seedling survival over time",
title=NULL,
# subtitle = "Seedling survival on the Cortana and JJT site. Seedlings were counted between fall
# 2019 to fall 2020.",
size = 16)+
# facet_wrap(site~oak, ncol=3, drop = T)+
theme_minimal()+
theme (
legend.justification = "center",
legend.position = "top",
strip.placement = "outside",
# axis.text.x=element_text(angle=45, vjust=0.5),
plot.title = element_text (size = 16),
plot.title.position = "plot",
axis.line.x = element_line(),
axis.line.y = element_line(),
axis.title.y = element_text(size =10.5, vjust = 1),
axis.title.x = element_text(size =10.5, vjust = 1),
#strip.background = element_blank(),
strip.text.y = element_text(size =9.5),
strip.text.x= element_text(size =9.5, margin = margin (0,0,5,0)),
panel.spacing.x = unit(0.5,"lines"),
# panel.grid=element_blank())
#modifies just the vertical graphs (space between top and bottom)
panel.margin.y=unit (0.4, "lines"),
panel.grid.minor.y = element_line(color = "lightgrey"))
Sample of my dataset
merged_data_date<-structure(
list(
oak = c(
"Live",
"Red",
"Shin",
"Red",
"Live",
"Shin",
"Red",
"Shin",
"Red",
"Shin",
"Shin",
"Shin",
"Shin",
"Live",
NA,
"Shin",
"Red",
"Red",
"Red",
"Red",
"Red",
"Red",
"Live",
"Red",
"Red",
"Shin",
"Live",
"Red",
"Live",
"Live",
"Live",
"Live",
"Shin",
"Red",
"Red",
"Red",
"Shin",
"Live",
"Shin",
"Live",
"Live",
"Shin",
NA,
NA,
"Shin",
"Live",
"Red",
"Red",
"Red",
NA
),
plot = c(
"CS",
"C",
"C",
"O",
"O",
"C",
"O",
"W",
"O",
"C",
"CS",
"C",
"CS",
"W",
NA,
"O",
"W",
"CS",
"O",
"CS",
"CS",
"O",
"C",
"W",
"O",
"O",
"W",
"C",
"O",
"C",
"C",
"O",
"W",
"O",
"O",
"CS",
"O",
"C",
"CS",
"CS",
"O",
"CS",
NA,
NA,
"C",
"O",
"W",
"W",
"C",
NA
),
date = structure(
c(
18475,
18048,
18109,
18506,
18048,
18078,
18170,
18109,
18048,
18170,
18475,
18506,
18048,
18109,
18017,
18506,
18078,
18048,
18475,
17956,
18506,
18109,
18170,
18078,
18506,
18506,
18078,
18475,
18170,
18475,
18048,
18506,
18078,
17956,
18170,
18475,
18078,
18078,
18078,
18048,
18048,
18109,
18322,
18353,
17956,
17956,
18475,
17956,
18048,
18414
),
class = "Date"
),
site = c(
"Cortana",
"JJT",
"Cortana",
"JJT",
"JJT",
"JJT",
"JJT",
"JJT",
"Cortana",
"Cortana",
"Cortana",
"Cortana",
"JJT",
"Cortana",
NA,
"Cortana",
"JJT",
"JJT",
"Cortana",
"JJT",
"JJT",
"JJT",
"JJT",
"Cortana",
"Cortana",
"JJT",
"Cortana",
"JJT",
"JJT",
"Cortana",
"Cortana",
"JJT",
"JJT",
"Cortana",
"Cortana",
"Cortana",
"JJT",
"JJT",
"JJT",
"JJT",
"Cortana",
"JJT",
NA,
NA,
"JJT",
"JJT",
"JJT",
"Cortana",
"Cortana",
NA
),
mean = c(
8.6,
17.2,
10.8,
0,
8,
14.2,
0,
6,
15.2,
10.4,
3.6,
2.8,
18.8,
4.2,
NA,
3.6,
16.8,
22.4,
0,
14.2,
18.6,
17.4,
0,
9.6,
0,
0,
4.8,
0,
0,
0.4,
6.8,
0,
6.2,
7.8,
15.6,
7,
15.6,
10.6,
19,
9.8,
7,
19.2,
NA,
NA,
2.4,
1,
5.6,
5.6,
16.4,
NA
),
sd = c(
1.69115345252878,
1.59373774505092,
1.82756668824971,
0,
0.447213595499958,
1.2,
0,
3.09838667696593,
1.46287388383278,
2.22710574513201,
0.92736184954957,
1.35646599662505,
1.39283882771841,
1.24096736459909,
NA,
1.6,
1.98494332412792,
1.20830459735946,
0,
2.24499443206436,
2.11187120819429,
0.6,
0,
3.85486705347928,
0,
0,
1.31909059582729,
0,
0,
0.4,
0.374165738677394,
0,
3.18433666561813,
1.15758369027902,
1.4,
1.92353840616713,
0.509901951359279,
0.748331477354788,
1.09544511501033,
1.49666295470958,
1.30384048104053,
1.01980390271856,
NA,
NA,
0.678232998312527,
0.632455532033676,
2.92574776766556,
1.69115345252878,
1.80554700852678,
NA
),
max = c(
10.2911534525288,
18.7937377450509,
12.6275666882497,
0,
8.44721359549996,
15.4,
0,
9.09838667696593,
16.6628738838328,
12.627105745132,
4.52736184954957,
4.15646599662505,
20.1928388277184,
5.44096736459909,
NA,
5.2,
18.7849433241279,
23.6083045973595,
0,
16.4449944320644,
20.7118712081943,
18,
0,
13.4548670534793,
0,
0,
6.11909059582729,
0,
0,
0.8,
7.17416573867739,
0,
9.38433666561813,
8.95758369027902,
17,
8.92353840616713,
16.1099019513593,
11.3483314773548,
20.0954451150103,
11.2966629547096,
8.30384048104053,
20.2198039027186,
NA,
NA,
3.07823299831253,
1.63245553203368,
8.52574776766556,
7.29115345252878,
18.2055470085268,
NA
),
min = c(
6.90884654747122,
15.6062622549491,
8.97243331175029,
0,
7.55278640450004,
13,
0,
2.90161332303407,
13.7371261161672,
8.17289425486799,
2.67263815045043,
1.44353400337495,
17.4071611722816,
2.95903263540091,
NA,
2,
14.8150566758721,
21.1916954026405,
0,
11.9550055679356,
16.4881287918057,
16.8,
0,
5.74513294652072,
0,
0,
3.48090940417271,
0,
0,
5.55111512312578e-17,
6.42583426132261,
0,
3.01566333438187,
6.64241630972098,
14.2,
5.07646159383287,
15.0900980486407,
9.85166852264521,
17.9045548849897,
8.30333704529042,
5.69615951895947,
18.1801960972814,
NA,
NA,
1.72176700168747,
0.367544467966324,
2.67425223233444,
3.90884654747122,
14.5944529914732,
NA
),
monthprcp = c(
0.0496551724137931,
0.1475,
0.0316071428571429,
0.221166666666667,
0.1475,
0.0404838709677419,
0.140645161290323,
0.0316071428571429,
0.1475,
0.140645161290323,
0.0496551724137931,
0.221166666666667,
0.1475,
0.0316071428571429,
0.207692307692308,
0.221166666666667,
0.0404838709677419,
0.1475,
0.0496551724137931,
0.0323214285714286,
0.221166666666667,
0.0316071428571429,
0.140645161290323,
0.0404838709677419,
0.221166666666667,
0.221166666666667,
0.0404838709677419,
0.0496551724137931,
0.140645161290323,
0.0496551724137931,
0.1475,
0.221166666666667,
0.0404838709677419,
0.0323214285714286,
0.140645161290323,
0.0496551724137931,
0.0404838709677419,
0.0404838709677419,
0.0404838709677419,
0.1475,
0.1475,
0.0316071428571429,
0.0920967741935484,
0.0646551724137931,
0.0323214285714286,
0.0323214285714286,
0.0496551724137931,
0.0323214285714286,
0.1475,
0.069
)
),
row.names = c(NA,-50L),
class = c("tbl_df",
"tbl", "data.frame")
)
I tried to filter the NA's which gives me a graph without NA rows or NA columns but also removes my full date dataset
merged_data<-full_join(final_survprcp1, finalprcp, by = "date") %>%
mutate(date=as.yearmon(date)) %>%
mutate(date=as.Date(date)) %>%
filter(!is.na(site)) %>%
filter(!is.na(plot)) %>%
filter(!is.na(oak)) %>%
mutate(monthprcp_tr=monthprcp*80)
I also want my site names to be strip placed on the outside, not sure why that code isn't working. I also tried to change the prcp line in the graph to blue and yet the line color didn't change.
Hope this makes sense.
If I'm understanding your question, I think it would probably be easier to have the two different data sets remain distinct. If you do that, then you can plot the line for the precipitation data independently of the survival data points.
First, I'll just split the two datasets apart.
library(dplyr)
library(ggplot2)
library(tidyr)
precip_data <- merged_data_date %>%
group_by(date) %>%
summarise(monthprcp = first(monthprcp))
surv_data <- merged_data_date %>%
dplyr::select(-monthprcp) %>%
na.omit()
For this to work as you intend, you'll need to replicate the precipitation data for all levels of site
and oak
, which you could do as follows:
precip_data <- expand_grid(oak = c("Live", "Red", "Shin"),
site = c("Cortana", "JJT"),
date = precip_data$date) %>%
left_join(precip_data)
Then you can make the plot. Notice below that when plotting with the precipitation data, I use inherit.aes=FALSE
, which will allow the attributes in precip_data
to be rendered without regard to whether they correspond with the aesthetic elements and data in the call to ggplot()
.
pd<-position_dodge(0.2)
ggplot(data = surv_data,
aes( x=date, y=mean, ymin=min, ymax=max, color = plot)) +
geom_line(data = precip_data, aes(x=date, y=monthprcp*80), color="black", inherit.aes = FALSE) +
geom_line(show.legend = TRUE,
linewidth=.75,
position =pd)+
geom_hline(yintercept=0, color="#333333", linewidth =0.005)+
geom_point(size =2, position =pd)+
facet_grid(site~oak,
switch ="y",
scales="free",
labeller = as_labeller(c("Cortana" = "CT", "JJT" = "JT",
"Live" = "Live", "Red" = "Red", "Shin"= "Shin"))) +
scale_x_date(date_breaks="4 month", date_labels = "%b %Y")+
# scale_y_continuous (limits =c(0, 24), breaks = seq(0,24, by = 4))+
scale_y_continuous(name = "Mean survival", sec.axis =sec_axis (trans=~./80, name = "Precipitation"))+
scale_color_manual(name =NULL,
breaks =c("C", "CS", "O", "W", "ligthblue"),
values=c( "#C16622FF", "#225EA8","#FECC5C", "#006D2C", "lightblue"),
labels =c ("Cage + No shade", "Cage + Shade", "No cage + No shade", "Wooded", "Precipitation"))+
labs (x="Time",
y="Mean seedling survival per subplot",
# title="Seedling survival over time",
title=NULL,
# subtitle = "Seedling survival on the Cortana and JJT site. Seedlings were counted between fall
# 2019 to fall 2020.",
size = 16)+
# facet_wrap(site~oak, ncol=3, drop = T)+
theme_minimal()+
theme (
legend.justification = "center",
legend.position = "top",
strip.placement = "outside",
# axis.text.x=element_text(angle=45, vjust=0.5),
plot.title = element_text (size = 16),
plot.title.position = "plot",
axis.line.x = element_line(),
axis.line.y = element_line(),
axis.title.y = element_text(size =10.5, vjust = 1),
axis.title.x = element_text(size =10.5, vjust = 1),
#strip.background = element_blank(),
strip.text.y = element_text(size =9.5),
strip.text.x= element_text(size =9.5, margin = margin (0,0,5,0)),
panel.spacing.x = unit(0.5,"lines"),
# panel.grid=element_blank())
#modifies just the vertical graphs (space between top and bottom)
panel.margin.y=unit (0.4, "lines"),
panel.grid.minor.y = element_line(color = "lightgrey"))
Created on 2024-01-16 with reprex v2.0.2
merged_data_date<-structure(list(oak = c("Live", "Red", "Shin", "Red", "Live",
"Shin", "Red", "Shin", "Red", "Shin", "Shin", "Shin", "Shin",
"Live", NA, "Shin", "Red", "Red", "Red", "Red", "Red", "Red",
"Live", "Red", "Red", "Shin", "Live", "Red", "Live", "Live",
"Live", "Live", "Shin", "Red", "Red", "Red", "Shin", "Live",
"Shin", "Live", "Live", "Shin", NA, NA, "Shin", "Live", "Red",
"Red", "Red", NA), plot = c("CS", "C", "C", "O", "O", "C", "O",
"W", "O", "C", "CS", "C", "CS", "W", NA, "O", "W", "CS", "O",
"CS", "CS", "O", "C", "W", "O", "O", "W", "C", "O", "C", "C",
"O", "W", "O", "O", "CS", "O", "C", "CS", "CS", "O", "CS", NA,
NA, "C", "O", "W", "W", "C", NA), date = structure(c(18475, 18048,
18109, 18506, 18048, 18078, 18170, 18109, 18048, 18170, 18475,
18506, 18048, 18109, 18017, 18506, 18078, 18048, 18475, 17956,
18506, 18109, 18170, 18078, 18506, 18506, 18078, 18475, 18170,
18475, 18048, 18506, 18078, 17956, 18170, 18475, 18078, 18078,
18078, 18048, 18048, 18109, 18322, 18353, 17956, 17956, 18475,
17956, 18048, 18414), class = "Date"), site = c("Cortana", "JJT",
"Cortana", "JJT", "JJT", "JJT", "JJT", "JJT", "Cortana", "Cortana",
"Cortana", "Cortana", "JJT", "Cortana", NA, "Cortana", "JJT",
"JJT", "Cortana", "JJT", "JJT", "JJT", "JJT", "Cortana", "Cortana",
"JJT", "Cortana", "JJT", "JJT", "Cortana", "Cortana", "JJT",
"JJT", "Cortana", "Cortana", "Cortana", "JJT", "JJT", "JJT",
"JJT", "Cortana", "JJT", NA, NA, "JJT", "JJT", "JJT", "Cortana",
"Cortana", NA), mean = c(8.6, 17.2, 10.8, 0, 8, 14.2, 0, 6, 15.2,
10.4, 3.6, 2.8, 18.8, 4.2, NA, 3.6, 16.8, 22.4, 0, 14.2, 18.6,
17.4, 0, 9.6, 0, 0, 4.8, 0, 0, 0.4, 6.8, 0, 6.2, 7.8, 15.6, 7,
15.6, 10.6, 19, 9.8, 7, 19.2, NA, NA, 2.4, 1, 5.6, 5.6, 16.4,
NA), sd = c(1.69115345252878, 1.59373774505092, 1.82756668824971,
0, 0.447213595499958, 1.2, 0, 3.09838667696593, 1.46287388383278,
2.22710574513201, 0.92736184954957, 1.35646599662505, 1.39283882771841,
1.24096736459909, NA, 1.6, 1.98494332412792, 1.20830459735946,
0, 2.24499443206436, 2.11187120819429, 0.6, 0, 3.85486705347928,
0, 0, 1.31909059582729, 0, 0, 0.4, 0.374165738677394, 0, 3.18433666561813,
1.15758369027902, 1.4, 1.92353840616713, 0.509901951359279, 0.748331477354788,
1.09544511501033, 1.49666295470958, 1.30384048104053, 1.01980390271856,
NA, NA, 0.678232998312527, 0.632455532033676, 2.92574776766556,
1.69115345252878, 1.80554700852678, NA), max = c(10.2911534525288,
18.7937377450509, 12.6275666882497, 0, 8.44721359549996, 15.4,
0, 9.09838667696593, 16.6628738838328, 12.627105745132, 4.52736184954957,
4.15646599662505, 20.1928388277184, 5.44096736459909, NA, 5.2,
18.7849433241279, 23.6083045973595, 0, 16.4449944320644, 20.7118712081943,
18, 0, 13.4548670534793, 0, 0, 6.11909059582729, 0, 0, 0.8, 7.17416573867739,
0, 9.38433666561813, 8.95758369027902, 17, 8.92353840616713,
16.1099019513593, 11.3483314773548, 20.0954451150103, 11.2966629547096,
8.30384048104053, 20.2198039027186, NA, NA, 3.07823299831253,
1.63245553203368, 8.52574776766556, 7.29115345252878, 18.2055470085268,
NA), min = c(6.90884654747122, 15.6062622549491, 8.97243331175029,
0, 7.55278640450004, 13, 0, 2.90161332303407, 13.7371261161672,
8.17289425486799, 2.67263815045043, 1.44353400337495, 17.4071611722816,
2.95903263540091, NA, 2, 14.8150566758721, 21.1916954026405,
0, 11.9550055679356, 16.4881287918057, 16.8, 0, 5.74513294652072,
0, 0, 3.48090940417271, 0, 0, 5.55111512312578e-17, 6.42583426132261,
0, 3.01566333438187, 6.64241630972098, 14.2, 5.07646159383287,
15.0900980486407, 9.85166852264521, 17.9045548849897, 8.30333704529042,
5.69615951895947, 18.1801960972814, NA, NA, 1.72176700168747,
0.367544467966324, 2.67425223233444, 3.90884654747122, 14.5944529914732,
NA), monthprcp = c(0.0496551724137931, 0.1475, 0.0316071428571429,
0.221166666666667, 0.1475, 0.0404838709677419, 0.140645161290323,
0.0316071428571429, 0.1475, 0.140645161290323, 0.0496551724137931,
0.221166666666667, 0.1475, 0.0316071428571429, 0.207692307692308,
0.221166666666667, 0.0404838709677419, 0.1475, 0.0496551724137931,
0.0323214285714286, 0.221166666666667, 0.0316071428571429, 0.140645161290323,
0.0404838709677419, 0.221166666666667, 0.221166666666667, 0.0404838709677419,
0.0496551724137931, 0.140645161290323, 0.0496551724137931, 0.1475,
0.221166666666667, 0.0404838709677419, 0.0323214285714286, 0.140645161290323,
0.0496551724137931, 0.0404838709677419, 0.0404838709677419, 0.0404838709677419,
0.1475, 0.1475, 0.0316071428571429, 0.0920967741935484, 0.0646551724137931,
0.0323214285714286, 0.0323214285714286, 0.0496551724137931, 0.0323214285714286,
0.1475, 0.069)), row.names = c(NA, -50L), class = c("tbl_df",
"tbl", "data.frame"))