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

geo*_*ory 14 r ggplot2

我正在使用ggplot2(使用geom_barcoord_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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

wch*_*wch 8

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

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

我不认为有任何地方的参数,这将使它下降到传递coord_munchsegment_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")
Run Code Online (Sandbox Code Playgroud)

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

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

pdf('polar.pdf')
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")
dev.off()
Run Code Online (Sandbox Code Playgroud)

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