Search code examples
rggplot2dplyrscatter-plot

How to create a scatterplot of one variable comparing two groups?


I am trying to make a scatter/xy plot of two groups against each other for a single numerical variable using ggplot.

If I have a dataset that looks something like this:

#  condition   group     size
#1   apple_1   apple      200
#2   apple_2   apple      400
#3   apple_3   apple      600
#4    pear_1    pear      300
#5    pear_2    pear      400
#6    pear_3    pear      700

I want to make a scatterplot with the x-axis being the size of apples and y-axis being the size of pears, with apple_1 and pear_1 matching up to create a point, apple_2 with pear_2, and apple_3 with pear_3. This would basically result in a positively correlated scatterplot.

Is there a way to do this?


Solution

  • Bring apple and pear in separate columns and then it would be easier to plot.

    library(tidyverse)
    
    df %>%
      select(-condition) %>%
      pivot_wider(names_from = group, values_from = size, values_fn = list) %>%
      unnest(cols = everything()) %>%
      ggplot(aes(apple, pear)) + geom_point(size = 3)
    

    enter image description here