2014-10-26 60 views
0

我有一個數據集,其中包含一個年齡欄和一個肺容量相應的列。我怎樣才能創建一個直方圖來顯示肺活量在年齡方面的分佈?R給定變量或數據列的直方圖密度

下面是數據的例子。其實,我想比較那些誰不與那些誰抽菸分佈:

Caes Age Gender Smoke Height FEV 

0 16 1 0 64.8 2.65 

0 12 0 0 60.5 2.27 

1 19 1 0 71.7 4.29 

0 15 0 0 64.8 2.52 
+0

更多信息,得到一個很好的答案。你能否描述你的數據 - 是肺還是年齡分類?你可以創建一些模擬數據,並顯示你已經嘗試過的代碼。 – user20650 2014-10-26 21:23:41

回答

1

感謝您的回覆。我意識到我想要一個barplot而不是一個直方圖。這裏是我想出瞭解決方案:

smoke=read.csv("SmokingEffect.csv",header=TRUE) 
smokes=subset(smoke,select=c(Age,Smoke,FEV)) 
library(plyr) 
smokesmeans <- ddply(smokes, c("Age","Smoke"), summarize, mean=mean(FEV), 
sem=sd(FEV)/sqrt(length(FEV))) 
smokesmeans <- transform(smokesmeans, lower=mean-sem, upper=mean+sem) 
smokesmeans[,2] <- sapply(smokesmeans[,2], as.character) 
library(ggplot2) 
plotation <- qplot(x=Age, y=mean, fill=Smoke, data=smokesmeans, 
geom="bar",stat="identity",position="dodge",main="distribution of FEV", 
ylab="mean FEV") 
plotation <- plotation + geom_errorbar(aes(ymax=upper, 
ymin=lower), position=position_dodge(0.9), data=smokesmeans) 
png(myplot.png) 
plotation 
dev.off() 

輸出看起來是這樣的:需要在你的問題

enter image description here

1

,當你有一個載體(如肺容量)直方圖通常使用,並要顯示值的分佈:

library(ggplot2) 
foo <- data.frame(age=runif(1000,min=10,max=50), capacity=rnorm(1000,mean=10)) 
ggplot(foo, aes(capacity))+geom_histogram(fill="blue") 

enter image description here

如果你想繪製兩個變量之間的關係,散點圖可能是一個更好的選擇:

ggplot(foo, aes(age, capacity))+geom_point(color="blue") 

enter image description here