首页 文章

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

提问于
浏览
0

我在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

1 回答

  • 1

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

    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

相关问题