I'm trying to replicate a Rose/Coxcomb/Circular Bar chart: example 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.
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!
but when I add this line to the end of the ggplot call:
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!
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.
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.
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!
CodePudding user response:
Your categories are placed on 1, 2, .. in numerical terms. 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:
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))