r - 如何仅显示带有刻面的极地ggplot的部分绘图区域?

标签 r ggplot2

假设有一个极坐标数据集被绘制为扇区

library(ggplot2)
library(reshape2)
data <- melt(matrix(rnorm(1000), nrow = 20))
data$type <- 1:2
data$Var1 <- data$Var1*6 - 60

ggplot(data, aes(Var1, Var2)) + 
  geom_tile(aes(fill = value)) + 
  coord_polar(theta = "x", start = pi) + 
  scale_x_continuous(limits = c(-180, 180)) +
  facet_wrap(~type)

给出了下图:
enter image description here

我们如何在不画一个完整圆圈的情况下删除情节的底部(空白)部分?

最佳答案

这是一个不雅的 hack,但你可以使用 grid功能来掩盖你不想要的区域。例如:

library(ggplot2)
library(reshape2)
library(grid)

data <- melt(matrix(rnorm(1000), nrow = 20))
data$type <- 1:2
data$Var1 <- data$Var1*6 - 60

p1 = ggplot(data, aes(Var1, Var2)) + 
  geom_tile(aes(fill = value)) + 
  coord_polar(theta = "x", start = pi) + 
  scale_x_continuous(limits = c(-180, 180)) +
  facet_wrap(~type)
g1 = ggplotGrob(p1)

grid.newpage()
pushViewport(viewport(height=1, width=1, clip="on"))
grid.draw(g1)
grid.rect(x=0,y=0,height=1, width=2, gp=gpar(col="white"))

这切断了图表的下半部分(见下文)。找到一种更优雅的方法会很好,但如果失败了,也许您可​​以使用视口(viewport)放置和绘图功能(更不用说更改轴标签和图例的位置)来获得接近您想要的东西。
enter image description here

关于r - 如何仅显示带有刻面的极地ggplot的部分绘图区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22398350/

相关文章:

r - 为 gganimate 设置较慢的帧速率或较长的持续时间

r - R 中带有rep 和 dplyr 管道的 mapply 的奇怪行为

r - 在 R 中使用 'introjs' 突出显示 pickerInput

r - 如何在facet ggplot的空白区域定位注释文本

r - ggplot2 (Barplot + LinePlot) - 双 Y 轴

r - 修改ggplot2中圆环图百分比标签的位置和格式

r - 如何使用 maptools、ggplot 或其他软件包在 R map 中添加城市和质心的名称?

r - 在 R 环境中工作的绘图,但不是通过 RScript

r - 在 RStudio 中打印太大的图例框

r - 生成季节性图,但具有财政年度开始/结束日期