我想绘制一个直方图,其中包含变量计数的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应该有四个分类变量。
答案 0 :(得分: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)
答案 1 :(得分:0)
我在这篇文章的帮助下找到了答案。我希望绘图中的每个条形为(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))