Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GGplot generates two legends with a bubble plot, how to delete one of them

Tags:

r

ggplot2

The following code block generates a plot with two legends:

Spend7d_bubble <- ggplot(cluster_visuals, 
                          aes(x = ltv_7d, y = avg_daily_sessions, 
                              color = factor(cluster8), size = n)) +
                   geom_point(alpha = 0.5) +
                   scale_size_continuous(range = c(2, 25))

enter image description here

Notice how this generates two legends on the right, one for n and one for factor(cluster8).

How can I only include the legend for factor(cluster8) and also rename it to just 'cluster'?

like image 284
Doug Fir Avatar asked Oct 23 '25 16:10

Doug Fir


2 Answers

Spend7d_bubble <- ggplot(cluster_visuals, 
                          aes(x = ltv_7d, y = avg_daily_sessions, 
                              color = factor(cluster8), size = n)) +
                   geom_point(alpha = 0.5) +
                   scale_size_continuous(range = c(2, 25), guide = 'none') +
                   labs(color = "Cluster")
like image 74
Ben Avatar answered Oct 26 '25 07:10

Ben


Whichever of those aesthetics (color or size) that you don't want a legend for, should be out of aes(). As you see, you don't have any legend for alpha in geom_point since it is not an argument of aes.

ggplot(cluster_visuals, 
        aes(x = ltv_7d, y = avg_daily_sessions, color = factor(cluster8)), size = n) +
 geom_point(alpha = 0.5) +
 scale_size_continuous(range = c(2, 25))
like image 45
M-- Avatar answered Oct 26 '25 06:10

M--



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!