【问题标题】:How to add a geom_text label containing data in the top right of a ggplot如何在ggplot的右上角添加包含数据的geom_text标签
【发布时间】:2020-04-17 14:35:13
【问题描述】:

我有一个分组图,其中我已经用它的 x 轴值标记了峰值。但是,我想在情节的右上角有另一个标签和另一个度量,但我无法让它工作。这是一个例子。

d <- data.frame(USArrests)
nrow(d)
d$predictor <- factor(c(rep(c(1, 2), times = 25)))

label <- d %>% 
  group_by(predictor) %>%
  filter(Assault == max(Assault))
label$measure1 <- c(1.45, 5.67)
label$measure2 <- c(4.55, 6.11)

library(ggplot2)
ggplot(d, aes(x=UrbanPop, y=Assault, fill=predictor)) +
  geom_col(position=position_dodge(width = 0, preserve = "single"), width = 5) +
  geom_text(data = label, aes(label = UrbanPop)) +
  geom_text(data = label, aes(label = measure), hjust="right", vjust="top")

我希望右上角的第二个标签以红色显示“measure1 = 1.45”,然后在新行“measure2 = 4.55”,然后在其下方以绿色“measure1 = 5.67”和新行“measure2= 6.11"。显然“度量”是动态对象,所以我不想插入静态标题。非常感谢任何帮助!

【问题讨论】:

    标签: r ggplot2 geom-text


    【解决方案1】:

    这有点笨拙,但您可以在 data.frame 中创建完整标签,并使用\n 强制它们位于不同的行上。

    d <- data.frame(USArrests)
    d$predictor <- factor(c(rep(c(1, 2), times = 25)))
    
    label <- d %>% 
      group_by(predictor) %>%
      filter(Assault == max(Assault))
    label$measure1 <- c(1.45, 5.67)
    label$measure2 <- c(4.55, 6.11)
    
    label2 <- label %>%
      pivot_longer(measure1:measure2, 'measure', 'value') %>%
      mutate(label = case_when(
        predictor == 1 & measure == 'measure1' ~ paste0(measure, ' = ', value),
        predictor == 1 & measure == 'measure2' ~ paste0('\n', measure, ' = ', value),
        predictor == 2 & measure == 'measure1' ~ paste0('\n\n', measure, ' = ', value),
        predictor == 2 & measure == 'measure2' ~ paste0('\n\n\n', measure, ' = ', value)
      ))
    
    ggplot(d, aes(x=UrbanPop, y=Assault, fill=predictor)) +
      geom_col(position=position_dodge(width = 0, preserve = "single"), width = 5) +
      geom_text(data = label, aes(label = UrbanPop)) +
      geom_text(data = label2, 
                aes(x = Inf, y = Inf, label = label, color = predictor), 
                hjust="right", vjust="top")
    

    【讨论】:

      【解决方案2】:

      例如,使用 x 和 y = Inf。在您的示例中,分面是有意义的,因为您将标签传递给不同的数据集。

      library(tidyverse)
      d <- data.frame(USArrests)
      
      d$predictor <- factor(c(rep(c(1, 2), times = 25)))
      
      label <- d %>% 
        group_by(predictor) %>%
        filter(Assault == max(Assault))
      label$measure1 <- c(1.45, 5.67)
      label$measure2 <- c(4.55, 6.11)
      
      ggplot(d, aes(x=UrbanPop, y=Assault, fill=predictor)) +
        geom_col(position=position_dodge(width = 0, preserve = "single"), width = 5) +
        geom_text(data = label, aes(label = UrbanPop)) +
        geom_text(data = label, aes(x = Inf, y = Inf, label = measure2), hjust="right", vjust="top")+
        facet_grid(~predictor)
      #> Warning: position_dodge requires non-overlapping x intervals
      
      #> Warning: position_dodge requires non-overlapping x intervals
      

      reprex package (v0.3.0) 于 2020-04-17 创建

      【讨论】:

      • 太好了,谢谢。恐怕我需要看到重叠,所以不能使用 facet。您的解决方案很好地将标签移动到右上角,但两组的测量值 1 和 2 都重叠。有没有办法在它们停留在右上角时通过线来抵消它们?
      猜你喜欢
      • 2017-05-22
      • 2018-07-26
      • 2020-08-27
      • 2017-09-10
      • 1970-01-01
      • 2019-08-11
      • 2021-12-12
      • 2021-03-09
      • 1970-01-01
      相关资源
      最近更新 更多