简体   繁体   中英

scale_color_gradient2 having text be rounded

I am using scale_fill_gradient2() and the colourbar that is created is showing decimal places. I tried to reproduce the text that shows decimals but could not but the text below is in scientific notations.

How can you round the numbers that displayed in the colourbar using scale_fill_gradient2() ? For example I am seeing "25.00" and I'd like to show just "25"?

Also how can you set the labels manually? Let's say I want to look a the data and set labels like c(15, 25, 40) ?

library(ggplot2)
dat <- data.frame(group = c(rep("A", 10), rep("B", 10)),
                  value = c(rnorm(10, 5,300), rnorm(10, 5000, 80000)))

ggplot(dat, aes(x = group, y = value, fill= value)) + 
       geom_bar(stat = "identity") +
       scale_fill_gradient2(low = "red", mid = "yellow", high = "blue", midpoint = 0, name = "")

You can manually specify breaks and labels as needed.

ggplot(dat, aes(x = group, y = value, fill= value)) +
  geom_bar(stat = "identity")+
  scale_fill_gradient2(low = "red", mid = "yellow", high = "blue", 
                       midpoint = 0, name = "",
                       breaks = c(0, 1e5, 2e5),
                       labels = c("0", "100,000", "200,000"))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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