在ggplot中为colorbar图例添加更多标签

问题描述 投票:0回答:1

我在ggplot中制作了一些情节,下面是一个颜色条图例。默认情况下,图例只有4个标签显示特定颜色的值。有些主题元素可以改变颜色条的大小,但不能改变标签的数量。如何增加标签数量?

library(ggplot2)

ggplot(mtcars, aes(x=mpg, y=carb, color=disp)) + 
  geom_point(size=3) + 
  theme(legend.key.height = unit(2,'cm'))

enter image description here

r ggplot2
1个回答
1
投票

你只需要在breaks中指定scale_color_continous()

g1 <- ggplot(mtcars, aes(x=mpg, y=carb, color=disp)) + 
  geom_point(size=3) + 
  theme(legend.key.height = unit(2,'cm'))

g1 + scale_color_continuous(breaks=seq(50,500,25))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.