Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show more bubble sizes in legend of ggplot?

Tags:

r

ggplot2

I am trying to plot a bubble plot using ggplot with different sized dots based on a vector of values. It works as it should with this code

ggplot(example, aes(x=x, y=y)) +
  geom_point(aes(size=size))

However, the sample size legend only shows two values that are on the higher end of the size spectrum, even though in the example of http://docs.ggplot2.org/current/geom_point.html it shows 4 dot sizes by default. I want to have a wider range of dot sizes in the legend.

I tried it with

example$size_bin <- cut(example$size, breaks = c(0,1000,5000,10000,50000,100000,300000),
                               labels=c("0-1000","1000-5000","5000-10000","10000-50000","50000-100000",">100000"))
ggplot(example, aes(x=x, y=y)) +
  geom_point(aes(size=size_bin))

but because this is not a continuous scale this does not work. How can I change the legend so that there are more sizes shown?

enter image description here

like image 598
Niek de Klein Avatar asked Jul 13 '16 15:07

Niek de Klein


People also ask

How do I make my Ggplot legend smaller?

To change the Size of Legend, we have to add guides() and guide_legend() functions to the geom_point() function. Inside guides() function, we take parameter color, which calls guide_legend() guide function as value.

How do I name a circle in a bubble chart in R?

To add labels on each bubble in a bubble plot in the R Language, we use the geom_text() function of the ggplot2 package. The geom_text() function adds textual annotation overlap on top of the ggplot plot. Parameter: x and y: determines the position of the label.

How do you read a bubble plot?

Each bubble in a chart represents a single data point. The values for each bubble are encoded by 1) its horizontal position on the x-axis, 2) its vertical position on the y-axis, and 3) the size of the bubble. Sometimes, the color of the bubble or its movement in animation can represent more dimensions.


1 Answers

Use the breaks argument in a separate scale_size_continuous() specification. From ?scale_size_continuous:

breaks: One of:

• ‘NULL’ for no breaks
• ‘waiver()’ for the default breaks computed by the transformation object
• A numeric vector of positions
• A function that takes the limits as input and returns breaks as output

like image 173
Ben Bolker Avatar answered Nov 15 '22 00:11

Ben Bolker