前一篇文章《R 绘制极坐标散点图》介绍了极坐标下的散点图的绘制,本文重点介绍极坐标系下的堆积柱状图的绘制。

同样我们以ggplot2为基础,以x轴为极轴示例如下:


library(ggplot2)
library(ggthemes)
library(dplyr)

data <- data.frame(
  Class = c("A", "A", "B", "B", "C", "C", "D", "D"),
  Value = c(12, 32, 45, 65, 76, 45, 23, 98),
  Name = c("N1", "N2", "N1", "N2", "N1", "N2", "N1", "N2")
)


data  %>%
  ggplot(aes(x = Class, y = Value, fill = Name)) +
  geom_bar(stat = "identity", width = 0.5) +
  theme_few() +
  theme(
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    axis.text.y = element_blank()
  ) +
  # 调整中心位置,防止点聚集在一起
  ylim(-50,150) +
  annotate(
    "text",
    x = rep(0, 4),
    y = c(0, 50, 100, 150),
    label = c("0", "50", "100", "150") ,
    color = "black",
    size = 3 ,
    angle = 0,
    hjust = 1,
    fontface = "bold"
  ) + 
  geom_segment(aes(x = 0, y = 0, xend = 0, yend = 150), size = 0.1, colour = "grey30") +
  # 极坐标系
  coord_polar(theta = "x") +
  ylab("Value")

以y轴为极轴示例如下:


data  %>%
  ggplot(aes(x = Class, y = Value, fill = Name)) +
  geom_bar(stat = "identity", width = 0.5) +
  theme_few() +
  theme(
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    axis.text.y = element_blank()
  ) +
  # 调整中心位置,防止点聚集在一起
  ylim(0,150) +
  annotate(
    "text",
    y = rep(0, 4),
    x = c("A", "B", "C", "D"),
    label = c("A", "B", "C", "D"),
    color = "black",
    size = 3 ,
    angle = 0,
    hjust = 1,
    fontface = "bold"
  ) + 
  # 极坐标系
  coord_polar(theta = "y") +
  ylab("Value")

极坐标展示就告一段落了,不同的坐标系需要注意的是你选取的参考点,其他的内容就可以类比直角坐标系的参数修改了。

参考资料:

1.https://www.data-to-viz.com/graph/circularbarplot.html