r - 如何将数据标签表达为金钱

标签 r ggplot2

我希望我的数据标签读取为 1.377 亿美元。我有办法实现这一目标吗?我的代码如下。如有任何帮助,我们将不胜感激。

#Entering Data
year <- c("2016", "2017", "2018", "2019", "2020", "2021", "2022")
nike <- c(2.8, 3.1, 2.9, 3.1,  3.6, 4.7,  5.1)
jordan <- c(137.7, 154.9, 142.8, 156.9, 180.5, 235.6, 255.0)

data <- data.frame(year = year, nike = nike, jordan = jordan)
adjust <- 50 # for adjusting second y-axis

# Plotting Charts and adding a secondary axis
library(ggplot2)
colors <- c("Jordan Brand Revenue" = "red3",
        "Michael Jordan Earnings" = "black") # For adding a legend.
ggplot(data, aes(x=year))+  
geom_col(aes(y = nike, color="Jordan Brand Revenue"), lwd = 1 , fill = "red3") +
 geom_line(aes(y = jordan/adjust, color="Michael Jordan Earnings"), lwd = 1.5,      

group     = 1)+
geom_label(aes(label = jordan, y = jordan/adjust), nudge_y = 0.7) +
scale_y_continuous(
name = "Jordan Brand Revenue (in $B)",
labels = scales::dollar,
breaks = seq(0,6,1),
limits = c(0,6),
sec.axis = sec_axis(~.*adjust, name = "Michael Jordan Earnings (in $M)",
                    labels = scales::dollar,
                    breaks= seq(0,300,50))) +
  theme_classic() +
  scale_color_manual(values=colors)+
  theme(axis.title.x=element_blank(),
    panel.grid.major.y = element_line(),
    legend.title = element_blank(),
    legend.position = "top")

最佳答案

更新:在OP请求后查看评论:

library(ggplot2)
library(scales)

colors <- c("Jordan Brand Revenue" = "red3",
            "Michael Jordan Earnings" = "black") # For adding a legend.

ggplot(data, aes(x=year)) +  
  geom_col(aes(y = nike, color = "Jordan Brand Revenue"), lwd = 1, fill = "red3") +
  geom_line(aes(y = jordan / adjust, color = "Michael Jordan Earnings"), lwd = 1.5, group = 1) +
  geom_point(aes(y = jordan / adjust, color = "Michael Jordan Earnings"), size = 3) +  # Add points to the line
  geom_label(aes(label = paste0("$", jordan, "M"), y = jordan / adjust), nudge_y = 0.7) +
  scale_y_continuous(
    name = "Jordan Brand Revenue (in $B)",
    labels = dollar_format(suffix = "B"),  # Set tick labels to include "B" for billions
    breaks = seq(0, 6, 1),
    limits = c(0, 6),
    sec.axis = sec_axis(~ . * adjust, name = "Michael Jordan Earnings (in $M)",
                        labels = dollar_format(suffix = "M"),  # Set tick labels to include "M" for millions
                        breaks = seq(0, 300, 50))
  ) +
  theme_classic() +
  scale_color_manual(values = colors) +
  theme(
    axis.title.x = element_blank(),
    panel.grid.major.y = element_line(),
    legend.title = element_blank(),
    legend.position = "top"
  )

enter image description here

第一个答案:

geom_label 更改为 geom_label(aes(label = Paste0("$", jordan, "M"), y = jordan/adjustment), nudge_y = 0.7) +

library(ggplot2)
library(scales)

colors <- c("Jordan Brand Revenue" = "red3",
            "Michael Jordan Earnings" = "black") # For adding a legend.

ggplot(data, aes(x=year)) +  
  geom_col(aes(y = nike, color = "Jordan Brand Revenue"), lwd = 1, fill = "red3") +
  geom_line(aes(y = jordan / adjust, color = "Michael Jordan Earnings"), lwd = 1.5, group = 1) +
  geom_label(aes(label = paste0("$", jordan, "M"), y = jordan / adjust), nudge_y = 0.7) +
  scale_y_continuous(
    name = "Jordan Brand Revenue (in $B)",
    labels = dollar_format(),
    breaks = seq(0, 6, 1),
    limits = c(0, 6),
    sec.axis = sec_axis(~ . * adjust, name = "Michael Jordan Earnings (in $M)",
                        labels = dollar_format(),
                        breaks = seq(0, 300, 50))
  ) +
  theme_classic() +
  scale_color_manual(values = colors) +
  theme(
    axis.title.x = element_blank(),
    panel.grid.major.y = element_line(),
    legend.title = element_blank(),
    legend.position = "top"
  )

enter image description here

关于r - 如何将数据标签表达为金钱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76281549/

相关文章:

R:如何计算“bgeva”对象/模型的 AUC 和 ROC 曲线?

r - 如何旋转/取消旋转(类型转换/熔化)数据框?

R计算两列值并以表格形式获取结果

r - 根据每组的其他列匹配列的值

r - 如何从 URL 读取表格并将其保存为数据框?

r - 在 ggplot 上创建带有比例的条形图

r - 使用 ggplot2 绘制热图; logscale 颜色和修改颜色图例

r - 将ggplot对象从R中的循环内存储在列表中

r - 从ggplot2中的图例中删除空格的方法

r - 如何从 ggplot2 的热图函数中提取多边形?