增加ggplot极坐标图的多边形分辨率

时间:2012-02-28 13:45:11

标签: r ggplot2

我正在用 ggplot2 (使用 geom_bar coord_polar(theta =“y”)绘制40+条/环的大极坐标图/饼图)),我发现y轴绘图压缩导致最内圈的多边形分辨率非常差。

有人知道如何提高多边形分辨率吗?

df <- data.frame(
  x = sort(sample(1:40, 400, replace=TRUE)), 
  y = sample(0:9, 400, replace=TRUE)
)


ggplot(df, aes(x=x, y=y, fill=y)) + 
  geom_bar(stat='identity', position="fill") + 
  coord_polar(theta="y") + 
  scale_fill_continuous(low="blue", high="pink")

enter image description here

这就是我想要实现的几何分辨率。我通过绘制只有5个级别来管理这个。

enter image description here

当我增加到40级时,中央多边形会失去光滑度并变得过于锯齿状,如下所示:

enter image description here

2 个答案:

答案 0 :(得分:8)

问题在于ggplot2:::coord_munch函数,其函数segment_length的默认值为0.01:

https://github.com/hadley/ggplot2/blob/master/R/coord-munch.r

我认为没有任何地方可以传递参数,这些参数会归结为coord_munch的{​​{1}}参数。目前处理它的一种方法是使用具有segment_length不同默认值的包装函数替换coord_munch

segment_length

完成后,您可以再次运行该示例:

# Save the original version of coord_munch
coord_munch_old <- ggplot2:::coord_munch

# Make a wrapper function that has a different default for segment_length
coord_munch_new <- function(coord, data, range, segment_length = 1/500) {
  coord_munch_old(coord, data, range, segment_length)
}
# Make the new function run in the same environment
environment(coord_munch_new) <- environment(ggplot2:::coord_munch)

# Replace ggplot2:::coord_munch with coord_munch_new
assignInNamespace("coord_munch", coord_munch_new, ns="ggplot2")

在命名空间中分配值只应用于开发目的,因此这不是一个好的长期解决方案。

答案 1 :(得分:0)

除了上面的正确诊断和解决方法之外,Jean-Olivier还通过ggplot2 Google Group建议了另一种解决方法:

  

相反,更改数据以增加坐标值   数据空间,通过这样做:

ggplot(df, aes(x=x+100, y=y, fill=y)) +
 geom_bar(stat='identity', position="fill") +
 coord_polar(theta="y") +
 scale_fill_continuous(low="blue", high="pink")

谢谢大家。