如何使用ggplot2在R中在geom_bar上添加标签

时间:2021-07-27 13:47:03

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())

如何使用ggplot2在R中在geom_bar上添加标签

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. Even better if I could have the percent values as well. E.g. 3 (37.5%) and 5 (62.5%). Like so: 如何使用ggplot2在R中在geom_bar上添加标签

我想把3和5放在这两根杠上面。如果我也有百分数,那就更好了。例3(37.5%)和5(62.5%)。像这样:

Is this possible? If so, how?

这是可能的吗?如果是这样,如何?

2 个解决方案

#1


30  

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:

与ggplot中的许多任务一样,一般策略是将您想要添加到绘图中的内容以某种方式放入数据框架,以便变量与您的绘图中的变量和美学相匹配。例如,你要创建一个新的数据框架

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:

所以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())

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.

注意,在当前版本的ggplot2中,不赞成使用opts,因此我们现在将使用theme和element_blank。

#2


38  

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

要在ggplot上绘制文本,可以使用geom_text。但是我发现首先用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"参数添加到地_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()
)

如何使用ggplot2在R中在geom_bar上添加标签

#1


30  

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:

与ggplot中的许多任务一样,一般策略是将您想要添加到绘图中的内容以某种方式放入数据框架,以便变量与您的绘图中的变量和美学相匹配。例如,你要创建一个新的数据框架

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:

所以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())

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.

注意,在当前版本的ggplot2中,不赞成使用opts,因此我们现在将使用theme和element_blank。

#2


38  

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

要在ggplot上绘制文本,可以使用geom_text。但是我发现首先用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"参数添加到地_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()
)

如何使用ggplot2在R中在geom_bar上添加标签