简体   繁体   English

在ggplot中向颜色条图例添加更多标签

[英]Add more labels to colorbar legend in ggplot

I'm making some plots in ggplot with a colorbar legend like below. 我在ggplot中使用以下颜色条图例进行一些绘图。 By default the legend has only 4 labels showing the value of specific colors. 默认情况下,图例只有4个标签,显示特定颜色的值。 There are theme elements which can change the size of the colorbar, but not the number of labels. 有些主题元素可以更改颜色条的大小,但不能更改标签的数量。 How can I increase the number of labels? 如何增加标签数量?

library(ggplot2)

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

在此处输入图片说明

You just need to specify the breaks in scale_color_continous() 您只需要在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))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM