3

I have the following code and would like to know if it is possible to use different shapes in the scale_size_binned command. In my actual data, it is a bit difficult to distinguish between the points so I would like to use different shapes for them. Any suggestions will be appreciated.

rct <- data.frame(

  Effect_Size_Study = rnorm(100),
  F_test_var_stat = runif(100),
  Outcome_Sample_Size = runif(100, min = 6, max = 10000)
)

ggplot(rct, aes(x = Effect_Size_Study, y = F_test_var_stat)) +
  geom_point(aes(size = Outcome_Sample_Size)) +
  scale_size_binned_area(
    limits = c(0, 10000),
    breaks = c(0, 100, 500, 1000, 5000, 10000),
  )
Abby
  • 59
  • 6

2 Answers2

2

You could use scale_shape_binned like this:

library(ggplot2)
rct <- data.frame(
  
  Effect_Size_Study = rnorm(100),
  F_test_var_stat = runif(100),
  Outcome_Sample_Size = runif(100, min = 6, max = 10000)
)

ggplot(rct, aes(x = Effect_Size_Study, y = F_test_var_stat)) +
  geom_point(aes(size = Outcome_Sample_Size, shape = Outcome_Sample_Size)) +
  scale_size_binned_area(
    limits = c(0, 10000),
    breaks = c(0, 100, 500, 1000, 5000, 10000),
  ) +
  scale_shape_binned()

Created on 2022-08-11 by the reprex package (v2.0.1)

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • Thank you so much @Quinten. My question now is the legend from the scale_shape_binned does not show the full range of values like you see when you do not use it. – Abby Aug 11 '22 at 19:32
2

We can combine the shape and size legend together. This is not much different from the other answer, except the legend.

library(ggplot2)

set.seed(123)
rct <- data.frame(Effect_Size_Study = rnorm(100),
                  F_test_var_stat = runif(100),
                  Outcome_Sample_Size = runif(100, min = 6, max = 10000))

ggplot(rct, aes(x = Effect_Size_Study, y = F_test_var_stat)) +
  geom_point(aes(size = Outcome_Sample_Size, shape = Outcome_Sample_Size)) +
  scale_size_binned_area(name = "Outcome Sample",
                         limits = c(0, 10000),
                         breaks = c(0, 100, 500, 1000, 5000, 10000),
                         labels = c(0, 100, 500, 1000, 5000, 10000)) +
  scale_shape_binned(name = "Outcome Sample", 
                     limits = c(0, 10000),
                     breaks = c(0, 100, 500, 1000, 5000, 10000),
                     labels = c(0, 100, 500, 1000, 5000, 10000))

M--
  • 25,431
  • 8
  • 61
  • 93
  • thanks for the answer however I get the following error when I run your example "Error in `f()`: ! Breaks and labels are different lengths Run `rlang::last_error()` to see where the error occurred. " – Abby Aug 12 '22 at 11:10
  • @Abby I specifically set the breaks and labels to be the same. If you are changing the code for your actual dataset, make sure to mimic that. – M-- Aug 12 '22 at 12:32
  • I got this error when I run your example. – Abby Aug 12 '22 at 12:34
  • 1
    @Abby I am running R 4.0.3 and ggplot 3.3.5. Besides different versions, I don't know why you'd get an error. Here's a screenshot showing what I have :) https://i.stack.imgur.com/Y9abM.png – M-- Aug 12 '22 at 21:34