简体   繁体   English

在 ggplot2 中将 geom_text() 或 annotate() 添加到 coord_polar plot 时的奇怪行为

[英]Strange behaviour when adding geom_text() or annotate() to coord_polar plot in ggplot2

I'm trying to replicate a Rose/Coxcomb/Circular Bar chart: example plot .我正在尝试复制 Rose/Coxcomb/Circular 条形图:示例 plot I've managed to get the following to replicate the geom and the panel, but when I try to move the axis label from the left hand side of the plot margin and into the middle axis, it seems to send the aesthetic into chaos - it adds a "column" (ie pie-slice) and each variable straddles two other columns.我设法得到以下复制几何和面板,但是当我尝试将轴 label 从 plot 边距的左侧移动到中间轴时,它似乎使美学陷入混乱 - 它添加一个“列”(即饼图)并且每个变量跨越另外两个列。

Here's a replica of code that works:这是有效的代码副本:

df <- tibble(name = "Sam",
             business = 2,
             product = 1,
             collaboration = 2,
             leadership = 2,
             strategy = 4,
             tactics = 1,
             users = 2,
             industry = 3)

df <- df %>% 
  pivot_longer(cols = business:industry, 
               names_to = "dimension", 
               values_to = "score")

df %>% 
  mutate(dimension = factor(dimension,
                            levels = unique(dimension),
                            labels = str_to_title(unique(dimension)))) %>%
  ggplot(aes(x = dimension,
           y = score)) +
  geom_col(fill = "red",
           alpha = 1,
           width = 1) +
  geom_hline(yintercept = seq(0, 5, by = 1),
             color = "grey", size = 1) +
  geom_vline(xintercept = seq(.5, 16.5, by = 1),
             color = "grey", size = 1) +
  coord_polar() +
  theme(panel.grid = element_blank(),
        panel.background = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank()) +
  expand_limits(y = c(-1,5))

which produces this output: code that works!产生这个 output:代码有效!

but when I add this line to the end of the ggplot call:但是当我将此行添加到 ggplot 调用的末尾时:

df %>% 
  mutate(dimension = factor(dimension,
                            levels = unique(dimension),
                            labels = str_to_title(unique(dimension)))) %>%
  ggplot(aes(x = dimension,
           y = score)) +
  geom_col(fill = "red",
           alpha = 1,
           width = 1) +
  geom_hline(yintercept = seq(0, 5, by = 1),
             color = "grey", size = 1) +
  geom_vline(xintercept = seq(.5, 16.5, by = 1),
             color = "grey", size = 1) +
  coord_polar() +
  theme(panel.grid = element_blank(),
        panel.background = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank()) +
  expand_limits(y = c(-1,5)) +
  annotate("text", 
           x = 0,
           y = 1:5,
           label = c(1, 2, 3, 4, 5))

It produces this plot: code that fails!它产生这个 plot:代码失败!

What's weird is if I add just a single label (ie - x = 1) then it works perfectly, but I don't fancy having to manually add 10 new geom_text lines to individually label each axis tick.奇怪的是,如果我只添加一个 label (即 - x = 1),那么它工作得很好,但我不想手动添加 10 条新的 geom_text 行到 label 每个轴刻度。

I'd love to be able to have one vertical set of axis labels running from the centre up the x = 0 point and one horizontal set of labels along the y = 0 line.我希望能够有一组从中心向上运行 x = 0 点的垂直轴标签和一组沿 y = 0 线的水平标签。

Any help greatly appreciated!非常感谢任何帮助!

Apologies for the links instead of images, this is my first post to StackOverflow and I don't have enough reputation points to embed images!为链接而不是图像道歉,这是我在 StackOverflow 上的第一篇文章,我没有足够的声誉点来嵌入图像!

Your categories are placed on 1, 2, .. in numerical terms.您的类别以数字形式排列在 1、2、.. 上。 Hence, to place you annotations on the grid line between two categories you have to use.5 for the "x=0" line and 2.5 for the "y=0" line:因此,要将注释放置在必须使用的两个类别之间的网格线上。5 用于“x=0”行,2.5 用于“y=0”行:

library(dplyr, warn = FALSE)
library(tidyr)
library(ggplot2)
library(stringr)

df %>% 
  mutate(dimension = factor(dimension,
                            levels = unique(dimension),
                            labels = str_to_title(unique(dimension)))) %>%
  ggplot(aes(x = dimension,
             y = score)) +
  geom_col(fill = "red",
           alpha = 1,
           width = 1) +
  geom_hline(yintercept = seq(0, 5, by = 1),
             color = "grey", size = 1) +
  geom_vline(xintercept = seq(.5, 16.5, by = 1),
             color = "grey", size = 1) +
  coord_polar() +
  theme(panel.grid = element_blank(),
        panel.background = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank()) +
  expand_limits(y = c(-1,5)) +
  annotate("text", 
           x = .5,
           y = 1:5,
           label = c(1, 2, 3, 4, 5)) +
  annotate("text", 
           x = 2.5,
           y = 1:5,
           label = c(1, 2, 3, 4, 5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM