如何使用ggplot2在R中的geom_bar上放置标签

发布于 2024-11-16 23:40:33 字数 875 浏览 0 评论 0原文

我想要将一些标签堆叠在 geom_bar 图表的顶部。这是一个示例:

df <- data.frame(x=factor(c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE)))
ggplot(df) + geom_bar(aes(x,fill=x)) + opts(axis.text.x=theme_blank(),axis.ticks=theme_blank(),axis.title.x=theme_blank(),legend.title=theme_blank(),axis.title.y=theme_blank())

现在

表格(df$x)

FALSE  TRUE 
    3     5 

我希望将 3 和 5 放在两个栏的顶部。如果我也能得到百分比值就更好了。例如3 (37.5%)5 (62.5%)。就像这样: < /a>
(来源:
skitch.com

这可能吗?如果是这样,怎么办?

I'd like to have some labels stacked on top of a geom_bar graph. Here's an example:

df <- data.frame(x=factor(c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE)))
ggplot(df) + geom_bar(aes(x,fill=x)) + opts(axis.text.x=theme_blank(),axis.ticks=theme_blank(),axis.title.x=theme_blank(),legend.title=theme_blank(),axis.title.y=theme_blank())

Now

table(df$x)

FALSE  TRUE 
    3     5 

I'd like to have the 3 and 5 on top of the two bars. Even better if I could have the percent values as well. E.g. 3 (37.5%) and 5 (62.5%). Like so:

(source: skitch.com)

Is this possible? If so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

简单气质女生网名 2024-11-23 23:40:33

要在ggplot上绘制文本,您可以使用geom_text。但我发现首先使用 ddply 汇总数据很有帮助,

dfl <- ddply(df, .(x), summarize, y=length(x))
str(dfl)

因为数据是预先汇总的,因此您需要记住将 stat="identity" 参数添加到 <代码>geom_bar:

ggplot(dfl, aes(x, y=y, fill=x)) + geom_bar(stat="identity") +
    geom_text(aes(label=y), vjust=0) +
    opts(axis.text.x=theme_blank(),
        axis.ticks=theme_blank(),
        axis.title.x=theme_blank(),
        legend.title=theme_blank(),
        axis.title.y=theme_blank()
)

在此处输入图像描述

To plot text on a ggplot you use the geom_text. But I find it helpful to summarise the data first using ddply

dfl <- ddply(df, .(x), summarize, y=length(x))
str(dfl)

Since the data is pre-summarized, you need to remember to change add the stat="identity" parameter to geom_bar:

ggplot(dfl, aes(x, y=y, fill=x)) + geom_bar(stat="identity") +
    geom_text(aes(label=y), vjust=0) +
    opts(axis.text.x=theme_blank(),
        axis.ticks=theme_blank(),
        axis.title.x=theme_blank(),
        legend.title=theme_blank(),
        axis.title.y=theme_blank()
)

enter image description here

蓦然回首 2024-11-23 23:40:33

与 ggplot 中的许多任务一样,一般策略是将您想要添加到图中的内容放入数据框中,以使变量与图中的变量和美观相匹配。例如,您将创建一个如下所示的新数据框:

dfTab <- as.data.frame(table(df))
colnames(dfTab)[1] <- "x"
dfTab$lab <- as.character(100 * dfTab$Freq / sum(dfTab$Freq))

以便 x 变量与 df 中的相应变量匹配,依此类推。然后,您只需使用geom_text包含它:

ggplot(df) + geom_bar(aes(x,fill=x)) + 
    geom_text(data=dfTab,aes(x=x,y=Freq,label=lab),vjust=0) +
    opts(axis.text.x=theme_blank(),axis.ticks=theme_blank(),
        axis.title.x=theme_blank(),legend.title=theme_blank(),
        axis.title.y=theme_blank())

此示例将仅绘制百分比,但您也可以通过类似以下方式将计数粘贴在一起:

dfTab$lab <- paste(dfTab$Freq,paste("(",dfTab$lab,"%)",sep=""),sep=" ")

请注意,在当前版本中ggplot2 中,opts 已弃用,因此我们现在使用 themeelement_blank

As with many tasks in ggplot, the general strategy is to put what you'd like to add to the plot into a data frame in a way such that the variables match up with the variables and aesthetics in your plot. So for example, you'd create a new data frame like this:

dfTab <- as.data.frame(table(df))
colnames(dfTab)[1] <- "x"
dfTab$lab <- as.character(100 * dfTab$Freq / sum(dfTab$Freq))

So that the x variable matches the corresponding variable in df, and so on. Then you simply include it using geom_text:

ggplot(df) + geom_bar(aes(x,fill=x)) + 
    geom_text(data=dfTab,aes(x=x,y=Freq,label=lab),vjust=0) +
    opts(axis.text.x=theme_blank(),axis.ticks=theme_blank(),
        axis.title.x=theme_blank(),legend.title=theme_blank(),
        axis.title.y=theme_blank())

This example will plot just the percentages, but you can paste together the counts as well via something like this:

dfTab$lab <- paste(dfTab$Freq,paste("(",dfTab$lab,"%)",sep=""),sep=" ")

Note that in the current version of ggplot2, opts is deprecated, so we would use theme and element_blank now.

树深时见影 2024-11-23 23:40:33

另一种解决方案是在处理离散变量时使用 stat_count()(对于连续变量则使用 stat_bin())。

ggplot(data = df, aes(x = x)) +
geom_bar(stat = "count") + 
stat_count(geom = "text", colour = "white", size = 3.5,
aes(label = ..count..),position=position_stack(vjust=0.5))

输入图像描述这里

Another solution is to use stat_count() when dealing with discrete variables (and stat_bin() with continuous ones).

ggplot(data = df, aes(x = x)) +
geom_bar(stat = "count") + 
stat_count(geom = "text", colour = "white", size = 3.5,
aes(label = ..count..),position=position_stack(vjust=0.5))

enter image description here

温柔一刀 2024-11-23 23:40:33

所以,这是我们的初始图↓

library(ggplot2)

df <- data.frame(x=factor(c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE)))

p <- ggplot(df, aes(x = x, fill = x)) +
  geom_bar()
p

initial barplot without labels

正如yuan-ning,我们可以使用stat_count()

默认情况下,geom_bar() 使用stat_count()。正如 ggplot2 参考中所述,stat_count()< /code> 返回两个值:count 表示 bin 中的点数,prop 表示分组比例。由于我们的组与 x 值匹配,因此两个 prop 均为 1 并且没有用。但我们可以在 geom_text() 中使用 count(称为“..count..”)来实际表示条形高度。请注意,我们还必须将“stat = 'count'”包含到我们的 geom_text() 调用中。

由于我们希望标签中同时包含计数和百分比,因此我们需要在“标签”美学中进行一些计算和字符串粘贴,而不仅仅是“..count..”。我更喜欢添加一行代码来从“scales”包(与“ggplot2”一起提供)创建一个包装百分比格式化函数。

pct_format = scales::percent_format(accuracy = .1)

p <- p + geom_text(
    aes(
      label = sprintf(
        '%d (%s)',
        ..count..,
        pct_format(..count.. / sum(..count..))
      )
    ),
    stat = 'count',
    nudge_y = .2,
    colour = 'royalblue',
    size = 5
  )
p

barplot with labels

当然,您可以使用 coloursize 进一步编辑标签、轻推、调整等,或者如果需要,可以使用 glue::glue() 而不是 sprintf()

UPD: ggplot2 作者现在坚持使用after_stat(something) 而不是通常的 ..something.. 表示法。因此,第二部分(添加标签)的最新版本现在有点庞大,但产生完全相同的结果:

pct_format = scales::percent_format(accuracy = .1)

p <- p + geom_text(
  aes(
    label = sprintf(
      '%d (%s)',
      after_stat(count),
      pct_format(after_stat(count) / sum(after_stat(count)))
    )
  ),
  stat = 'count',
  nudge_y = .2,
  colour = 'royalblue',
  size = 5
)

So, this is our initial plot↓

library(ggplot2)

df <- data.frame(x=factor(c(TRUE,TRUE,TRUE,TRUE,TRUE,FALSE,FALSE,FALSE)))

p <- ggplot(df, aes(x = x, fill = x)) +
  geom_bar()
p

initial barplot without labels

As suggested by yuan-ning, we can use stat_count().

geom_bar() uses stat_count() by default. As mentioned in the ggplot2 reference, stat_count() returns two values: count for number of points in bin and prop for groupwise proportion. Since our groups match the x values, both props are 1 and aren’t useful. But we can use count (referred to as “..count..”) that actually denotes bar heights, in our geom_text(). Note that we must include “stat = 'count'” into our geom_text() call as well.

Since we want both counts and percentages in our labels, we’ll need some calculations and string pasting in our “label” aesthetic instead of just “..count..”. I prefer to add a line of code to create a wrapper percent formatting function from the “scales” package (ships along with “ggplot2”).

pct_format = scales::percent_format(accuracy = .1)

p <- p + geom_text(
    aes(
      label = sprintf(
        '%d (%s)',
        ..count..,
        pct_format(..count.. / sum(..count..))
      )
    ),
    stat = 'count',
    nudge_y = .2,
    colour = 'royalblue',
    size = 5
  )
p

barplot with labels

Of course, you can further edit the labels with colour, size, nudges, adjustments etc. or use glue::glue() instead of sprintf() if you want.

UPD: The ggplot2 authors now insist on using after_stat(something) instead of the usual ..something.. notation. Therefore, the up-to-date version of the second part (adding labels) is now a bit more bulky, but produces the exact same result:

pct_format = scales::percent_format(accuracy = .1)

p <- p + geom_text(
  aes(
    label = sprintf(
      '%d (%s)',
      after_stat(count),
      pct_format(after_stat(count) / sum(after_stat(count)))
    )
  ),
  stat = 'count',
  nudge_y = .2,
  colour = 'royalblue',
  size = 5
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文