Search code examples
rlinesmoothing

How to smooth graph lines in R and add a line of best fit


ggline(Titration.Aug.9,x="Dilution",y="X..bound",color="Sample")+
  scale_x_continuous(trans="log10",labels=trans_format("log10",math_format(10^x)),minor_breaks=10^(seq(0,7, by=0.25)))+
  theme_minimal()+labs(x="Dilution",y="% Bound")+
  scale_color_brewer(type="Sample",palette="Set1")

I have this line of code and it generates the graph I want. The only things I need to add to it now are smoother lines for the plot. I though maybe using geom_smooth(aes(linetype=X..bound)) but that just gives me a shaded line of best fit and an error message of "a continuous variable cannot be mapped to livetype" I suspect maybe because I made the x-axis a log scale but left the y scale a linear scale. How do I smoothen out my graph lines? Also how do I add a line of best fit that is sigmoidal to the graph? I don't need any other background color with it like in some commands I have tried; I just need a plane line of best fit that is able to be labeled on the side of the graph in the legend. Any help is appreciated!! Thank you in advance :)

Graph data

structure(list(Dilution = c(300L, 900L, 2700L, 8100L, 24300L, 
72900L, 218700L, 300L, 900L, 2700L, 8100L, 24300L, 72900L, 218700L, 
300L, 900L, 2700L, 8100L, 24300L, 72900L, 218700L, 300L, 900L, 
2700L, 8100L, 24300L, 72900L, 218700L, 300L, 900L, 2700L, 8100L, 
24300L, 72900L, 218700L, 300L, 900L, 2700L, 8100L, 24300L, 72900L, 
218700L, 300L, 900L, 2700L, 8100L, 24300L, 72900L, 218700L, 300L, 
900L, 2700L, 8100L, 24300L, 72900L, 218700L, 300L, 900L, 2700L, 
8100L, 24300L, 72900L, 218700L), X..bound = c(52.74, 40.31, 30.63, 
18.89, 7.57, 0.8, 0.01, 20.23, 11.29, 7.55, 3.24, 0.54, 0.12, 
0.03, 53.27, 46.82, 38.17, 26.77, 11.59, 2.23, 0.07, 69.25, 63.55, 
56.34, 40.95, 19.35, 2.4, 0.05, 75.8, 68.21, 62.82, 40.33, 11.73, 
0.82, 0.04, 85.75, 82.82, 74.29, 46.63, 9.36, 0.24, 0.05, 71.65, 
66.54, 56.63, 33.96, 6.33, 0.19, 0.03, 85.43, 86.49, 75.73, 51.62, 
15.16, 1.05, 0.01, 92.44, 90.13, 85.92, 72.06, 30.08, 3.15, 0.12
), Sample = c("1mer 0DA", "1mer 0DA", "1mer 0DA", "1mer 0DA", 
"1mer 0DA", "1mer 0DA", "1mer 0DA", "1mer 2DA", "1mer 2DA", "1mer 2DA", 
"1mer 2DA", "1mer 2DA", "1mer 2DA", "1mer 2DA", "1mer 3DA", "1mer 3DA", 
"1mer 3DA", "1mer 3DA", "1mer 3DA", "1mer 3DA", "1mer 3DA", "1mer 4DA", 
"1mer 4DA", "1mer 4DA", "1mer 4DA", "1mer 4DA", "1mer 4DA", "1mer 4DA", 
"5mer 0DA", "5mer 0DA", "5mer 0DA", "5mer 0DA", "5mer 0DA", "5mer 0DA", 
"5mer 0DA", "5mer 2DA", "5mer 2DA", "5mer 2DA", "5mer 2DA", "5mer 2DA", 
"5mer 2DA", "5mer 2DA", "5mer 4DA", "5mer 4DA", "5mer 4DA", "5mer 4DA", 
"5mer 4DA", "5mer 4DA", "5mer 4DA", "5mer 2DA GDG", "5mer 2DA GDG", 
"5mer 2DA GDG", "5mer 2DA GDG", "5mer 2DA GDG", "5mer 2DA GDG", 
"5mer 2DA GDG", "5mer 2DA GDGDG", "5mer 2DA GDGDG", "5mer 2DA GDGDG", 
"5mer 2DA GDGDG", "5mer 2DA GDGDG", "5mer 2DA GDGDG", "5mer 2DA GDGDG"
)), class = "data.frame", row.names = c(NA, -63L))

Graph image


Solution

  • If you can do without the fitted line in the legend, here is a way but with packages ggplot2 and scales, not with package ggpubr.

    suppressPackageStartupMessages({
      library(ggplot2)
      library(scales)
    })
    
    ggplot(Titration.Aug.9, aes(x = Dilution, y = `X..bound`)) +
      geom_line(aes(color = Sample)) +
      geom_point() +
      geom_smooth(formula = y ~ x, method = "loess", se = FALSE) +
      scale_x_continuous(
        trans = "log10",
        breaks = trans_breaks("log10", function(x) 10^x),
        labels = trans_format("log10", math_format(10^.x)),
        minor_breaks = 10^(seq(0, 7, by = 0.25))) +
      scale_color_brewer(type="Sample",palette="Set1") +
      labs(x="Dilution",y="% Bound") +
      theme_minimal()
    

    Created on 2022-09-01 by the reprex package (v2.0.1)