I am trying to create histogram using ggplot of two lists. At the moment I am using the base function plot. Please can someone explain how to using ggplot? I would like the y axis to show the density.
The easiest way to plot the two distributions is ggplot is to combine the two data sets. How to do this combination depends on just how your data are structured. If you explain more about your data, someone might provide more specific advice.
library(ggplot2)
DF1 <- data.frame(A = rep("Dat1", 20), Value = rnorm(20))
DF2 <- data.frame(A = rep("Dat2", 20), Value = rnorm(20, 0.5, 1))
AllDat <- rbind(DF1, DF2)
ggplot(AllDat, aes(x = Value, fill = A)) + geom_histogram(binwidth = 0.2, position = "dodge")
Other possibilities might be to mimic adding two histograms, as you did, or using frequency polygons instead. I'll use @FJCC's defintions of DF1 and DF2, to illustrate.
The first results in one histogram obscuring the other:
DF1 <- data.frame(A = rep("Dat1", 20), Value = rnorm(20))
DF2 <- data.frame(A = rep("Dat2", 20), Value = rnorm(20, 0.5, 1))
library(ggplot2)
ggplot() +
geom_histogram(aes(Value, y = ..density.., fill = 'DF1'), data = DF1) +
geom_histogram(aes(Value, y = ..density.., fill = 'DF2'), data = DF2)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
By changing the transparency of the bars, this might be improved, or you could add position = "dodge" as @FJCC does. Frequency polygons allow for a different kind of visibility:
ggplot() +
geom_freqpoly(aes(Value, y = ..density.., color = 'DF1'), data = DF1) +
geom_freqpoly(aes(Value, y = ..density.., color = 'DF2'), data = DF2)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
It may be easy to convert your data from a list to a data frame, in fact a data frame is basically a list, but we would have to know the structure of the list. Try running the commands
str(samples1)
str(samples_cp)
When you paste the result into you reply, please place three back ticks, ```, on the lines before and after the output so that it gets formatted as code.
will give you the data frame you want from that data set and you can run a similar command with the other list. You need to have the dplyr package, of course. You can then add a column to each data frame with a command like
This output is an mcmc.list from modelling using Bayesian techniques (well that's my guess since you haven't told us how it was produced). You will need special tools to process the output and visualize the results. Check out the {MCMCvis} and {coda} packages on CRAN which are designed to handle mcmc.list output.
HTH