【问题标题】:Adding specific labels to a the top of columns on geom_bar in R将特定标签添加到 R 中 geom_bar 上的列顶部
【发布时间】:2021-07-24 02:18:19
【问题描述】:

我有一个数据库,其中包含我正在处理的示例。我试图让代码为每个变量(12 个变量)添加所有收入,然后在每列的顶部显示一个数字的缩写版本,因为它以数十亿为单位,并且有两个长。问题似乎是我不能使用计数,因为我在美学上有 x 和 y,但是如果我剪掉一个,那么我就无法制作图表。这是我的代码:

set.seed(180173)
renglones <- sample(1:nrow(Metro), size=300, replace = FALSE)
MuestraNE <- Metro[renglones, ]
View (MuestraNE)
summary(MuestraNE)

library(ggplot2)
library(reshape2)

ing = aggregate(.~ TIPO_INGRESO, FUN = sum, data=MuestraNE)

tot = colSums(ing[,3:14])
lblstot = c(tot[1]/100000000,tot[2]/100000000,tot[3]/100000000,tot[4]/100000000,
            tot[5]/100000000,tot[6]/100000000,tot[7]/100000000,tot[8]/100000000,
            tot[9]/100000000,tot[10]/100000000,tot[11]/100000000,tot[12]/100000000)
p <- as.numeric(lblstot)

gg <- melt(MuestraNE[ , 3:14])   
ggplot(gg, aes(x=variable, y=value)) + 
  geom_bar(fill = "orange", stat = "identity") + 
  ggtitle("Total por Línea de Metro")+
  geom_text(aes(y = p), vjust=-1, size = 3)+
  theme(axis.title = element_blank(), legend.position = "none",
        plot.title = element_text(hjust = 0.5, face = "bold"),
        axis.ticks = element_blank(), axis.text.y = element_blank(),
        axis.text.x = element_text(angle = 270, vjust = 0.3, hjust = -0.5,
                                   color = "black")) 

每当我尝试使用 geom_text() 添加某种格式时,我总是会收到一条错误消息:

Error: Aesthetics must be either length 1 or the same as the data (3600): y
Run `rlang::last_error()` to see where the error occurred.

我已经尝试了所有可以在谷歌和这里找到的东西,但似乎没有任何效果,如果你们能帮助我,我将不胜感激。 This is the link to my database libstot 部分是我试图在每一列之上得到的,即 tot1/10000000 在第一列之上,tot[2]/100000000 在第二列之上等等。

【问题讨论】:

  • 何塞,如果您在此处发布一个可重现的最小示例,或者输入您数据的前 10 或 20 行,那么您会在这里找到很多朋友 :) 当我被问到时,我停止访问您的数据库创建一个帐户......让你开始:我可以从代码中看到:p 是一个数字向量。 {ggplot} 使用数据框!因此,geom_text() 期望在您的数据框gg 中找到y = p。这是错误试图告诉您的,因为 ggplot 难以协调 p 是什么。或者,您可以向geom_text() 提供带有单独 x、y 和标签的单独数据框。

标签: r ggplot2 label geom-bar


【解决方案1】:

我认为您需要在 geom_text(x=..?, y=p) 中指定 也试试ggrepel

如果可能的话,在 csv 中分享您的数据 或它的第一行。

希望对你有帮助

【讨论】:

    【解决方案2】:

    以下是一个通用示例。 {ggplot} 适用于(长)数据帧。一个好的策略是准备您的数据框以包含标签。如果您仅为标签提供向量,请确保标签的长度与您定义的数据相符(检查替代方案 2)

    另外两点

    • 您现在可以使用geom_col() 而不是geom_bar(... stat = "identity")
    • ggplot 中的美学可以被继承,因此您可以将aes(x = variable, y = value) 移动到ggplot() 调用中。我将其分开,因此您可以看到添加新数据框(选项 2)允许您将文本作为“完全”单独的层来处理。从长远来看,这可能会派上用场/您将产生的其他问题/图表。
    df <- data.frame(
       variable = 1:12
      ,value = c(3,4,5,6,7,6,4,3,8,2,6,5)
    )
    
    # generate labels
    lbl <- paste0(df$value, " B")
    df <- df %>% mutate(label = lbl)
    
    ggplot(data = df) +
      geom_col( aes(x = variable, y = value), fill = "orange") +          
      geom_text(aes(x = variable, y = value, label = label), vjust = 1)   # vjust offsets the label
    

    替代方案:

    如果您选择继承美学映射以及“仅”提供标签向量的内容。代码如下:

    # we call data and set the x and y mappings
    ggplot(data = df, aes(x=variable, y= value)) + 
      geom_col(fill = "orange") +
      #------------- annotation layer ---------------------
      # the anchor points of the labels are inherited from ggplot() call 
      geom_text(aes(label = lbl), vjust = 1)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多