Smoothing geom_path() with scale_y_reverse() and s

2020-04-20 10:51发布

问题:

My dataset:

reading,depth
31055,0.0
57635,0.5
34268,1.0
20926,1.5
13507,2.0
12944,2.5
13002,3.0
12892,3.5
12610,4.0
12158,4.5
12004,5.0

I'm plotting an instrument reading as a function of below-ground depth. Because of what I'm visualizing, I want the axes to have a non-standard configuration, like this:

ggplot(data=f0101, aes(x=reading, y=depth)) + 
    theme_classic() + 
    scale_y_reverse() + 
    scale_x_continuous(position="top") + 
    geom_path()

This is perfect, except that I'd like the line smoothed, via stat_smooth() or similar. But, calling a smooth appears to throw off geom_path().

ggplot(data=f0101, aes(x=reading, y=depth)) + 
    theme_classic() + 
    scale_y_reverse() + 
    scale_x_continuous(position="top") + 
    geom_path() + 
    stat_smooth()

What can I do to get the smoothing effect of stat_smooth() while still plotting the data in an admittedly non-standard configuration?

Thank you!

回答1:

I may be wrong, but I think you probably want something like this:

dat %>%
  ggplot(aes(x = depth, y = reading)) +
  geom_path() +
  stat_smooth() +
  theme_classic() + 
  scale_x_reverse() +
  scale_y_continuous(position = "top") +
  coord_flip()

which produces

For this, you're running the smooth with depth on the x and reading on the y, then flipping the coordinates.



标签: r ggplot2