如何通过构面或组将中断值传递给stat_contour

时间:2019-01-17 15:13:38

标签: r ggplot2 dplyr

我正在尝试使用ks库为数据集中的组计算95%的起始范围。问题在于,定义95%轮廓的截断的“断裂”值在组之间不同。到目前为止,我已经能够获取绘图,但是我必须手动为每个组/级别添加中断值,并且我真的很想找到一种解决方案,可以在ggplot中创建图形,并自动导入中断值。

require(ks)
require(dplyr)
require(ggplot2)

   # define the ks function to pass to a grouped_df 
ksFUN = function(data){

  H = Hpi(data[,c("x","y")], binned = TRUE) * 1

  fhata = kde(data[,c("x","y")], H = H, compute.cont = TRUE,
                  xmin = c(minX, minY), xmax = c(maxX, maxY))
  res95 = data.frame(HR = contourSizes(fhata, cont = 95, approx = TRUE))

  dimnames(fhata[['estimate']]) = list(fhata[["eval.points"]][[1]],
                                       fhata[["eval.points"]][[2]])
  dat = reshape2::melt(fhata[['estimate']])
  dat$breaks50 = fhata[["cont"]]["50%"]
  dat$breaks95 = fhata[["cont"]]["5%"]
  return(dat)
}
set.seed(100)


# create some data
df1 = data.frame(x = rnorm(100, 0, 5),
                 y = rnorm(100, 0, 5), 
                 Group = "Test1")
df2 = data.frame(x = rnorm(100, 10, 5),
                 y = rnorm(100, 10, 5), 
                 Group = "Test2")
df = rbind(df1, df2)

# Set the minimum and maximum x and y values outside 
# of the ksFUN to keep the data on the same scale
minX = min(df$x, na.rm = T); maxX = max(df$x, na.rm = T)
minY = min(df$y, na.rm = T); maxY = max(df$y, na.rm = T)

xx = df %>%
  group_by(Group) %>%
  do(as.data.frame(ksFUN(.)))


# extract the break value for the 95% contour (home range) and 50% (core area)
breaks = xx %>%
  group_by(Group) %>%
  summarize(breaks95 = mean(breaks95),
            breaks50 = mean(breaks50))

breaks



# The only way I have been able to add the breaks is to manually add them

ggplot(data = xx, aes(x = Var1, y = Var2, fill = Group)) +
  geom_point(data = df, aes(x = x, y = y, col = Group)) +
  stat_contour(data = xx[xx$Group == "Test1",], aes(z = value),
                         breaks = 0.000587, alpha = 0.3, geom = "polygon") +
  stat_contour(data = xx[xx$Group == "Test2",], aes(z = value),
                         breaks = 0.000527, alpha = 0.3, geom = "polygon")

enter image description here

我真的很想找到一个无需将中断值显式传递给stat_contour函数的解决方案

1 个答案:

答案 0 :(得分:1)

使用breaks中的breaks列是否有问题?例如

# base plot
pl <- ggplot(data = xx, aes(x = Var1, y = Var2, fill = Group)) +
  geom_point(data = df, aes(x = x, y = y, col = Group))
groups <- unique(xx$Group)

# loop and add for each group
for(i in groups){
  pl <- pl + stat_contour(data = xx[xx$Group == i,], aes(z = value),
                          breaks = breaks[breaks$Group == i, ]$breaks, 
                          alpha = 0.3, geom = "polygon")
}
pl

我在边缘得到了一些奇怪的图,尤其是当我从breaks中删除stat_contour部分时,这使我认为ksFUN中可能存在错误