本文介绍了使用stat_summary居中标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多关于使用geom_text进行此操作的文章,但是没有关于如何使用stat_summary进行此操作的文章.我尝试添加到文本"标签上,position = position_stack(0.5),但它仅返回无法显示此视觉效果".

I see a lot of posts on doing this with geom_text, but not how to do this with stat_summary. I try adding on to the "text" label,position = position_stack(0.5), but it just returns "cannot display this visual".

library(ggplot2)

汇总数据

ag<- aggregate(dataset$Value, by = list(PvA=dataset$PvA, Area=dataset$Area, Unit=dataset$Unit), FUN=sum)
ag

绘图

b<-ggplot(dataset, aes(x=PvA, y=Value, fill=Attribute)) +
     stat_summary_bin(aes(fill=Attribute), fun.y = sum, geom="bar", position="stack", color="black")+
     stat_summary_bin(aes(label=..y..), fun.y=sum,  geom="text", color = "white", data=subset(dataset, Value >1))
b

数据集示例(链接至Dropbox下载,txt文件)

Dataset Sample (link to Dropbox download, txt file)

推荐答案

实际上,您所拥有的比看起来所需的要复杂得多.我将使用 mpg 数据集的一个子集进行说明,而不是下载您的数据集.

What you've got is actually more complicated than it seems like it needs to be. I'll illustrate with a subset of the mpg dataset, rather than downloading yours.

您无需在 stat_summary_bin 中计算简单的摘要,而是可以在绘制之前自己进行操作(可以控制发生的事情),也可以让 geom_bar 进行操作. geom_bar 类似于 geom_col ,但它内部运行的是 stat_count .如果在 geom_text 中使用相同的统计信息,则可以访问此计算出的计数以创建标签.然后将标签居中,而不使用速记 position ="stack" ,而是使用 position = position_stack(vjust = 0.5).

Instead of calculating simple summaries in stat_summary_bin, you can do them yourself before plotting (gives you control over what's going on), or you can have geom_bar do it. geom_bar is like geom_col, except it internally runs a stat_count. If you use the same stat in geom_text, you can access this calculated count to make labels. Then to center the labels, instead of the shorthand position = "stack", you use position = position_stack(vjust = 0.5).

请注意, stat(count) .count .. 的替代,简称为 calc(count).我正在使用github的dev版本;CRAN版本可能仍然是 .. count .. 表示法.

Note that stat(count) is the replacement to ..count.., which was briefly called calc(count). I'm using the dev version from github; the CRAN version might still be ..count.. notation.

library(tidyverse)

df <- mpg %>% filter(manufacturer %in% c("chevrolet", "dodge"))

ggplot(df, aes(x = manufacturer, fill = class)) +
  geom_bar(position = "stack") +
  geom_text(aes(label = stat(count)), stat = "count", position = position_stack(vjust = 0.5))

第二种方法(我的偏好是因为它提供了更多控制权)是计算计数,然后将其通过管道传输到 ggplot 中.调用 count 可以使您:

The second way—my preference because it gives a little more control—is to calculate the counts and then pipe it into ggplot. Calling count gets you:

df %>%
  count(manufacturer, class)
#> # A tibble: 6 x 3
#>   manufacturer class       n
#>   <chr>        <chr>   <int>
#> 1 chevrolet    2seater     5
#> 2 chevrolet    midsize     5
#> 3 chevrolet    suv         9
#> 4 dodge        minivan    11
#> 5 dodge        pickup     19
#> 6 dodge        suv         7

然后,您可以将其传送到简化的 ggplot 调用中.除了 count 被标记为 n .

Then you can pipe that into a simplified ggplot call. This gets the same plot, except that count will instead be labeled n.

df %>%
  count(manufacturer, class) %>%
  ggplot(aes(x = manufacturer, y = n, fill = class)) +
    geom_col(position = "stack") +
    geom_text(aes(label = n), position = position_stack(vjust = 0.5))

这篇关于使用stat_summary居中标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:33
查看更多