R中直方图的颜色框 [英] colour bins of histogram in R

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

问题描述

我想绘制一张如图所示的巴洛克图。我希望每个bin都根据其他列中的值的总和进行着色。


  library(reshape)
library(ggplot2)

值=复制(4,DIFF(C(0,排序(runif(92)),1)))
colnames(值)= C( A, b, C,d )
counts = sample(10:100,93,replace = T)
df = data.frame(cbind(values,count= counts))
mdf = melt(df (bCount = cut(count,breaks = seq(0,100,by = 5)))$ b(b = 1),

mdf = mdf%>%
mutate


$ b plot = ggplot(mdf)+
geom_bar(aes(x = binCounts,fill = variable))+
theme(axis.text.x = element_text(角度= 90,hjust = 1))

打印(曲线)



我想在y轴上计数。对于每个栏,我想绘制列A B C和D列中数据的比例。然而,使用上面的代码,它倾向于绘制变量的数量而不是总和。

解决方案

  mdf%>%
ungroup()%>%
mutate(binCounts = cut(count,breaks = seq(0,100,by = 5)))%>%
group_by(binCounts,variable)%>%
summary( count = sum(count))%>%
ggplot(aes(x = binCounts,y = count,fill = variable))+
geom_col()+
theme(axis.text .x = element_text(angle = 90,hjust = 1))


I want to plot a barplot like in figure. I want each bin to be coloured based on the sum of the value in the other columns. . I made a reproducible example here.

library(reshape)
library(ggplot2)

values= replicate(4, diff(c(0, sort(runif(92)), 1)))
 colnames(values) = c("A","B","C","D")
 counts = sample(10:100, 93, replace=T)
 df = data.frame(cbind(values,"count"=counts))
 mdf = melt(df,id="count")

 mdf = mdf %>%
  mutate(binCounts = cut(count, breaks = seq(0, 100, by = 5)))



  plot = ggplot(mdf) +
  geom_bar(aes(x=binCounts, fill=variable)) +
  theme(axis.text.x=element_text(angle = 90, hjust=1))

print(plot)

I want the count on the y axis. For each bar I want to plot the proportion of the data from the column A B C and D. However with the above code it tends to plot the count of the variable rather than the sum.

解决方案

mdf %>%
  ungroup() %>% 
  mutate(binCounts = cut(count, breaks = seq(0, 100, by = 5))) %>% 
  group_by(binCounts,variable) %>% 
  summarise(count = sum(count)) %>% 
  ggplot(aes(x=binCounts,y = count, fill=variable)) +
  geom_col() +
  theme(axis.text.x=element_text(angle = 90, hjust=1))

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

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