【问题标题】:R histogram plot density for a given variable or column of data给定变量或数据列的 R 直方图密度
【发布时间】:2014-10-26 20:31:23
【问题描述】:

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

以下是数据的示例。我实际上想比较不吸烟者和吸烟者的分布:

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

【问题讨论】:

  • 您的问题需要更多信息才能获得好的答案。您能否描述您的数据 - 是肺还是年龄分类?您能否创建一些模拟数据并显示您尝试过的代码。

标签: r plot histogram


【解决方案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")

如果你想绘制两个变量之间的关系,散点图可能是更好的选择:

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

【讨论】:

    【解决方案2】:

    感谢您的回复。我意识到我想要一个条形图而不是直方图。这是我想出的解决方案:

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

    输出如下:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 2023-03-30
      • 1970-01-01
      • 2020-08-08
      相关资源
      最近更新 更多