将括号用于y轴ggplot2上的负值 [英] Using parentheses for negative values on y-axis ggplot2

查看:65
本文介绍了将括号用于y轴ggplot2上的负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在括号中显示y轴负值,而不是带负号.例如,我要显示的不是(2,000).我在R中使用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.

我尝试在scale_y_continuous内部使用"negative_parens = TRUE",如下所示,但未成功.

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

上面的代码不会引发错误,但仍返回标有负号而不是括号的y轴.关于如何使负值出现在y轴上的括号的任何提示?任何帮助表示赞赏.

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.

推荐答案

我相信 scales :: dollar_format 具有 negative_parens 选项,但其他格式如 scales:: comma_format 不会.(从当前的v0.4.1开始)

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)

应使用:

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

例如:

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

这篇关于将括号用于y轴ggplot2上的负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆