ggplot2剪辑stat_density(密度图)中的数据

时间:2012-04-12 07:22:27

标签: r ggplot2

数据文件:http://ubuntuone.com/3x5z4kFEUcVUB8KXYjICpD

绘制上述数据,如下图所示:

tc = read.table('tertiary-tc.csv', header=T, sep=',')
library(ggplot2)
old <- theme_set(theme_bw())
pg <- ggplot(tertiary, aes(Conductivity)) + stat_density(geom = 'path'
    , position = 'identity') + facet_wrap(~Lithology)
print(pg)

而这不是:

library(lattice) 
densityplot(~Conductivity | Lithology
    , data = tc
    , groups=Lithology
    , plot.points=T
    , ref = T
    , main="Density plot"
    , auto.key=F
    , scales=list(tck=-1))  # set to a negative to plot inside ticks? Works!
dev.off()

未绘制的点(仅快速查看)是岩性列下的两个点,分类为“白云石”。 ggplot2没有绘制它们,因为它们是不规范的吗?或者因为只有两点?

1 个答案:

答案 0 :(得分:2)

这样的事可能会让你开始

density2 <- function(x, select, ...){
  n <- nrow(x)
  range <- range(x[[select]])
  xgrid <- seq(range[1], range[2], length=200)

  weight <- rep(1, n) / n
  d <- density(x[[select]], ...,
                  weight=weight, from=range[1], to=range[2])

  as.data.frame(d[c("x","y")])

}


summaries <- ddply(tc, "Lithology", density2, select="Conductivity")

 ggplot(summaries, aes(x, y))  + facet_wrap(~Lithology) +
  geom_path()

不确定为什么它看起来有点修剪,我不熟悉density()