去除极坐标图边缘的额外空间和环

时间:2013-12-27 21:22:26

标签: r ggplot2

我在ggplot2中有一个极地情节,我在完成时非常接近(相当简单的情节)。我已经能够帮助移除矩形边界,但是我不需要移除最后一个范围轮廓和具有方位标签的绘图周围环之间的额外空间。我希望这个情节的界限是15,000 ......而不是15,214(我把这个数字提高了)。谢谢你的帮助。

生成图表的代码如下:

# 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

1 个答案:

答案 0 :(得分:11)

额外空间由panel.grid的最外圈生成。默认情况下会在您使用的theme(以及大多数其他ggplot主题中添加网格; default settings here

因此,请移除panel.grid中的theme。然后,您可以根据品味使用例如网格创建自己的网格。 geom_hlinegeom_vline。在这里,我使用了您在scale_x_y中指定的休息作为截距。我从panel.grid.major中的默认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())

enter image description here