Search code examples
rggplot2visualization

How to make a single plot from two dataframes with ggplot2


I have 2 datasets, called A and B. I want to compare the distribution of one common variable, called k, showing up in both dataset, but of different lengths (A contains 2000 values of k, while B has 1000, both have some N/A). So I would like to plot the distribution of A$k anf B$k in the same plot.

I have tried:

g1 <- ggplot(A, aes(x=A$k)) + geom_density()
g2 <- ggplot(B, aes(x=B$k)) + geom_density()
g <- g1 + g2

But then comes the error:

Don't know how to add o to a plot.

How can I overcome this problem?


Solution

  • Since we dont have any data it is hard to provide a specific solution that meets your scenario. But below is a general principle of what I think you trying to do.

    The trick is to put your data together and have another column that identifies group A and group B. This is then used in the aes() argument in ggplot. Bearing in mind that combining your data frames might not be as simple as what I have done since you might have some extra columns etc.

    # generating some pseudo data from a poisson distribution
    A <- data.frame(k = rpois(2000, 4))
    B <- data.frame(k = rpois(1000, 7))
    
    # Create identifier
    A$id <- "A"
    B$id <- "B"
    
    A_B <- rbind(A, B)
    
    g <- ggplot(data = A_B, aes(x = k, 
                                group = id, colour = id, fill = id)) + # fill/colour aes is not required
      geom_density(alpha = 0.6) # alpha for some special effects
    
    g