我想在Archimedean螺旋上创建条形图,就像here一样。

最终目标类似于this,但不那么压倒性的。

这是一个示例数据框:

    test <- structure(list(month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
                                     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
           year = c(2015, 2015, 2015, 2015, 2015, 2015, 2015,
                    2015, 2015, 2015, 2015, 2015, 2016, 2016,
                    2016, 2016, 2016, 2016, 2016, 2016, 2016,
                    2016, 2016, 2016),
           value = c(49, 34, 35, 34, 50, 35, 48, 50, 44, 38, 42,
                   43, 33,30, 42, 43, 58, 55, 47, 36, 35, 53,
                   61, 59)),
          .Names = c("month", "year", "value"),
          class = "data.frame", row.names = c(NA, -24L))

我可以使用以下代码制作条形图:
    ggplot(monthly, aes(x = ym, y = value)) +
      geom_bar(stat = "identity")

我可以使用以下代码使螺旋状旋转:
    a <- 0   #Any number here & it still looks the same to me...
    b <- 10  #Any number here & it still looks the same to me...
    theta <- seq(0,10*pi, 0.01)
    r <- a + b*theta
    df <- data.frame(x = r*cos(theta), y = r*sin(theta))
    ggplot(df, aes(x,y)) +
      geom_point(col = 'red')

但是,如何(如果有的话)可以在螺旋线上绘制条形图?

这与我所获得的差不多:用我的数据而不是上面的公式创建一个螺旋。但是我的数据并没有真正显示出来...
    d <- ggplot(monthly, aes(x = month, y = month, color = year)) +
      geom_path(size = 2) +
      coord_polar() +
      theme_minimal() +
      theme(legend.position = "none")
    d

最佳答案

一个有趣的问题。为了正确使用钢筋,我们需要使用多边形来正确考虑每个角的“翘曲”。每个周期中的条形越多,此图看起来越好,但可以更改y限制以避免中心出现严重变形。您必须想出一些方法来很好地标记y轴,但是coord_polar在一开始并不是很好。

library(tidyverse)

首先,要么从头开始创建样本df:
monthly <-
  expand.grid(month = 1:12, year = factor(unique(monthly$year))) %>%
  mutate(value = runif(n(), 10, 20),
         y = as.numeric(year) - 1 + (month - 1) / 12)

或者,从现有的df工作:
    monthly <- monthly %>%
      mutate(y = as.numeric(year) - 1 + (month - 1) / 12)

继续执行以下操作:
bars <- monthly %>%
  mutate(value_norm = value / (max(value) * 1.1),
         xmin = month - 0.5,
         xmax = month + 0.5,
         ymin = y,
         ymax = y + value_norm)
# we could plot `bars` here, but things will not line up nicely, since
# the bar will be nice and flat, but it needs to curve with the spiral.

poly <- bars %>%
  rowwise() %>%
  do(with(., data_frame(year = year,
                        month = month,
                        x = c(xmin, xmax, xmax, xmin),
                        y = c(ymin - 1/24,
                              ymin + 1/24,
                              ymax + 1/24,
                              ymax - 1/24))))

ggplot(poly, aes(x, y, fill = interaction(month, year))) +
  geom_polygon(col = 1) +
  coord_polar() +
  ylim(-3, 5) +
  viridis::scale_fill_viridis(discrete = TRUE, option = 'C') +
  scale_x_continuous(breaks = 1:12, labels = month.name) +
  theme_minimal() +
  theme(legend.position = "none", axis.text.y = element_blank(),
        axis.title = element_blank())

r - 使用ggplot和coord_polar的螺旋条形图(Condegram)-LMLPHP

另一种选择是仅制作螺旋状的热图:
bars2 <- monthly %>%
  mutate(xmin = month - 0.5,
         xmax = month + 0.5,
         ymin = y,
         ymax = y + 1)

poly2 <- bars2 %>%
  rowwise() %>%
  do(with(., data_frame(value = value,
                        year = year,
                        month = month,
                        x = c(xmin, xmax, xmax, xmin),
                        y = c(ymin - 1/24, ymin + 1/24, ymax + 1/24, ymax - 1/24))))

r - 使用ggplot和coord_polar的螺旋条形图(Condegram)-LMLPHP

关于r - 使用ggplot和coord_polar的螺旋条形图(Condegram),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41603341/

10-09 10:19