gghighlight(R):给柱状图添加标签

4

好的,经过长时间的默读后,这是我的第一个问题。我正在尝试为分组条形图添加未高亮项目的相应标签。当我在geom_text前面插入gghighlight时,我得到了以下绘图:

library(tidyverse)
library(gghighlight)


df <- data.frame (group = c("A", "A", "B", "B", "C", "C"),
                  value  = c("value_1", "value_2","value_1", "value_2","value_1", "value_2"),
                  mean = c(1.331, 1.931, 3.231, 3.331, 4.631, 3.331)
)

ggplot(data = df, aes(x = group, y = mean, fill = value)) +
  geom_bar(stat = "identity", position = "dodge") +
  gghighlight(group != "B",
              label_key = group
              ) +
  geom_text(aes(label = round(mean, digits = 2)), 
            stat= "identity", 
            vjust = -.5,
            position = position_dodge(width = .9)
            ) 

仅突出显示标签的条形图

如果我将gghightlight移动到geom_text的后面,我会得到以下的图表:

ggplot(data = df, aes(x = group, y = mean, fill = value)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label = round(mean, digits = 2)), 
            stat= "identity", 
            vjust = -.5,
            position = position_dodge(width = .9)
            ) +
  gghighlight(group != "B",
              label_key = group)

带有双标签的条形图:高亮和非高亮

是否有一种方法可以像高亮的那些一样给未高亮的条形添加标签?

谢谢提前。

############# 编辑 ##############

除了将某些列变为灰色(参见@TarJae的答案),还可以使它们变得透明(此帖子的关键部分来自:ggplot透明度对个别柱的影响):

subset_df <- df %>% 
  mutate(alpha.adj = as.factor(ifelse(group != "B", 1, 0.6)))


ggplot(data = subset_df, aes(x = group, y = mean, fill = value, alpha=factor(alpha.adj))) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label = round(mean, digits = 2)), 
            stat= "identity", 
            vjust = -.5,
            position = position_dodge(width = .9)
  ) +
  scale_alpha_manual(values = c("0.6"=0.6, "1"=1), guide='none') 

[透明条形图及标签]

2个回答

3
你是否在寻找以下内容? 这是一种不使用 gghighlight 包的解决方案:
library(tidyverse)

subset_df <- df %>% 
  mutate(highlight = if_else(group != "B", mean, NA_real_))

ggplot(data = subset_df, aes(x = group, y = mean, group=value)) +
  geom_col(fill = 'grey', alpha = 0.6, position = 'dodge') +
  geom_col(aes(y = highlight, fill = value), position = 'dodge') +
  geom_text(aes(group, label = round(mean, digits = 2)),
            position = position_dodge(width = 1))

enter image description here


1
亲爱的TarJae, 非常感谢您的超快速指导。事实上,您的代码是一种非常整洁的方式,甚至可以进行更多的自定义。所以我从您那里得到了一些灵感,并提出了一个透明而不是灰色着色的选项。请参见上面的编辑。 - Arno

2
这是一个使用gghighlight包和一些有限的hacky代码的解决方案。
阅读vignette时,我注意到该包的作者“过滤掉”没有高亮的数据。如果您将突出显示的绘图保存在p_h中,然后查看p_h$data,则组B的值已经消失了。
library(tidyverse)
library(gghighlight)

p_h <- ggplot(data = df, aes(x = group, y = mean, fill = value)) +
  geom_bar(stat = "identity", position = "dodge") +
  gghighlight(group != "B",
              label_key = group) +
  geom_text(aes(label = round(mean, digits = 2)), 
            stat= "identity", 
            vjust = -.5,
            position = position_dodge(width = .9)) 

> p_h$data
  group   value  mean
1     A value_1 1.331
2     A value_2 1.931
5     C value_1 4.631
6     C value_2 3.331

如果我们在调用 gghighlight() 删除数据后重新插入它们,那么geom_text() 将能够再次找到组B的均值
可以使用以下代码“恢复”数据并重新插入它们:
### create a ggplot object with the original complete data
### you could check that with p_to_copy_data$data
p_to_copy_data <- ggplot(data = df)

### copy the complete data to your highlighted plot data section
p_h$data <- p_to_copy_data$data
p_h

这将产生以下图表:

enter image description here


亲爱的KoenV,非常感谢您的快速帮助和深入研究gghighlight。您在包中提供的解决方案正是我最初寻找的。有了TarJae的可能性以及原始问题中的补充,现在应该涵盖了相当多的可能性。太棒了。 - Arno

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接