简体   繁体   English

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

[英]How to put labels over geom_bar in R with ggplot2

I'd like to have some labels stacked on top of a geom_bar graph.我想在geom_bar图上堆叠一些标签。 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)表(df$x)

FALSE  TRUE 
    3     5 

I'd like to have the 3 and 5 on top of the two bars.我想将 3 和 5 放在两个酒吧的顶部。 Even better if I could have the percent values as well.如果我也能拥有百分比值,那就更好了。 Eg 3 (37.5%) and 5 (62.5%) .例如3 (37.5%)5 (62.5%) Like so:像这样:
(source: skitch.com ) (来源: skitch.com

Is this possible?这可能吗? If so, how?如果是这样,如何?

To plot text on a ggplot you use the geom_text .要在ggplot上绘制文本,请使用geom_text But I find it helpful to summarise the data first using ddply但我发现首先使用ddply汇总数据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 :由于数据是预先汇总的,您需要记住更改添加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()
)

在此处输入图片说明

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.与 ggplot 中的许多任务一样,一般策略是以某种方式将您想要添加到图中的内容放入数据框中,以使变量与图中的变量和美学相匹配。 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.以便x变量与df中的相应变量匹配,依此类推。 Then you simply include it using geom_text :然后您只需使用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:此示例将仅绘制百分比,但您也可以通过以下方式将计数paste在一起:

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.请注意,在当前版本的 ggplot2 中,不推荐使用opts ,因此我们现在将使用themeelement_blank

Another solution is to use stat_count() when dealing with discrete variables (and stat_bin() with continuous ones).另一种解决方案是在处理离散变量时使用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))

在此处输入图片说明

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

没有标签的初始条形图

As suggested by yuan-ning , we can use stat_count() .正如stat_count() -ning所建议的,我们可以使用stat_count()

geom_bar() uses stat_count() by default. geom_bar()默认使用stat_count() As mentioned in the ggplot2 reference , stat_count() returns two values: count for number of points in bin and prop for groupwise proportion.正如 ggplot2 参考中提到的stat_count()返回两个值: count表示 bin 中的点数, prop表示分组比例。 Since our groups match the x values, both prop s are 1 and aren't useful.由于我们的组匹配 x 值,因此两个prop都是 1 并且没有用。 But we can use count (referred to as “..count..”) that actually denotes bar heights, in our geom_text() .但是我们可以在geom_text()中使用实际表示条形高度的count (称为“..count..” geom_text() Note that we must include “stat = 'count'” into our geom_text() call as well.请注意,我们还必须在geom_text()调用中包含“stat = 'count'”。

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..”.由于我们需要标签中的计数和百分比,因此我们需要在“标签”美学中进行一些计算和字符串粘贴,而不仅仅是“..count..”。 I prefer to add a line of code to create a wrapper percent formatting function from the “scales” package (ships along with “ggplot2”).我更喜欢添加一行代码来从“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

带标签的条形图

Of course, you can further edit the labels with colour , size , nudges, adjustments etc.当然,您可以进一步编辑带有coloursize 、微调、调整等的标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM