我希望我的数据标签显示为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")
2条答案
按热度按时间ukxgm1gy1#
**更新:**在OP要求后,请参见备注:
第一个答案:
将
geom_label
更改为geom_label(aes(label = paste0("$", jordan, "M"), y = jordan / adjust), nudge_y = 0.7) +
rxztt3cl2#
另一种选择是使用
scales
包的dollar
函数,如下所示:创建于2023-05-18带有reprex v2.0.2