2

I have a ggplot chart

q3 <- ggplot(y, aes(T,C))+  geom_line() 

and the kernel density

den <-density(y$C)      

How can I overlay a kernel density chart on the y axis?

Thanks for your help

Justin
  • 42,475
  • 9
  • 93
  • 111
adam.888
  • 7,686
  • 17
  • 70
  • 105
  • I don't know what you mean by "overlaying a kernel density chart on the y axis". Could you provide more explanation, or perhaps an example image to demonstrate what you mean? – joran Jul 03 '12 at 15:09
  • 1
    You can often get the "sideways" plotting by reversing the x and y roles, so in extra layer calls it would mean reversing the aes() arguments .... if I understand ggplot syntax correctly, which I often don't. – IRTFM Jul 03 '12 at 15:17
  • 1
    I'm guessing you want some fraction of the methods used in this earlier SO posting: http://stackoverflow.com/questions/8545035/scatterplot-with-marginal-histograms-in-ggplot2 – IRTFM Jul 03 '12 at 15:51

1 Answers1

2

Following up on DWin's idea, this might be the sort of thing you're looking for:

dat <- data.frame(x = 1:100,
                  y = 1:100,
                  z = rnorm(100))

ggplot(dat) + 
    geom_point(aes(x = y/100,y = x/100)) + 
    geom_density(aes(x = z)) + 
    coord_flip()

enter image description here

Note the reversal of the x and y aesthetics in geom_point.

joran
  • 169,992
  • 32
  • 429
  • 468