How to graph points connected by line for known values, and smooth curves for predicted values using ggplot with legend

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:

  1. the original freq values are not graphed (they don’t need to show up on the legend, but it’s fine if they do)
  2. the predictive curves have points connected by lines rather than just being lines

I hope this all makes sense! Please help!

Recognized by R Language Collective

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật