首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向ggplot的x轴上的数字添加逗号和加号(+

向ggplot的x轴上的数字添加逗号和加号(+
EN

Stack Overflow用户
提问于 2021-10-15 16:55:47
回答 1查看 96关注 0票数 1

我已经绘制了下图,它显示了x轴上的数字。我想要的是在这些数字上添加逗号和加号。我期望的是"+100,000“或"+200,000”。尽管如此,我还是只能单独完成,比如:"100,000“或"+100000”

我使用了以下代码。

代码语言:javascript
复制
ggplot(data, aes(x = difference_gdp, y = difference_rate, color = region)) +  
  geom_point(size = 4) + 
  xlab("Variation in GDP per capita, 1990 vs 2019")  + 
  ylab("Variation in age-standardised\nT2DM-attributable deaths per\n100,000 people, 1990 vs 2019") +
  stat_cor(method = "pearson", aes(x=difference_gdp, y = difference_rate, color = NA), digits = 2, p.accuracy = 0.05) +   
  geom_smooth(method = 'lm', formula = 'y~x', se = FALSE, aes(color = NA)) + 
  scale_x_continuous(labels = function(x) sprintf("%+d", x)) +
  scale_y_continuous(labels = function(y) sprintf("%+d", y))

我知道添加逗号的代码是scale_x_continuous(labels = comma),但我不知道如何将它添加到前面的代码中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-15 17:00:31

我认为scales包涵盖了这个用例

e.g

代码语言:javascript
复制
scales::number_format(prefix = "+",big.mark = ",")(1000)

也许这行得通,不能测试它

代码语言:javascript
复制
ggplot(data, aes(x = difference_gdp, y = difference_rate, color = region)) +  
geom_point(size = 4) + xlab("Variation in GDP per capita, 1990 vs 2019")  + ylab(
  "Variation in age-standardised\nT2DM-attributable deaths per\n100,000 people, 1990 vs 2019"
) + stat_cor(
  method = "pearson",
  aes(x = difference_gdp, y = difference_rate, color = NA),
  digits = 2,
  p.accuracy = 0.05
) +   geom_smooth(method = 'lm',
                  formula = 'y~x',
                  se = FALSE,
                  aes(color = NA)) +  scale_x_continuous(
                    labels = scales::number_format(prefix = "+",big.mark = ",")
                  ) + scale_y_continuous(
                    labels = scales::number_format(prefix = "+",big.mark = ",")
                  )
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69588182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档