Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to add a density curve and mean line to geom_histogram?

$
0
0

I am currently writing my thesis and I have one thing I could not solve by searching the web. I have some datasets and I have to compare the results before and after, and I would like to visualise it by comparing two sets of histograms (both sets contain 5 plots). The think it is a really easy problem for you guys but a little help is still needed.

I tried a few thing but I keep ending up by messing up the ggplot. I know that I probably have to add two small lines of code but I am really struggling to find them.

I have got the following code that works for the time being.

df <- as.data.frame(clust1_mat[,1:5])

p1 <- ggplot(gather(df), aes(value)) + 
        geom_histogram(bins = 10) +
        facet_wrap(~key, scales = 'free_x', nrow= 1) +
        xlab("Average results students in CLuster 1")

df <- as.data.frame(cijfers_list[,1:5])

p2 <- ggplot(gather(df), aes(value)) +  
        geom_histogram(bins = 10) + 
        facet_wrap(~key, scales = 'free_x', nrow=1) +
        xlab("Average results students before clustering")

grid.arrange(p1, p2, nrow=2)

I would like to add a shaded density curve and a red vertical line on the mean of each histogram.

Thanks!


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>