Search code examples
rggplot2flip

Drawing flipped Normal distribution in R without using coord_flip()


Good day

Without using coord_flip(), Is there a way to draw normal distribution flipped by exchanging position x and y in aes()? I' ve tried as below.

df3 <- data.frame(x=seq(-6,6,b=0.1),y=sapply(seq(-6,6,b=0.1),function(x) dnorm(x))) 
ggplot(df3,aes(y,x))+ geom_line()  # x,y position exchanged

Solution

  • I'm not sure what's wrong with coord_flip, but you can avoid it with geom_path. geom_path connects the points in the order they appear in the data, rather than in order of the magnitude of the x-value. So you just need to make sure the data are ordered by y-axis value (which they already are here).

    ggplot(df3, aes(y,x)) + 
      geom_path() +
      theme_classic()
    

    enter image description here