首页 > 解决方案 > ggplot 条形图中的标签问题

问题描述

我目前有一个结构如下的数据框:

  Establishment.date Species  Shade.Tol         Ele    Kipuka
1                1980  PSEMEN Intolerant Under 1050m On Kipuka
2                1981  PINCON Intolerant Above 1050m On Kipuka
3                1981  ABIPRO Intolerant Under 1050m On Kipuka
4                1981  ABIPRO Intolerant Under 1050m On Kipuka
5                1981  ABILAS   Tolerant Above 1050m On Kipuka
6                1982  ABILAS   Tolerant Above 1050m On Kipuka
7                1983  PSEMEN Intolerant Under 1050m On Kipuka
8                1984  TSUHET   Tolerant Under 1050m On Kipuka
9                1984  TSUHET   Tolerant Under 1050m On Kipuka
10               1984  PSEMEN Intolerant Under 1050m On Kipuka
11               1984  PINCON Intolerant Under 1050m On Kipuka
12               1984  ABIPRO Intolerant Above 1050m On Kipuka
13               1984  ABIPRO Intolerant Above 1050m On Kipuka

我正在尝试制作一个条形图,以突出显示在高海拔和低海拔地区发生的设施数量,这些设施的数量取决于它们的阴影容差,并将每个类别的数量显示为标签。我目前的方法是过滤数据框以获得一个新的汇总数据框,如下所示

# A tibble: 9 x 4
# Groups:   Establishment.date, Shade.Tol [7]
  Establishment.date Shade.Tol  Ele         count
               <int> <fct>      <fct>       <int>
1               1980 Intolerant Under 1050m     1
2               1981 Intolerant Above 1050m     1
3               1981 Intolerant Under 1050m     2
4               1981 Tolerant   Above 1050m     1
5               1982 Tolerant   Above 1050m     1
6               1983 Intolerant Under 1050m     1
7               1984 Intolerant Above 1050m     2
8               1984 Intolerant Under 1050m     2
9               1984 Tolerant   Under 1050m     2

并将新信息绘制到 ggplot 中,如下所示:

cores_clean %>%
  group_by(Establishment.date,Shade.Tol,Ele) %>%
  summarise(count = n()) %>%
ggplot(aes(x = Ele, y=count, label=count)) +
  geom_bar(stat = "identity",position = "dodge") +
  geom_text(aes(label=count),size = 3)+
  facet_wrap(~ Shade.Tol)+
  #scale_fill_grey()+
  theme_bw() + 
  labs(x = "Elevation Range",
       y = "Count",
       title = "Establishments")+
  theme(plot.title = element_text(hjust = 0.5))

但是当我运行代码时,图形输出会打印出如下堆叠的值行,

在此处输入图像描述

不代表在数据框中找到的那些(n = 740)。我尝试添加geom_text(aes(label=sum(count))),但打印的数字位置相同,观察总数重复多次。不确定我是否错误地过滤了数据或没有正确地将其添加到 ggplot 中。

标签: rggplot2

解决方案


您的主要问题是您group_by(Estabilishment.date),但您似乎甚至不希望在您的图表中使用它。这是stat_summary用于计算总和的一个选项:

cores_clean %>%
  group_by(Establishment.date,Shade.Tol,Ele) %>%
  dplyr::summarise(count = n()) %>%
ggplot(aes(x = Ele, y=count, fill = as.factor(Establishment.date))) +
  geom_bar(stat = "identity") +
  stat_summary(geom = "text", aes(label = ..y.., group = Ele),
               fun = sum, vjust = -0.1) + 
  facet_wrap(~ Shade.Tol) +
  theme_bw() + 
  labs(x = "Elevation Range", y = "Count",
       title = "Establishments", fill = "Year")+
  theme(plot.title = element_text(hjust = 0.5))

在此处输入图像描述

或者,您可以Estabilishment.date从您的中删除group_by并执行以下操作:

cores_clean %>%
  group_by(Shade.Tol,Ele) %>%
  dplyr::summarise(count = n()) %>%
ggplot(aes(x = Ele, y=count)) +
  geom_bar(stat = "identity") +
  stat_summary(geom = "text", aes(label = ..y.., group = Ele),
               fun = sum, vjust = -0.1) + 
  facet_wrap(~ Shade.Tol) +
  theme_bw() + 
  labs(x = "Elevation Range", y = "Count",
       title = "Establishments")+
  theme(plot.title = element_text(hjust = 0.5))

在此处输入图像描述


推荐阅读