0

我想用变量计数的 ggplot 绘制直方图。但是,我希望每个条形都显示第二个(分类)变量的相对分数。

例如,四个变量的总和始终为 1。我想根据计数变量绘制直方图。

library(reshape)
library(ggplot2)

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



ggplot(mdf, aes(count,fill=variable)) +
  geom_histogram(alpha=0.3, 
   position="identity",lwd=0.2,binwidth=5,boundary=0)

我希望根据列的相对分数(A,B,C,D)对直方图的每个条进行着色。所以每个 bin 应该有四个分类变量。

4

2 回答 2

1

我认为这就是你想要的(我也使用了 dplyr 包):

library(reshape2)
library(ggplot2)
library(dplyr)

set.seed(2)
values= replicate(4, diff(c(0, sort(runif(92)), 1)))
colnames(values) = c("A","B","C","D")
counts = sample(1: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))) %>%
  group_by(binCounts) %>%
  mutate(sumVal = sum(value)) %>%
  ungroup() %>%
  group_by(binCounts, variable) %>%
  summarise(prct = sum(value)/mean(sumVal))

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

print(plot)

在此处输入图像描述

于 2018-04-23T13:43:49.360 回答
1

我在这篇文章中的其他人的帮助下找到了答案。我希望图中的每个条都作为(A,B,C,D)中变量的分数。尽管代码并不优雅。可能对某人有帮助! 在此处输入图像描述

library(reshape2)
library(ggplot2)
library(dplyr)

##generate the random variables that sum to 1 for each columns
values <- matrix(runif(100*4),nrow=100) 
S <- apply(values,1,sum); values = values/S 
colnames(values) = c("A","B","C","D")
set.seed(2)
counts = sample(1:100, 100, replace=T)

##frequency of the data in binwidth of 5
table = hist(counts,breaks=seq(0, 100, by = 5),plot=F)$counts

##create a dataframe
df = data.frame(cbind(values,"count"=counts))


breaks = seq(5, 100, by = 5)
newdf = do.call("rbind",lapply(as.numeric(breaks), function(x) apply(df[which(df$count < x),][,1:4],2,sum)))
newdf = melt(sweep(newdf, 1, rowSums(newdf), FUN="/") * table)
colnames(newdf) = c("bins","variable","value")
ggplot(newdf) +
  geom_bar(aes(x=bins, y=value, fill=variable), stat="identity") +
  theme(axis.text.x=element_text(angle = 90, hjust=1))
于 2018-04-23T18:12:00.343 回答