I have 8 different equations for predictive curves, but included a shortened version of the csv I am working with here:
Nb_fires <- c(1,2,3,4,5,6,7,9,11,13,17,20,36)
freq <- c(10,3,4,4,6,1,4,1,4,1,1,1,1)
pc <- c(24.390244, 7.317073, 9.756098, 9.756098, 14.634146, 2.439024, 9.756098, 2.439024, 9.756098, 2.439024, 2.439024, 2.439024, 2.439024)
fit <- data.frame(Nb_fires, freq, pc)
freq_predict_aexp1 <- c(24.159103, 6.843879, 6.312791, 6.255808, 6.249186, 6.24841, 6.248319, 6.248307, 6.248307, 6.248307, 6.248307, 6.248307, 6.248307)
freq_predict_ageom <- c(14.962906, 4.740895, 4.44209, 4.740895, 5.145346, 5.557973, 5.951721, 6.658814, 7.261951, 7.777633, 8.609601, 9.105095, 10.755967)
df_fit_adj <- data.frame(Nb_fires, freq, freq_predict_aexp1, freq_predict_ageom)
modpalette <- c("darkred", '#CC79A7', '#BF33E9', '#0072B2', '#4939A7', '#D55E00', "#009E73", "#E39A00")
I have two different dataframes here because I started with the “fit” df, and expanded it so that my predictive equations would include all values between the minimum and maximum Nb_fires values. I then tried reducing the expanded df (df_fit_adj) back down to only the Nb_fires values in the fit df to reduce the chances of getting n/as in my graph.
What I would like is for my known freq values to be plotted as black points connected by a thin black line, and then for my predictive curves (freq_predict_aexp1 and freq_predict_ageom) to be plotted as smooth lines without points. I would also like for all of these variables to show up in a legend on the graph.
In my first graph attempt, I made a point graph of my freq data and connected these points with lines. I then added the lines for each predictive curve separately. The issues I had here were that the original data format wouldn’t let me produce a legend that contains the colours for the different curves. As you can see, I was using annotate to make labels for each curve, but I don’t want to be messing around individually adjusting the y,x coordinates of the labels for every curve (I have to make 21 graphs, so prefer to keep this as streamlined as possible).
df_fit_graph <- fit%>%
dplyr::inner_join(df_fit_adj, fit, by = "Nb_fires")
plot <- ggplot(df_fit_graph, aes(x=Nb_fires, y=freq))+
geom_point()+
scale_x_continuous(limits=c(0, max(df_fit_graph$Nb_fires*1.15)))+
geom_line()+
geom_line(aes(y=freq_predict_aexp1), colour = 'darkred', linewidth = 1.1)+
annotate("text", label = "adjusted geometric", x= 37.5, y =3,
size = 3, colour = 'darkred')+
geom_line(aes(y=freq_predict_ageom), colour = '#CC79A7', linewidth = 1.1)+
annotate("text", label = "adjusted geometric", x= 37.5, y =3,
size = 3, colour = '#CC79A7')
plot
Going through some suggestions on Stack Overflow, I tried adding the Nb_fires variable back into my dataframe and flipping my data so it was in tall rather than wide format like so:
df_fit_graph <- fit%>%
dplyr::inner_join(df_fit_adj, fit, by = "Nb_fires")%>%
pivot_longer(., cols = -c(Nb_fires, freq, pc), names_to = "curve_eqn")
Then I tried graphing it:
plot <- ggplot(df_fit_graph, aes(x=Nb_fires, y=value, colour = curve_eqn))+
geom_point()+
labs(title=paste("Curve_R_",AOI), legend = "Curve Equation")+
scale_x_continuous(limits=c(0, max(df_fit_graph$Nb_fires*1.15)))+
geom_line()+
scale_colour_manual(values = modpalette, labels=c("adjusted exponential +1", "adjusted geometric", "adjusted log A", "adjusted log B", "adjusted reciprocal", "harris", "mmf", "power")))
plot
While this code is much easier to follow, the issues are:
- the original freq values are not graphed (they don’t need to show up on the legend, but it’s fine if they do)
- the predictive curves have points connected by lines rather than just being lines
I hope this all makes sense! Please help!
Got it!
Modified some variables:
df_fit_graph <- fit%>%
dplyr::inner_join(df_fit_adj, fit, by = "Nb_fires")%>%
pivot_longer(., cols = -c(Nb_fires, freq, pc), names_to = "curve_eqn")%>%
dplyr::rename(obs_freq = freq)%>%
dplyr::rename(freq = value)
Then graphed:
plot <- ggplot(df_fit_graph, aes(x=Nb_fires, y=freq, colour = curve_eqn))+
labs(title=paste("Curve_R_",AOI))+
scale_x_continuous(limits=c(0, max(df_fit_graph$Nb_fires*1.15)))+
geom_line(linewidth = 1.1)+
scale_colour_manual(values = modpalette, labels=c("adjusted exponential +1", "adjusted geometric"), name = "Curve Equation") +
geom_point(aes(y=obs_freq),colour = "black")+
geom_line(aes(y=obs_freq), colour="black")
plot