rggplot2

How to plot a curved line between points


I need to plot a dotted line which is curved between points. Here is an image of what I want to do made using MicroSoft Excel:

[example line plot with curved dotted line made in MicroSoft Excel1

library(ggplot2)
Sample <- c(-1, -3, -5, -7, -9, -11, -13, -15, -17, -19, -21, -23, -25, -27, 
            -29, -31, -33, -35, -37)
N <- c(0.14, 0.16, 0.09, 0.09, 0.11, 0.09, 0.10, 0.09, 0.11, 0.11, 0.09, 0.09,
       0.05, 0.09, 0.09, 0.10, 0.11, 0.11, 0.11)
tab <- data.frame(N, Sample)
ggplot(tab,  aes(x=N,  y=Sample,  c(0, 0.16)), pch=17) + 
  geom_path(linetype=3, color='#2980B9',  size = 0.1) +
  geom_point(color='#2980B9',  size = 2) +
  scale_x_continuous(position = "top",  limits=c(0, 0.16)) +
  scale_y_continuous(limits=c(-40, 0))

and here is the plot I obtain:

[example line plot with straight dotted line made using R2


Solution

  • One way get smoothed lines instead of straight lines would be to flip x and y in your aesthetics, then apply geom_smooth instead of geom_path and then flip the coordinates through coord_flip:

    ggplot(tab, aes(x=Sample, y=N, c(0,0.16)),pch=17) + 
    coord_flip() + 
    geom_point(color='#2980B9', size = 2) + 
    geom_smooth(method = "loess", se = FALSE, 
              span = 0.25, linetype=3,color='#2980B9', size = 0.1)
    

    enter image description here