R 提高ggplot极坐标图的多边形分辨率

R 提高ggplot极坐标图的多边形分辨率,r,ggplot2,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, fi

我正在用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")

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

我不认为有任何地方可以传递可以归结为
coord\u munch
segment\u length
参数的参数。目前解决这个问题的一种方法是用一个包装函数替换
coord\u munch
,该包装函数对
segment\u 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")
#保存coord_munch的原始版本

coord_munch_old除了上述wch的正确诊断和解决方法外,Jean Olivier还通过ggplot2谷歌集团提出了另一种解决方法:

相反,更改数据以增加图形中的坐标值 数据空间,通过执行以下操作:


谢谢大家。

如果您坚持使用代码和一些示例数据,我会给您投赞成票。如果您不发布可复制的代码和示例数据,我甚至可能会投反对票。请参阅,以获取有关提问的提示。干杯!这段代码再现了这个问题。如果你保存到PDF并放大到中心,你就会明白我的意思<代码>df我不认为这是一个图像分辨率问题。我认为最里面的环只是用太少的多边形线段绘制的。这可能是一个bug。在ggplot2邮件列表中询问此问题可能会更幸运。这是因为
coord_polar
确定路径物理长度的除法数。如果圆周长度较短,则分割数较小。在大多数情况下,这是可以的,但正如你所说,在某些情况下,你需要更多的除法;版本0.8.9没有
coord\u munch
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()
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")