# A standard plot
p <- ggplot(mtcars, aes(disp, mpg)) +
geom_point(aes(size = hp), alpha = 0.3)
# By default, the sizes aren't large enough to make this guide clear
p + scale_size_area(guide = "circles")
# Update with a more approrpriate scale
p <- p +
scale_size_area(
max_size = 30,
limits = c(0, NA),
breaks = c(0, 25, 100, 250)
)
p + guides(size = "circles")
# Horizontal orientation
p + guides(size = guide_circles(
vjust = 0.5, hjust = 0, text_position = "bottom"
))
# Alternative text placement
p + guides(size = guide_circles(
text_position = "ontop",
clip_text = TRUE
))
# More styling options
p + guides(size = guide_circles(override.aes = aes(colour = "red")))+
theme(
# Key background
legend.key = element_rect(colour = "black", fill = 'white'),
# Padding around central shapes
legendry.legend.key.margin = margin(1, 1, 1, 1, "cm"),
legend.ticks = element_line(colour = "blue"),
legend.text.position = "left"
)
Run the code above in your browser using DataLab