R给定变量或数据列的直方图密度

R给定变量或数据列的直方图密度,r,plot,histogram,R,Plot,Histogram,我有一个数据集,其中一列是年龄,另一列是肺活量。如何创建一个直方图,显示肺容量随年龄的分布 下面是一个数据的示例。实际上,我想比较不吸烟人群和吸烟人群的分布情况: 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 直方图通常用于有单个向量(如肺活量)且希望显示值分布的情况: library(ggplot2) foo &l

我有一个数据集,其中一列是年龄,另一列是肺活量。如何创建一个直方图,显示肺容量随年龄的分布

下面是一个数据的示例。实际上,我想比较不吸烟人群和吸烟人群的分布情况:

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

直方图通常用于有单个向量(如肺活量)且希望显示值分布的情况:

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")

谢谢您的回复。我意识到我想要的是条形图而不是柱状图。以下是我提出的解决方案:

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()
smoke=read.csv(“SmokingEffect.csv”,header=TRUE)
烟雾=子集(烟雾,选择=c(年龄,烟雾,FEV))
图书馆(plyr)

Smokes表示你的问题需要更多的信息才能得到好的答案。你能描述一下你的数据吗?是肺还是年龄分类的?您可以创建一些模拟数据并显示您尝试过的代码吗。
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()