2

我想绘制患有特定疾病的患者样本的一些临床特征。有四个变量是二分法的,如果其中任何一个因具有攻击性而为真,则患者被标记为具有攻击性病程。一次只做一个变量意味着我们可以使用堆叠或闪避的条形图。我们甚至可以使用饼图。但是在单个图表上显示所有变量和组合更具挑战性。

我创建了一些虚拟数据(只有三个特征+复合)。我无法相信我必须通过多少次操作才能绘制出我想要的数据。我遇到了所有存在的问题。每个问题都需要更多的操作。当我寻找答案时(例如在 stackoverflow 上),我什么也找不到,可能是因为我不知道流行语是什么来描述我正在尝试做的事情。

问题
1) 我正在尝试做的事情的流行语是什么
2) 真的需要这么难吗,或者 ggplot2 中是否有更直接的路线可以让我从包含 as 的原始数据文件直接进入图表有很多行,因为有人类受试者

创建了一些模拟数据

require(data.table)
aggr.freq <- sample(c(TRUE, FALSE), size=100, replace=TRUE, prob=c(0.1, 0.9) )
aggr.count <- sample(c(TRUE, FALSE), size=100, replace=TRUE, prob=c(0.2, 0.8) )
aggr.spread <- sample(c(TRUE, FALSE), size=100, replace=TRUE, prob=c(0.4, 0.6) )
human.subjects <- data.table(aggr.freq, aggr.count, aggr.spread)
human.subjects[,aggr.course.composite:=aggr.freq|aggr.count|aggr.spread]

理清事实

aggr.true  <-  human.subjects [,list(aggr.freq = sum(aggr.freq), aggr.count = sum(aggr.count), aggr.spread = sum(aggr.spread), aggr.course.composite= sum(aggr.course.composite))]

该计数的绘图方向错误

aggr.true.vertical <- data.table(t(aggr.true))
aggr.true.vertical[,clinical.characteristic:=factor(dimnames(t(aggr.true))[[1]], ordered=TRUE, levels= c("aggr.freq", "aggr.count", "aggr.spread", "aggr.course.composite"))]#have to specify levels otherwise ggplot2 will plot the variables in alphabetical order
setnames(x=aggr.true.vertical, old = "V1", new = "aggressive")
aggr.true.vertical[,indolent:=human.subjects[,.N]-aggressive]#we had the tally of trues now we need to tall the falses


ggplot(aggr.true.vertical, aes(x=clinical.characteristic, y=aggressive)) + geom_bar(stat="identity") # alas, this graph only shows the count of those with an aggressive characteristic and does not give the reader a feel for the proportion.

第二次重塑

require(reshape2)
long <- melt(aggr.true.vertical, variable.name="aggressiveness",value.name="count")
ggplot(long, aes(x=clinical.characteristic, y=count, fill=aggressiveness)) + geom_bar(stat="identity")  

谢谢。

4

1 回答 1

3

我想我可以看到你对问题的思考方式发生了什么,但我认为你在这个过程的早期“走错了方向”。我不确定我是否可以帮助您搜索要搜索的关键字。无论如何,您只需要一次融化,然后您就可以进行绘图了。数据生成后:

human.subjects$id<-1:nrow(human.subjects) # Create an id variable (which you probably have)
melted.humans<-melt(human.subjects,id='id') 
ggplot(melted.humans, aes(x=variable,fill=value)) + geom_bar()  

在此处输入图像描述

也许你更愿意颠倒真假的顺序,但你明白了。

此外,您可能对您正在做的其他部分的一些简化代码感兴趣,即计算真假。(在我的解决方案中,我只是让它ggplot去做。)

# Count the trues:
sapply(human.subjects,sum)

# Collect all the trues and falses into a single matrix,
# by running table on each column.
sapply(human.subjects,table)
于 2013-07-25T23:22:04.787 回答