【问题标题】:How to fill in the contour fully using stat_contour如何使用 stat_contour 完全填充轮廓
【发布时间】:2015-02-12 05:14:30
【问题描述】:

我正在寻找完全填充 ggplot2 的 stat_contour 生成的轮廓的方法。目前的结果是这样的:

# Generate data
library(ggplot2)
library(reshape2) # for melt
volcano3d <- melt(volcano)
names(volcano3d) <- c("x", "y", "z")

v <- ggplot(volcano3d, aes(x, y, z = z))
v + stat_contour(geom="polygon", aes(fill=..level..)) 

如下手动修改代码即可产生想要的结果。

v + stat_contour(geom="polygon", aes(fill=..level..)) +
  theme(panel.grid=element_blank())+  # delete grid lines
  scale_x_continuous(limits=c(min(volcano3d$x),max(volcano3d$x)), expand=c(0,0))+ # set x limits
  scale_y_continuous(limits=c(min(volcano3d$y),max(volcano3d$y)), expand=c(0,0))+  # set y limits
  theme(panel.background=element_rect(fill="#132B43"))  # color background

我的问题:有没有办法在不手动指定颜色或使用geom_tile() 的情况下完全填充绘图?

【问题讨论】:

  • 据我所知,您需要手动扩展数据集。您的解决方案看起来更简单,所以如果您对它感到满意,请保持原样。
  • 我也看到了那个帖子,但是 geom_tile() 使用了小矩形,所以这不是我想要的效果。 fill.contour 迄今为止产生了最好的结果,但它与多重绘图的不兼容导致我尝试使用 ggplots。我想知道是什么产生了透明区域。
  • 这里对您看到的工件进行了部分解释:stackoverflow.com/questions/23257951/…。看看您是否可以根据您的情况调整该答案?
  • 成功了! @tonytonov 非常感谢!我将在下面发布答案。

标签: r ggplot2 contour


【解决方案1】:

正如@tonytonov 所建议的thread,可以通过关闭多边形来删除透明区域。

# check x and y grid
minValue<-sapply(volcano3d,min)
maxValue<-sapply(volcano3d,max)
arbitaryValue=min(volcano3d$z-10)

test1<-data.frame(x=minValue[1]-1,y=minValue[2]:maxValue[2],z=arbitaryValue)
test2<-data.frame(x=minValue[1]:maxValue[1],y=minValue[2]-1,z=arbitaryValue)
test3<-data.frame(x=maxValue[1]+1,y=minValue[2]:maxValue[2],z=arbitaryValue)
test4<-data.frame(x=minValue[1]:maxValue[1],y=maxValue[2]+1,z=arbitaryValue)
test<-rbind(test1,test2,test3,test4)

vol<-rbind(volcano3d,test)

w <- ggplot(vol, aes(x, y, z = z))
w + stat_contour(geom="polygon", aes(fill=..level..)) # better

# Doesn't work when trying to get rid of unwanted space
w + stat_contour(geom="polygon", aes(fill=..level..))+
  scale_x_continuous(limits=c(min(volcano3d$x),max(volcano3d$x)), expand=c(0,0))+ # set x limits
  scale_y_continuous(limits=c(min(volcano3d$y),max(volcano3d$y)), expand=c(0,0))  # set y limits

# work here!
w + stat_contour(geom="polygon", aes(fill=..level..))+
coord_cartesian(xlim=c(min(volcano3d$x),max(volcano3d$x)),
                ylim=c(min(volcano3d$y),max(volcano3d$y)))

这个调整仍然存在的问题是除了反复试验之外寻找方法来确定arbitaryValue

[从这里编辑]

只是一个快速更新,以展示我如何确定arbitaryValue,而无需猜测每个数据集。

BINS<-50
BINWIDTH<-(diff(range(volcano3d$z))/BINS) # reference from ggplot2 code
arbitaryValue=min(volcano3d$z)-BINWIDTH*1.5

这似乎适用于我现在正在处理的数据集。不确定是否适用于其他人。另外,请注意,我在此处设置 BINS 值的事实要求我必须在 stat_contour 中使用 bins=BINS

【讨论】:

  • 很高兴我能帮上忙。很好的解决方案,尤其是这些看起来令人讨厌的工件现在已经消失了。感谢发帖!
【解决方案2】:

感谢@chengvt 的回答。我有时需要这种技术,所以我做了一个广义的function()

test_f <- function(df) {
  colname <- names(df)
  names(df) <- c("x", "y", "z")
  Range <- as.data.frame(sapply(df, range))
  Dim <- as.data.frame(t(sapply(df, function(x) length(unique(x)))))
  arb_z = Range$z[1] - diff(Range$z)/20
  df2 <- rbind(df,
               expand.grid(x = c(Range$x[1] - diff(Range$x)/20, Range$x[2] + diff(Range$x)/20), 
                           y = seq(Range$y[1], Range$y[2], length = Dim$y), z = arb_z),
               expand.grid(x = seq(Range$x[1], Range$x[2], length = Dim$x),
                           y = c(Range$y[1] - diff(Range$y)/20, Range$y[2] + diff(Range$y)/20), z = arb_z))
  g <- ggplot(df2, aes(x, y, z = z)) + labs(x = colname[1], y = colname[2], fill = colname[3]) + 
    stat_contour(geom="polygon", aes(fill=..level..)) + 
    coord_cartesian(xlim=c(Range$x), ylim=c(Range$y), expand = F)
  return(g)
}

library(ggplot2); library(reshape2)
volcano3d <- melt(volcano)
names(volcano3d) <- c("xxx", "yyy", "zzz")
test_f(volcano3d) + scale_fill_gradientn(colours = terrain.colors(10))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    相关资源
    最近更新 更多