0

I have some scatter plots look like this:

scatter plot

The X values are discrete. For every X there's some Y values. I want to plot the density distribution of Y for each X. Like this:

density plot

Sorry that I don't know how to describe it, so I draw it by hand. Each red line is a density curve of Y corresponding to an X value. The blue line is the trend line.

In my limited experience with R and ggplot2, I don't have any ideas how to do it. I am searching for a long time on Google but no use. Please help or try to give some ideas how to achieve this. Thanks a lot!

SATMOS
  • 11
  • 6
  • 2
    Check out the `ggridges` package for stacked density plots. You can then use `coord_flip()` to show the plots vertically instead of horizontally. – wurli May 06 '21 at 15:20
  • 1
    You might be looking for the [ggridges](https://wilkelab.org/ggridges/) package. – teunbrand May 06 '21 at 15:20
  • It will be extremely challenging to answer your question without a representative sample of your data. Provide it as text by clicking the [edit] button under your post. Either copy the output of the `dput(data)` command and paste the output into your question (preferred) or paste the tab delimited data from a spreadsheet program. Please see [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more information. – Ian Campbell May 06 '21 at 15:20
  • [Here](https://stackoverflow.com/a/31796083/496488), [here](https://stackoverflow.com/a/41082095/496488) and [here](https://stackoverflow.com/a/61590744/496488) are a few examples of such plots. – eipi10 May 06 '21 at 15:37

0 Answers0