-1

I am setting up a scatter plot, and the size of the dots represents the number of samples the measurement was based.

There is a skewed distribution of the samples sizes for the values / dots.

Many have samples sizes from 10 to 50 and others are based on 500 or 1000 measurements.

I would like to change the legend in a way that the dot size for sample size 10 and 50 as well as 500 and 1000 is indicated.

Which setting would allow this?

enter image description here

gg.pf0<-ggplot(bc.pf,aes(x=age.WERT,y=weight,color=as.factor(bc.pf$Group),size=(bc.pf$n)))
(gg.pf1<-gg.pf0 + geom_point(aes(alpha=0.5)))
Niels
  • 141
  • 12
  • Please add reproducible minimal sample data so that we can reproduce the plot (or a similar plot) you're showing. Concerning your question, you should take a look at `scale_size_manual`, specifically the `breaks` argument. Lastly, *never* use `$`-indexing inside `aes`, which can lead to some very surprising and unwanted results. – Maurits Evers May 09 '19 at 22:12
  • See also: [Issue when passing variable with dollar sign notation ($) to aes() in combination with facet_grid() or facet_wrap()](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio) – Maurits Evers May 09 '19 at 22:17
  • You probably want to use something like `scale_size_area(breaks = c(10, 50, 500, 1000))` – Jon Spring May 09 '19 at 22:21

1 Answers1

3

Two general comments:

  1. You should never use $-indexing inside aes. See this post to understand what can go wrong if you do.
  2. You should always provide reproducible minimal sample so that SO respondents have something to work with. Explicit sample data also often helps avoid ambiguities regarding data types.

As to your question, you can use scale_size_continuous. Here is a reproducible example based on mtcars

ggplot(mtcars, aes(mpg, disp, size = hp)) +
    geom_point() +
    scale_size_continuous(
        breaks = c(10, 50, 500, 1000),
        limits = c(0, 1000))

enter image description here

Note that scale_size scales the area of the dots, while scale_radius scales the radius. An alternative to scale_size is scale_size_area which ensures that a value of 0 is mapped to a size of 0.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68