简体   繁体   English

如何在 R 的条形图标签上添加 $ 符号? [包括图像和代码]

[英]How to add $ sign to labels on bar chart in R? [Image & Code Included]

Looking to add a dollar sign on the labels on my x axis and the numbers in the bar chart.希望在我的 x 轴上的标签和条形图中的数字上添加美元符号。 Below is my code and the chart.下面是我的代码和图表。

YTD_bar <- 
      ggplot(TYSales_LYSales, aes(x=as.character(FSCL_YR), y=SALES)) + geom_bar(stat="identity", fill="orange", color="grey40") + theme_bw() + coord_flip() + 
      geom_text(aes(x= as.character(FSCL_YR), y=0.01, label= SALES),
                hjust=-0.8, vjust=-1, size=3, 
                colour="black", fontface="bold",
                angle=360) + labs(title="D27 2020 YTD Sales v 2019 YTD Sales", x="Fiscal Year",y="Sales") + theme(plot.title=element_text(hjust=0.5))
    YTD_bar

在此处输入图像描述

在此处输入图像描述

You can do this with liberal application of sprintf("$%0.2f", ...) .您可以通过自由应用sprintf("$%0.2f", ...)来做到这一点。 The %0.2f part tells is to format as floating point numbers, but with two decimal places. %0.2f部分告诉是格式化为浮点数,但有两位小数。 You need to do this in two places: (1) within geom_text() , and (2) as part of a call to scale_y_continuous() :您需要在两个地方执行此操作:(1) 在geom_text()中,以及 (2) 作为对scale_y_continuous()的调用的一部分:

YTD_bar <- ggplot(TYSales_LYSales, aes(x=as.character(FSCL_YR), y=SALES)) +
    geom_bar(stat="identity", fill="orange", color="grey40") +
    theme_bw() +
    coord_flip() + 
    geom_text(aes(x = as.character(FSCL_YR), y=0.01,
                  label= sprintf("$%0.2f", SALES)),
              hjust=-0.8, vjust=-1, size=3, 
              colour="black", fontface="bold",
              angle=360) +
    labs(title="D27 2020 YTD Sales v 2019 YTD Sales",
         x="Fiscal Year", y="Sales") +
    theme(plot.title=element_text(hjust=0.5)) +
    scale_y_continuous(labels = function(breaks) sprintf("$%0.2f", breaks))
YTD_bar

在此处输入图像描述

Data数据

TYSales_LYSales <- data.frame(
    FSCL_YR = 2019:2020,
    SALES   = c(61851186, 5511072)
)

You can specify the label yourself and add a dollar sign in front by using paste0() function您可以自己指定 label 并使用 paste0() function 在前面添加一个美元符号

df <- data.frame(yr = 2019:2020, sales = c(1234, 5678))
df$text <- paste0("$", df$sales)

ggplot(df, aes(x=as.character(yr), y=sales)) + 
  geom_col(fill="orange", color="grey40") + 
  theme_bw() + 
  coord_flip() + 
  geom_text(aes(x = as.character(yr), y=0.01, label= text),
            hjust=-0.8, vjust=-1, size=3, 
            colour="black", fontface="bold",
            angle=360) +
  scale_y_continuous(breaks = 0:3 * 2000,
                     labels = paste0("$", 0:3 * 2000))

The scales package (installed with ggplot2) has the handy dollar and label_dollar() functions for converting the decimal values into currency.秤 package(与 ggplot2 一起安装)具有方便的dollarlabel_dollar()函数,用于将十进制值转换为货币。

See help to understand the may options available to adjust the formatting.请参阅帮助以了解可用于调整格式的可能选项。

library(ggplot2)
library(scales)

YTD_bar <- 
   ggplot(TYSales_LYSales, aes(x=as.character(FSCL_YR), y=SALES)) + 
   geom_bar(stat="identity", fill="orange", color="grey40") + 
   theme_bw() + coord_flip() + 
   geom_text(aes(x= as.character(FSCL_YR), y=0.01, label= dollar(SALES)),
             hjust=-0.8, vjust=-1, size=3, colour="black", fontface="bold", angle=360) + 
   labs(title="D27 2020 YTD Sales v 2019 YTD Sales", x="Fiscal Year",y="Sales") + 
   theme(plot.title=element_text(hjust=0.5)) +
   scale_y_continuous(labels = label_dollar())
YTD_bar

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM