简体   繁体   中英

Using parentheses for negative values on y-axis ggplot2

I'd like to show y-axis negative values in parentheses rather than with a negative sign. For example, rather than -2,000 I'd like to show (2,000). I'm using ggplot2 in R.

I've tried using "negative_parens = TRUE" inside of scale_y_continuous as shown below without success.

ggplot(dataset, aes(x=date, y=value, group = variable, color = variable, 
size = variable))+geom_line()+
scale_size_manual(values =c(1.5, 1.5, 1.5))+
theme_light(base_size = 16)+scale_y_continuous(labels = 
comma_format(negative_parens = TRUE), limits = c(-3500, 6000))

The code above does not throw an error, but still returns a y-axis labeled with negative signs rather than parentheses. Any tips on how to get negative values to appear in parentheses on the y-axis? Any help is appreciated.

I believe scales::dollar_format has that negative_parens option but the other formats like scales::comma_format do not. (As of current v0.4.1)

Should work with:

scale_y_continuous(
  labels =  scales::dollar_format(negative_parens = TRUE, prefix = ""), 
  limits = c(-3500, 6000)
)

For example:

ggplot(mtcars, aes(x=wt, y=mpg - mean(mpg)))+
  geom_point()+
  scale_y_continuous(
    labels =  scales::dollar_format(negative_parens = TRUE,
                                    prefix = "")
  )

在此处输入图像描述

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