【问题标题】:Label single bars in ggplot在ggplot中标记单条
【发布时间】:2022-08-15 21:50:25
【问题描述】:

我想知道如何将标签添加到ggplot2 中的单个条形图,因为我想在条形图中将我的行标记为在线经纪人、银行、无账户。谢谢您的帮助!

这是我的代码:

library(gridExtra)
library(ggplot2)
require(gridExtra)
library(tidyverse)
library (scales)

plot1 <- ggplot(data = df, aes(df$InvA, y = after_stat(prop)), na.rm = TRUE) +
geom_bar() +
scale_x_discrete(na.translate = FALSE) +
ggtitle(\"Post-Covid\") +
xlab(\"Accounts\") +
ylab(\"Individuals\") +
scale_y_continuous(labels = percent_format(), limits=c(0,0.8))
#> Scale for \'y\' is already present. Adding another scale for \'y\', which will
#> replace the existing scale.

plot2 <- ggplot(data = df, aes(df$InvAcc, y = after_stat(prop)), na.rm = TRUE) +
  geom_bar() +
  scale_x_discrete(na.translate = FALSE) +
  ggtitle(\"Pre-Covid\") +
  xlab(\"Accounts\") +
  ylab(\"Individuals\") +
 scale_y_continuous(labels = percent_format(), limits=c(0,0.8))
#> Scale for \'y\' is already present. Adding another scale for \'y\', which will
#> replace the existing scale.

grid.arrange(plot2, plot1, ncol = 2)

然后情节如下所示:

但是,对于我之前的绘图,标签应该看起来像这些,但是由于我设法将 y 比例缩放为百分比,因此我的标签消失了,或者无法将 y 比例缩放为百分比,而剩余的值作为因子(在线经纪人,银行,无账户)因为我不得不将它们更改为数字(1,2,3):

dput(dfaccounts) # (with 1=Online Broker, 2=Bank, 3=No Account)

structure(list(df.InvAcc = c(2L, NA, 2L, NA, NA, 3L, 3L, 3L, 
NA, 3L, 3L, NA, 1L, NA, 1L, NA, NA, 1L, NA, NA, NA, 1L, 3L, 1L, 
NA, NA, 1L, 2L, NA, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 2L, NA, 
NA, 3L, NA, NA, 1L, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 
1L, 1L, 1L, NA, NA, NA, 3L, NA, 1L, NA, NA, 2L, NA, 1L, 1L, 1L, 
NA, 1L, 3L, NA, 1L, NA, 3L, NA, NA, 2L, 3L, 2L, 1L, NA, 3L, 2L, 
NA, NA, 3L, NA, 2L, 1L, NA, 3L, 2L, 1L, 3L, 3L, 3L, NA, 3L, NA, 
3L, NA, 3L, 1L, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, NA, 3L, NA, 
NA, 3L, 3L, 3L, 3L, NA, 1L, NA, NA, NA, 3L, NA, 3L), df.InvA = c(NA, 
1L, NA, 2L, 1L, NA, NA, NA, 3L, NA, NA, 3L, NA, 3L, NA, 1L, 2L, 
NA, 1L, 1L, 1L, NA, NA, NA, 1L, 2L, NA, NA, 2L, 1L, NA, NA, NA, 
NA, NA, NA, NA, 3L, NA, 1L, 1L, NA, 1L, 1L, NA, 1L, NA, 1L, 3L, 
1L, 1L, 1L, 2L, 1L, 1L, NA, NA, NA, NA, 1L, 1L, 1L, NA, 2L, NA, 
2L, 1L, NA, 2L, NA, NA, NA, 2L, NA, NA, 2L, NA, 1L, NA, 3L, 3L, 
NA, NA, NA, NA, 1L, NA, NA, 1L, 2L, NA, 1L, NA, NA, 1L, NA, NA, 
NA, NA, NA, NA, 1L, NA, 1L, NA, 1L, NA, NA, 1L, 1L, 3L, NA, 1L, 
2L, 2L, NA, 1L, 1L, NA, 3L, 1L, NA, NA, NA, NA, 1L, NA, 1L, 3L, 
1L, NA, 3L, NA)), class = \"data.frame\", row.names = c(NA, -133L
))

    标签: r ggplot2 label


    【解决方案1】:

    问题是您 InvAInvAcc 列是数字。因此,使用scale_x_discrete 会删除轴文本。

    为了解决您的问题,我建议将列转换为 factors,并通过 labels 参数设置所需的标签。此外,为了获得正确的百分比,我们必须通过group=1 显式设置group aes:

    library(gridExtra)
    library(ggplot2)
    
    labels <- c("Online-Broker", "Bank", "No Account")
    data$InvA <- factor(data$InvA, labels = labels)
    data$InvAcc <- factor(data$InvAcc, labels = labels)
    
    plot1 <- ggplot(data = data, aes(InvA, y = after_stat(prop), group = 1), na.rm = TRUE) +
      geom_bar() +
      scale_x_discrete(na.translate = FALSE) +
      ylim(0, 40) +
      ggtitle("Post-Covid") +
      xlab("Accounts") +
      ylab("Total No. of Individuals") +
      scale_y_continuous(labels = scales::percent)
    #> Scale for 'y' is already present. Adding another scale for 'y', which will
    #> replace the existing scale.
    
    plot2 <- ggplot(data = data, aes(InvAcc, y = after_stat(prop), group = 1), na.rm = TRUE) +
      geom_bar() +
      scale_x_discrete(na.translate = FALSE) +
      ylim(0, 40) +
      ggtitle("Pre-Covid") +
      xlab("Accounts") +
      ylab("Total No. of Individuals") +
      scale_y_continuous(labels = scales::percent)
    #> Scale for 'y' is already present. Adding another scale for 'y', which will
    #> replace the existing scale.
    
    grid.arrange(plot2, plot1, ncol = 2)
    #> Warning: Removed 64 rows containing non-finite values (stat_count).
    #> Warning: Removed 69 rows containing non-finite values (stat_count).
    

    【讨论】:

    • 谢谢@stefan!你真的拯救了我的一天,非常感谢你!
    猜你喜欢
    • 2013-03-03
    • 2023-03-27
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    相关资源
    最近更新 更多