Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

r - How to add percentage or count labels above percentage bar plot?

Using ggplot2 1.0.0, I followed the instructions in below post to figure out how to plot percentage bar plots across factors:

Sum percentages for each facet - respect "fill"

test <- data.frame(
     test1 = sample(letters[1:2], 100, replace = TRUE), 
     test2 = sample(letters[3:8], 100, replace = TRUE)
     )
library(ggplot2)
library(scales)
ggplot(test, aes(x= test2, group = test1)) + 
geom_bar(aes(y = ..density.., fill = factor(..x..))) + 
facet_grid(~test1) +
scale_y_continuous(labels=percent)

enter image description here

However, I cannot seem to get a label for either the total count or the percentage above each of the bar plots when using geom_text.

What is the correct addition to the above code that also preserves the percentage y-axis?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Staying within ggplot, you might try

ggplot(test, aes(x= test2,  group=test1)) + 
  geom_bar(aes(y = ..density.., fill = factor(..x..))) +
  geom_text(aes( label = format(100*..density.., digits=2, drop0trailing=TRUE),
                 y= ..density.. ), stat= "bin", vjust = -.5) +
  facet_grid(~test1) +
  scale_y_continuous(labels=percent)

For counts, change ..density.. to ..count.. in geom_bar and geom_text

UPDATE for ggplot 2.x

ggplot2 2.0 made many changes to ggplot including one that broke the original version of this code when it changed the default stat function used by geom_bar ggplot 2.0.0. Instead of calling stat_bin, as before, to bin the data, it now calls stat_count to count observations at each location. stat_count returns prop as the proportion of the counts at that location rather than density.

The code below has been modified to work with this new release of ggplot2. I've included two versions, both of which show the height of the bars as a percentage of counts. The first displays the proportion of the count above the bar as a percent while the second shows the count above the bar. I've also added labels for the y axis and legend.

  library(ggplot2)
  library(scales)
#
# Displays bar heights as percents with percentages above bars
#
    ggplot(test, aes(x= test2,  group=test1)) + 
    geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
    geom_text(aes( label = scales::percent(..prop..),
                   y= ..prop.. ), stat= "count", vjust = -.5) +
    labs(y = "Percent", fill="test2") +
    facet_grid(~test1) +
    scale_y_continuous(labels=percent)
#
# Displays bar heights as percents with counts above bars
#
    ggplot(test, aes(x= test2,  group=test1)) + 
    geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
    geom_text(aes(label = ..count.., y= ..prop..), stat= "count", vjust = -.5) +
    labs(y = "Percent", fill="test2") +
    facet_grid(~test1) +
    scale_y_continuous(labels=percent)

The plot from the first version is shown below.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...