如何使用ggplot在直方图中标记离散的一个变量?

时间:2017-05-18 11:39:41

标签: r ggplot2 bar-chart

动物载体

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

h给出动物的状态1 =健康0 =不健康

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

直方图

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

我想在每个酒吧的顶部标注健康动物(例如:狗)的百分比,并且在每个酒吧我想要标签,例如:数字狗健康而不健康

enter image description here

我需要绘制一些像这样的东西

enter image description here

1 个答案:

答案 0 :(得分:2)

首先,您需要使用计数生成数据汇总表。我在这里使用BufferedWriter函数:

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

complete用于确保我们在tidyranimals之间拥有所有可能的组合。

然后你必须定义文本的healthy位置。对于y,它只是条形大小的一半,对于healthy == 1,它是条形的一半+ healthy == 0的条形高度,以及百分比的累积条形图:

healthy == 1

现在您可以使用此数据框来绘制标签:

(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

enter image description here