“缩放"/“缩放"与coord_polar() [英] "zoom"/"scale" with coord_polar()

查看:183
本文介绍了“缩放"/“缩放"与coord_polar()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用geom_smooth()的极坐标图.尽管平滑的黄土线很小,但在图的中心环绕.我想放大",以便您可以更好地看到它.

使用类似scale_y_continuous(limits = c(-.05,.7))的方法将使geom_smooth环变大,但也会改变它,因为它将重新计算受limits = c(-.05,.7)参数限制的数据点.

对于笛卡尔图,我可以使用像coord_cartesian(ylim = c(-.05,.7))这样的东西,它将剪切图表而不是基础数据.但是我看不到用coord_polar()

做到这一点

有什么想法吗?我以为可以使用网格包中的grid.clip()来执行此操作,但是我运气不高.

有什么想法吗?

我的情节现在是什么样子,请注意较高"的红线:

我想画些什么:

使用scale_y_continuous()时会得到什么,请注意较高"的蓝线,但它仍然没有那么大.

解决方案

我还没有找到直接在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.

我们可以通过查看关联的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)

注意,这可能不是一个完美的解决方案,因为这似乎引入了一些我不知道如何解决的图像裁剪问题.我尚未测试过是否可以使用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)
)

I've got a polar plot which uses geom_smooth(). The smoothed loess line though is very small and rings around the center of the plot. I'd like to "zoom in" so you can see it better.

Using something like scale_y_continuous(limits = c(-.05,.7)) will make the geom_smooth ring bigger, but it will also alter it because it will recompute with the datapoints limited by the limits = c(-.05,.7) argument.

For a Cartesian plot I could use something like coord_cartesian(ylim = c(-.05,.7)) which would clip the chart but not the underlying data. However I can see no way to do this with coord_polar()

Any ideas? I thought there might be a way to do this with grid.clip() in the grid package but I am not having much luck.

Any ideas?

What my plot looks like now, note "higher" red line:

What I'd like to draw:

What I get when I use scale_y_continuous() note "higher" blue line, also it's still not that big.

解决方案

I haven't figured out a way to do this directly in coord_polar, but this can be achieved by modifying the ggplot_build object under the hood.

First, here's an attempt to make a plot like yours, using the fake data provided at the bottom of this answer.

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

Here, my Y (or r for radius) axis ranges from about -2.4 to 4.3.

We can confirm this by looking at the associated ggplot_build object:

# 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

If we redefine the range of r and plot that, we get what you're looking for, a close-up of the plot.

# 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)

Note, this may not be a perfect solution, since this seems to introduce some image cropping issues that I don't know how to address. I haven't tested to see if those can be overcome using ggsave or perhaps by further modifying the ggplot_build object.

Sample data used above:

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)
)

这篇关于“缩放"/“缩放"与coord_polar()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆