ggplot2的彩色直方图 [英] Colored histogram with ggplot2

查看:305
本文介绍了ggplot2的彩色直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有值及其关联权重的数据框.我想制作一个直方图,以便每个条形图的高度对应于该bin中值的数量,而条形图的颜色对应于它们的总重量.我该怎么办?

I have a data frame with values and their associated weights. I want to make a histogram, such that each bar's height corresponds to the number of values in that bin and the bar's color corresponds to their total weight. How do I do that?

示例:

D <- data.frame(
    x = c(-0.39, 0.12, 0.94, 1.67, 1.76, 2.44, 3.72, 4.28, 4.92, 5.53, 0.06,
          0.48, 1.01, 1.68, 1.80, 3.25, 4.12, 4.60, 5.28, 6.22),
    w = c(0.1810479, 0.2209460, 0.2974134, 0.3768152, 0.3871925, 0.4682943,
          0.6220371, 0.6838944, 0.7473117, 0.7993555, 0.2159428, 0.2526883,
          0.3046069, 0.3779629, 0.3918383, 0.5667588, 0.6667623, 0.7166747,
          0.7790540, 0.8480375))

ggplot(D, aes(x)) +
    geom_histogram(aes(y=..density..), binwidth=0.5, boundary=0.5)

解决方案

基于eipi10的答案,但使用标准功能:

Solution

Based on eipi10's answer, but using standard functions:

breaks <- seq(-0.5, 6.5, 0.5)
bins   <- cut(D$x, breaks)

h <- data.frame(
    x      = head(breaks, -1) + 0.25,
    count  = sapply(split(D$x, bins), length),
    weight = sapply(split(D$w, bins), sum))
h$density <- h$count / sum(h$count)

ggplot(h) + geom_bar(aes(x, density, fill=weight), stat='identity')

推荐答案

另一个选择是预先汇总数据:

Another option is to pre-summarise the data:

library(dplyr)

D_bins = D %>% 
  mutate(bins = cut(x, seq(-0.5,6.5,0.5), labels=seq(-0.25,6.5,0.5)),
         bins = as.numeric(as.character(bins()))) %>%
  group_by(bins) %>%
  summarise(count_x = n(),
            sum_w = sum(w))

ggplot(D_bins) +
  geom_bar(aes(bins, count_x, fill=sum_w), colour="white", stat="identity") 

您还可以使用两组相对的条,而不是填充美学:

You could also use two sets of opposing bars, rather than a fill aesthetic:

ggplot(D_bins) +
  geom_bar(aes(bins, count_x), colour="white", fill="blue", stat="identity") +
  geom_bar(aes(bins, -sum_w), colour="white", fill="red", stat="identity") +
  scale_x_continuous(breaks=-1:10) +
  scale_y_continuous(limits=c(-2,4), breaks=seq(-2,5,1), labels=c(2,1,0:5)) +
  labs(y = c("Sum of w                       Count of x            ")) +
  coord_flip()

这篇关于ggplot2的彩色直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆