我有一组来自两个季节(冬季和夏季)的数据(1000+动物),我想展示这两个季节中妊娠长度(天数)模式的差异。我的数据类似于:
id <- c(1,2,3,4,5,6,7,8,9,10)
season <- c(1,1,2,2,1,2,1,1,2,1)
gest <- c(114,NA,123,116,NA,120,110,NA,116,119)
data <- cbind(id,season,gest)我想要这样的东西:
http://had.co.nz/ggplot2/graphics/55078149a733dd1a0b42a57faf847036.png
或者任何类似的图形形式,都能给我一个很好的对比。
谢谢你的帮助
巴松
发布于 2010-10-23 15:59:23
您链接的那个特定绘图使用了ggplot2。我不太擅长使用它,所以我将向您展示如何使用基础图形
data <- as.data.frame(data)
d1 <- density(data$gest[which(data$season==1)], na.rm=TRUE)
d2 <- density(data$gest[which(data$season==2)], na.rm=TRUE)
plot(d1, ylim=c(0, max(d1$y,d2$y)), xlim=range(c(d1$x, d2$x)),
main="Length of gestation", xlab="Length (days)", col="blue", lwd=2)
polygon(d1$x, d1$y, col=rgb(0, 0, 1, 0.5), lty=0)
points(d2, t="l", col="red", lwd=2)
polygon(d2$x, d2$y, col=rgb(1, 0, 0, 0.5), lty=0)或者,请查看lattice包的densityplot函数,尽管我不确定如何填充这些行。
PS:你的数据集有那么小吗?如果是这样的话,密度图可能不是一种方法(散点图会更好)
编辑
如果你想用直方图来做这件事,你可以这样做:
hist(data$gest[which(data$season==1)], main="Length of gestation",
xlab="Length (days)", col=rgb(0, 0, 1, 0.5))
# Note the add=TRUE parameter to superimpose the histograms
hist(data$gest[which(data$season==2)], col=rgb(1, 0, 0, 0.5), add=TRUE)发布于 2010-10-23 19:37:52
library(ggplot2)
df <- data.frame(id=id,season=season,gest=gest)
qplot(gest,data=df,geom="density",fill=season,alpha=I(0.2))这应该会给出与该示例类似的结果,但您可能希望使用alpha参数来获得正确的透明度。
发布于 2010-10-23 15:56:09
有一种图表类型通常用于显示人口统计数据,特别是为了直接对比两个组,其中您希望强调包含两个组的子组的比较,这两个组在人口统计上下文中的某些或所有变量上彼此相同,最常见的应用是男性与女性的年龄结构。这似乎是有效地可视化您的数据的一个很好的候选。
下面显示的图是使用R中的基本图形包和Duncan Temple Lang的(优秀)R包SVGAnnotation创建的,以创建交互元素(通过在SVG中重新呈现图像并对结果进行后处理)。
(虽然该图是使用R和SVGAnnotate创建的,但下图来自英国政府的Site)。

https://stackoverflow.com/questions/4002954
复制相似问题