r - 整个facet_grid的极坐标/圆形布局

标签 r ggplot2 facet

我有一个数据集,其中包含 2 个有序离散因子的值,以及一些相应的连续值。 (下面生成随机样本)

randomData = expand.grid(1:5, 1:100)    # The two discrete variables
colnames(randomData) = c("index1", "index2")

randomData$z = runif(nrow(randomData))   # The measured value 

ggplot(randomData, aes(x = 1, y = z)) + 
  geom_bar(stat = "identity") + 
  facet_grid(index1 ~ index2) + 
  theme_minimal()

enter image description here

我正在尝试使用生成的条形图将整个网格包装成圆形/极坐标布局,如下所示。

第一个图是所需的输出(由第一个方面图的照片编辑工具进行操作)。 第二个图是这篇博文的输出:http://chrisladroue.com/2012/02/polar-histogram-pretty-and-useful/ )。然而,它只包含一个“行”。

enter image description here enter image description here

我想不出办法做到这一点。当我尝试添加 coord_polar() 时,条形图本身受到影响,而不是小平面。

最佳答案

我不认为你可以将面排列成一个圆圈,但你可以通过更改 x 和 y 变量然后使用 coord_polar() 来作弊。

library(ggplot2)
library(dplyr)
d <- rbind(
    mutate(randomData, col = "dark"), 
    mutate(randomData, col = "blank", z = 1 - z)
) %>% arrange(d, index2, index1, col)

ggplot(d, aes(x = factor(index2), y = z)) + 
   geom_bar(aes(fill = col), stat = "identity", position = "stack") + 
   scale_fill_manual(values = c(blank = "white", dark = "black")) + 
   coord_polar() + 
   theme_minimal() + 
   guides(fill = FALSE)

enter image description here

您必须对此进行相当多的调整才能适应您的原始数据集,但您明白了 - 为值 (1 - z) 添加行并使用它们来堆叠条形

关于r - 整个facet_grid的极坐标/圆形布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32864107/

相关文章:

r - 在 facet_grid ggplot : x-axis labels differ per row 上强制 x 轴标签

r - 在 R 上安装包

r - 在 rgl 中绘制具有给定坐标的长方体

r - 如何在R中将向量分解为子向量

r - ggplot2:绘制多边形和点 - 冲突的scale_color_manual

jsf - <f :facet> do and when should I use it? 是什么意思

R保留至少一列大于值的行

r - 将 geom_line 与多个分组一起使用

r - ggplot facet_wrap 在每个方面都有特定的变量顺序

jsf - 在固定大小的表中设置 JSF 列标题的宽度