簡體   English   中英

對 y 軸上的負值使用括號 ggplot2

[英]Using parentheses for negative values on y-axis ggplot2

我想在括號中顯示 y 軸負值,而不是用負號。 例如,我想顯示 (2,000) 而不是 -2,000。 我在 R 中使用 ggplot2。

我嘗試在 scale_y_continuous 中使用“negative_parens = TRUE”,如下所示,但沒有成功。

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 軸的括號中的任何提示? 任何幫助表示贊賞。

我相信scales::dollar_format具有該negative_parens選項,但其他格式如scales::comma_format則沒有。 (截至當前 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 = "")
  )

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM