1

vector of animals

ah<- c("Dog","Cat","Horse","Monkey","Dog","Fish","Horse","Dog","Cat","Horse","Cat","Horse","Dog","Cat","Monkey","Horse","Dog","Cat","Horse","Dog")

h gives status of animals 1= healthy 0 = not healthy

h<-c(1,0,1,0,0,0,1,0,1,0,1,0,0,1,1,1,1,1,0,0)

histogram plot

data<- data.frame(animals=ah,healthy=h)
ggplot(data,aes(animals,fill=as.factor(h)))+geom_histogram(stat="count")

i want to label with percentage of healthy animal (eg:dog) on top of each bar and in each bar i want label eg: number dogs healthy and not healthy

enter image description here

i need to plot Some thing like this

enter image description here

premon
  • 159
  • 1
  • 3
  • 13

1 Answers1

3

First you need to generate a summary table of your data with counts. I am using the dplyr functions here:

library(dplyr)
tabDat <- data %>% group_by(animals, healthy) %>% 
               summarise(count = n()) %>% ungroup %>% 
               tidyr::complete(animals, healthy, fill = list(count = 0)) 

complete from tidyr is used to assure that we have all possible combinations between animals and healthy.

Then you have to define the y positions for the text. For healthy == 1 it is simply half of the size of the bar and for healthy == 0 it is half of the bar + the bar height for healthy == 1 for the counts, and cumulative bar seizes for the percentages:

(tabDatY <- left_join(tabDat, tabDat %>% filter(healthy == 1) %>% 
              select(animals, y.basis = count), by = "animals") %>%
              group_by(animals) %>%
              mutate(y     = ifelse(healthy == 1, count / 2, y.basis + count / 2),
                     perc  = count / sum(count),
                     y.sum = sum(count)))

# Source: local data frame [10 x 7]
# Groups: animals [5]

#    animals healthy count y.basis     y      perc y.sum
#     <fctr>   <dbl> <dbl>   <dbl> <dbl>     <dbl> <dbl>
# 1      Cat       0     1       4   4.5 0.2000000     5
# 2      Cat       1     4       4   2.0 0.8000000     5
# 3      Dog       0     4       2   4.0 0.6666667     6
# 4      Dog       1     2       2   1.0 0.3333333     6
# 5     Fish       0     1       0   0.5 1.0000000     1
# 6     Fish       1     0       0   0.0 0.0000000     1
# 7    Horse       0     3       3   4.5 0.5000000     6
# 8    Horse       1     3       3   1.5 0.5000000     6
# 9   Monkey       0     1       1   1.5 0.5000000     2
# 10  Monkey       1     1       1   0.5 0.5000000     2

Now you can use this data frame to plot your labels:

gg <- ggplot(data, aes(animals)) + 
         geom_histogram(aes(fill = as.factor(healthy)), stat = "count")
gg + geom_text(aes(y = y, label = count), data = tabDatY) +
     geom_text(aes(y = y.sum + .2, label = paste0(round(perc * 100), "%")),
               data = tabDatY %>% filter(healthy == 1))

enter image description here

thothal
  • 16,690
  • 3
  • 36
  • 71