如何使用ggplot2在R中的2-y轴图表的两个轴上添加千位分隔符

如何使用ggplot2在R中的2-y轴图表的两个轴上添加千位分隔符,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,使用此代码,只有左侧y轴发生更改。我需要右y轴也改变。 我能做什么 > df_F10_H20_summary # A tibble: 3 x 5 Dataset count_by_Data_FH sum_Fsize mean_Fsize median_Fsize <chr> <int> <dbl> <dbl> <dbl> 1 BOND

使用此代码,只有左侧y轴发生更改。我需要右y轴也改变。 我能做什么

> df_F10_H20_summary
# A tibble: 3 x 5
  Dataset count_by_Data_FH    sum_Fsize mean_Fsize median_Fsize
  <chr>              <int>        <dbl>      <dbl>        <dbl>
1 BOND                 660 346088878101 524377088.    211524144
2 EQTY                1668 958120137292 574412552.    188492560
3 MIXED                572 214626691552 375221489.    139865165

scaleFactor <- max(df_F10_H20_summary$count_by_Data_FH) / max(df_F10_H20_summary$sum_Fsize)
#options(scipen=10000)
ggplot(df_F10_H20_summary, aes(x=Dataset,  width=.4)) +
  geom_col(aes(y=count_by_Data_FH), fill="aquamarine3", position = position_nudge(x = -.4)) +
  geom_col(aes(y=sum_Fsize * scaleFactor), fill="aquamarine4") +
  scale_y_continuous(name="Number of Funds", 
                     sec.axis=sec_axis(~./scaleFactor, name="Total Value of Funds"),
                     labels=function(y) format(y, big.mark = "'", scientific = FALSE)) +
  scale_x_discrete(labels = c("BOND",
                              "EQTY", "MIXED")) +
  theme(
    axis.title.y.left=element_text(color="aquamarine3"),
    axis.text.y.left=element_text(color="aquamarine3"),
    axis.title.y.right=element_text(color="aquamarine4"),
    axis.text.y.right=element_text(color="aquamarine4")
  ) +
  labs(title = "Number of Funds and Total value by Asset ", x = element_blank())

>df_F10_H20_摘要
#一个tibble:3x5
数据集计数按数据计算总和平均值中位数
1债券660 34608878101 524377088。211524144
2设备数量1668 958120137292 574412552。188492560
3混合572 214626691552 375221489。139865165
scaleFactorsecu axis()
函数有一个
labels
参数,您可以使用它。如果要在每个垂直轴中使用相同的格式,可以定义并使用它,例如:

my_axis_format <- function(x) format(x, big.mark = "'", scientific = FALSE)
这将在两个轴上提供相同的格式。

函数的
sec_axis()
有一个
标签
参数,您可以使用它。如果要在每个垂直轴中使用相同的格式,可以定义并使用它,例如:

my_axis_format <- function(x) format(x, big.mark = "'", scientific = FALSE)

这将在两个轴上为您提供相同的格式。

Wowwwww惊人!!!非常感谢你!!!祝你玩得愉快!当然,没问题。如果答案符合您的要求,请随意接受!;)哇,太棒了!!!非常感谢你!!!祝你玩得愉快!当然,没问题。如果答案符合您的要求,请随意接受!;)