R语言 如何将数据标签表示为货币

7nbnzgx9  于 2023-05-20  发布在  其他
关注(0)|答案(2)|浏览(123)

我希望我的数据标签显示为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")
ukxgm1gy

ukxgm1gy1#

**更新:**在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"
  )

第一个答案:

geom_label更改为geom_label(aes(label = paste0("$", jordan, "M"), y = jordan / adjust), 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"
  )

rxztt3cl

rxztt3cl2#

另一种选择是使用scales包的dollar函数,如下所示:

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 = dollar(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")

创建于2023-05-18带有reprex v2.0.2

相关问题