去除极坐标图边缘的多余空间和环 [英] Remove extra space and ring at the edge of a polar plot
问题描述
我在 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.
生成绘图的代码如下:
# 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
的最外圈生成.网格默认添加到您使用的 theme
(以及大多数其他 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)
因此,删除theme
中的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())
这篇关于去除极坐标图边缘的多余空间和环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!