rggplot2scatter-plotscatter

Can you control the width and height of scatterplot points separately in R?


I would like to create a scatter plot with Age on the y-axis and Diameter on the x-axis. I would then plot values corresponding to different groups of people. I want the width of the scatter points to indicate the range of Diameter values seen in each group, and for the height of the point to indicate the range of Age values seen in each group. I have the desired width and height of each point listed as separate variables. The final graph would therefore look like a series of plotted ovals. For example, if group A has an age range of 25-35 and a diameter range of 4-6, then the oval would be 2 wide, 10 high, and would be centred on the point (5,30), i.e. the midpoint.

Is there any way of doing this in R/ggplot? What would the code look like? From past experience, I only know how to uniformly scale the circle up or down based on a single 3rd variable. I want to scale the height and width separately based on 2 other variables.

Thank you!


Solution

  • You could use ggforce::geom_ellipse. x0 and y0 specify the center, and a and b specify the width and height.

    library(ggforce)
    library(ggplot2)
    ggplot(mtcars) +
      geom_ellipse(aes(x0 = wt, y0 = mpg, a = disp/5000, b = cyl/10, angle = 0))
    

    enter image description here