user4126251
user4126251

Reputation: 25

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.

Upvotes: 2

Views: 788

Answers (1)

Jon Spring
Jon Spring

Reputation: 66870

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 = "")
  )

enter image description here

Upvotes: 4

Related Questions