“zoom”/“scale” with coord_polar()

安稳与你 提交于 2019-12-05 03:23:21

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)
)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!