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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With