【问题标题】:R: How to add a label on the right axis in a ggplot barchart?R:如何在 ggplot 条形图中的右轴上添加标签?
【发布时间】:2021-12-12 01:10:45
【问题描述】:

假设我用这段代码创建了一个 ggplot 条形图:

mtcars$carb<-as.character(mtcars$carb)
mtcars$gear<-as.character(mtcars$gear)
mtcars$carb_labelright<-paste0("label_right",mtcars$carb)

#pelacolset
ggplot(mtcars, 
       #color by which to fill
       aes(fill=gear, 
           #y axis
           y=wt, 
           #x axis
           x=carb)) +
  #title and subtitle
  
  
  #barplot
  geom_bar(position="fill", stat="identity",width=.8)+
  coord_flip()+
  

  #rotate x axis labels 90 degrees
  theme(axis.text.x = element_text(angle=90),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank())

剧情如下:

现在我想使用 carb_labelright 列在条形图的右侧添加数据。它应该是这样的:

如何做到这一点?

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    我发现使用geom_text(您需要一个数据框)或annotate(geom = "text",...) 最容易进行注释。使用注释通常更安全,因为geom_text 喜欢为每一行创建一个标签(当您使用精心准备的数据框进行注释时,这很好)。

    library(tidyverse)
    mtcars$carb<-as.character(mtcars$carb)
    mtcars$gear<-as.character(mtcars$gear)
    mtcars$carb_labelright<-paste0("label_right",mtcars$carb)
    
    ggplot(mtcars) +
      # use geom_col, a convenience wrapper for geom_bar(stat = "identity")
      geom_col(aes(fill=gear, y=wt, x=carb), 
               position="fill", width=.8) +
      # you have to turn clipping off
      coord_flip(clip = "off") +
      annotate(geom = "text", x = unique(mtcars$carb),
               label = unique(mtcars$carb_labelright),
               y = 1, hjust = 0) +
      # you need to increase the legend's margin and make it "transparent"
      # otherwise you will cover some labels. 
      theme(legend.margin = margin(l = 1, unit = "inch"),
            legend.background = element_blank())
    

    reprex package (v2.0.1) 于 2021 年 10 月 26 日创建

    【讨论】:

    • 谢谢,这可行,但有没有不创建新数据框的选项?在我的应用程序中,最好按原样使用该列...
    • @Olympia 更新
    • 完美答案!
    【解决方案2】:

    我还遇到了离散轴不支持二级刻度的问题(请参阅 ggplot2 repo 上的相关issue)。我自己写了a manual guide 解决了这个问题,这确实允许我制作具有离散比例的辅助轴。在下面的代码中,我们使用ggh4x::guide_axis_manual() 结合 rlang/purrr lambda 函数来格式化标签。 (免责声明:我是 ggh4x 的作者)。

    library(ggplot2)
    #> Warning: package 'ggplot2' was built under R version 4.1.1
    
    mtcars$carb<-as.character(mtcars$carb)
    mtcars$gear<-as.character(mtcars$gear)
    mtcars$carb_labelright<-paste0("label_right",mtcars$carb)
    
    p <- ggplot(mtcars, 
           aes(fill = gear, y = carb, x = wt)) +
      geom_col(position="fill", width=.8)
    
    p + guides(
      y.sec = ggh4x::guide_axis_manual(
        labels = ~ paste0("label_right_", .x)
      )
    )
    

    或者,您也可以直接将标签作为字符向量。

    p + guides(
      y.sec = ggh4x::guide_axis_manual(
        labels = sort(unique(mtcars$carb_labelright))
      )
    )
    

    reprex package (v2.0.1) 于 2021 年 10 月 26 日创建

    【讨论】:

    • 很好的解决方案!
    猜你喜欢
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多