本文介绍了去除多余的空间并在极坐标图的边缘敲响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ggplot2 中有一个极坐标图,我在完成时非常接近(相当简单的绘图)。我已经能够在移除矩形边框时获得帮助,但是我不需要去除最后一个距离轮廓和围绕其上具有方位标签的图的环之间的额外空间。我想这个阴谋的界限是15,000 ...而不是15,214(我把这个数字加起来)。感谢您的帮助。

I have a polar plot in ggplot2 that I am getting pretty close in finishing (fairly simple plot). I have been able to get assistance in removing the rectangular boarder but not I need to remove the extra space between the last range contour and the ring around the plot that has the azimuth labels on it. I would like for the bounds of this plot to be at 15,000... not 15,214 (I made that number up). Thanks for any help.

生成图的代码如下:

The code to generate the plot is below:

# Load needed Libraries ---------------------------------------------------

library(ggplot2)

# Generate Fake Data ------------------------------------------------------

N    = 25
bng  = runif(N, min =  0, max = 360)
rng  = rlnorm(N, meanlog = 9, sdlog = 1)
det  = runif(N, min = 0, max = 1) >= 0.5

det  = factor(det)

data = data.frame(bng, rng, det)

# Generate the Plot -------------------------------------------------------

plot = ggplot(data) + theme_bw() +
  geom_point(aes(x = bng, y = rng, color = det), size = 5, alpha = 0.7) +
  scale_x_continuous(limits = c(0,360), expand = c(0,0), breaks = seq(0,360-1, by=45)) +
  scale_y_continuous(limits = c(0,15000), breaks = seq(0,15000, by = 3000)) +
  coord_polar(theta = 'x', start = 0, direction = 1) +
  theme(legend.key = element_blank()) +
  theme(panel.border = element_blank(), axis.ticks = element_blank(), axis.text.y = element_blank()) +
  labs(x = '', y = '') +
  scale_color_manual(name = '', values = c('red', 'black'), breaks = c(FALSE, TRUE), labels = c('Not Detected', 'Detected'))
plot


推荐答案

额外空间由 panel.grid 的最外层圆圈生成。默认情况下,网格将添加到您已使用的主题中(以及大多数 ggplot 主题; )

The extra space is generated by the outermost circle of a panel.grid. The grid is added by default in the theme you have used (and in most other ggplot themes; default settings here)

因此,请在主题中移除 panel.grid 。然后,您可以根据自己的口味创建自己的网格,例如使用 geom_hline 和 geom_vline 。在这里,我使用了在 scale_x 和 _y 中指定的休息符作为拦截。我从 theme_bw 中的默认 panel.grid.major 中挑选了线条颜色和大小。

Thus, remove panel.grid in theme. You might then create an own grid, according to taste, using e.g. geom_hline and geom_vline. Here I used the breaks you had specified in scale_x and _y as intercepts. I picked line colour and size from default panel.grid.major in theme_bw.

ggplot(data = df) +
  geom_point(aes(x = bng, y = rng, color = det), size = 5, alpha = 0.7) +
  geom_hline(yintercept = seq(0, 15000, by = 3000), colour = "grey90", size = 0.2) +
  geom_vline(xintercept = seq(0, 360-1, by = 45), colour = "grey90", size = 0.2) +
  coord_polar(theta = 'x', start = 0, direction = 1) +
  labs(x = '', y = '') +
  scale_color_manual(name = '',
                     values = c('red', 'black'),
                     breaks = c(FALSE, TRUE),
                     labels = c('Not Detected', 'Detected')) +
  scale_x_continuous(limits = c(0, 360), expand = c(0, 0), breaks = seq(0, 360-1, by = 45)) +
  scale_y_continuous(limits = c(0, 15000), breaks = seq(0, 15000, by = 3000)) +
  theme_bw() +
  theme(panel.border = element_blank(),
        legend.key = element_blank(),
        axis.ticks = element_blank(),
        axis.text.y = element_blank(),
        panel.grid  = element_blank())

这篇关于去除多余的空间并在极坐标图的边缘敲响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 05:55