r - "zoom"/"scale"与 coord_polar()

标签 r ggplot2 r-grid

我有一个使用 geom_smooth() 的极坐标图。平滑的黄土线非常小,围绕着地 block 的中心。我想“放大”,以便您能看得更清楚。

使用诸如 scale_y_continuous(limits = c(-.05,.7)) 之类的东西将使 geom_smooth 环变大,但它也会改变它,因为它将使用受limits = c(-.05,.7) 参数。

对于笛卡尔图,我可以使用像 coord_cartesian(ylim = c(-.05,.7)) 这样的东西,它会剪切图表,但不会剪切基础数据。但是我看不出有什么办法可以用 coord_polar() 来做到这一点

有什么想法吗?我认为可能有一种方法可以使用 grid 包中的 grid.clip() 来做到这一点,但我运气不佳。

有什么想法吗?

我的情节现在是什么样子,请注意“更高”的红线: Current plot, note larger red line, but its small.

我想画什么: What I'd like to get

当我使用scale_y_continuous()时得到的结果请注意“更高”的蓝线,而且它仍然没有那么大。 What I get with <code>scale_y_continuous()</code> note the blue line is now bigger and its still not that big.

最佳答案

我还没有找到直接在 coord_polar 中执行此操作的方法,但这可以通过修改底层的 ggplot_build 对象来实现。

首先,尝试使用本答案底部提供的虚假数据来绘制像您这样的图。

library(ggplot2)
plot <- ggplot(data, aes(theta, values, color = series, group = series)) + 
  geom_smooth() + 
  scale_x_continuous(breaks = 30*-6:6, limits = c(-180,180)) +
  coord_polar(start = pi, clip = "on") # use "off" to extend plot beyond axes
plot

在这里,我的 Y(或半径 r)轴的范围约为 -2.4 到 4.3。 enter image description here

我们可以通过查看关联的 ggplot_build 来确认这一点对象:

# Create ggplot_build object and look at radius range
plot_build <- ggplot_build(plot)
plot_build[["layout"]][["panel_params"]][[1]][["r.range"]]
# [1] -2.385000  4.337039

如果我们重新定义r的范围并绘制它,我们得到了您正在寻找的东西,情节的特写。

# Here we change the 2nd element (max) of r.range from 4.337 to 1
plot_build[["layout"]][["panel_params"]][[1]][["r.range"]][2] <- 1
plot2 <- ggplot_gtable(plot_build)
plot(plot2)

enter image description here

请注意,这可能不是一个完美的解决方案,因为这似乎引入了一些我不知道如何解决的图像裁剪问题。我还没有测试过是否可以使用ggsave来克服这些问题。或者也许通过进一步修改 ggplot_build对象。

上面使用的示例数据:

set.seed(4.2)
data <- data.frame(
  series = as.factor(rep(c(1:2), each = 10)),
  theta  = rep(seq(from = -170, to = 170, length.out = 10), times = 2), 
  values = rnorm(20, mean = 0, sd = 1)
)

关于r - "zoom"/"scale"与 coord_polar(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35225461/

相关文章:

r - 如何获取基础图形plot.new并通过arrangeGrob与其他图形结合?

r - 如何基于R中其他列中的值添加计数列

r - 将列的平均值添加为线图中的点

r - 如何在 ggplot 函数内传递可选参数

r - R 中的周/天值图

r - 使用TableGrob更改单元格的文本颜色

r - 根据 "group"大小更改矢量的编号

r - 乘以概率分布函数

R:将数据从宽数据转换为长数据 - 多个条件 - 出现错误

r - 在多面 map 的绘图区域外添加比例尺和指北针