I have been trying to fix this issue for the last few hours, but still couldn’t resolve it. I would greatly appreciate it if someone could help me with this.
I have this data below:
data <- structure(list(Cohort = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), levels = c("SJLIFE", "CCSS"), class = "factor"),
Risks = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 2L), levels = c("Smokings", "Drinking"), class = "factor"),
individual = c("47%", "41%", "12%", "5%", "38%", "29%", "15%",
"5%", "44%", "39%", "28%", "31%", "61%", "59%", "19%", "36%",
"73%", "48%", "52%", "36%", "91%", "88%", "NA%", "1%", "35%",
"35%", "NA%", "4%"), group = structure(c(1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L), levels = c("A", "B",
"C", "D", "E", "F", "G"), class = "factor"), group2 = structure(c(3L,
1L, 4L, 2L, 3L, 1L, 4L, 2L, 3L, 1L, 4L, 2L, 3L, 1L, 4L, 2L,
3L, 1L, 4L, 2L, 3L, 1L, 4L, 2L, 3L, 1L, 4L, 2L), levels = c("CCSS-All Smokings",
"CCSS-Drinking", "SJLIFE-All Smokings", "SJLIFE-Drinking"
), class = "factor"), value = c(47, 41, 12, 5, 38, 29, 15,
5, 44, 39, 28, 31, 61, 59, 19, 36, 73, 48, 52, 36, 91, 88,
NA, 1, 35, 35, NA, 4), id = 1:28), row.names = c(NA, -28L
), class = "data.frame")
I also have this code which plots the figure I want. However, only issue is that I am unable to add gaps between group variable (A,B,C..G) bars so that the figure looks a bit less crowded. empty_bar <- 1
below is supposed to add gap but for some reason it’s not working. Can someone please suggest how to fix this? Thanks in advance!
# Set a number of 'empty bar' to add at the end of each group
empty_bar <- 0
to_add <- data.frame( matrix(0, empty_bar*nlevels(data$group), ncol(data)) )
colnames(to_add) <- colnames(data)
to_add$group <- rep(levels(data$group), each=empty_bar)
data <- rbind(data, to_add)
data <- data %>% arrange(group)
data$id <- seq(1, nrow(data))
# Get the name and the y position of each label
label_data <- data
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)
# prepare a data frame for base lines
base_data <- data %>%
dplyr::group_by(group) %>%
dplyr::summarize(start=min(id), end=max(id) - empty_bar) %>%
dplyr::rowwise() %>%
dplyr::mutate(title=mean(c(start, end)))
# prepare a data frame for grid (scales)
grid_data <- base_data
grid_data$end <- grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
grid_data$start <- grid_data$start - 1
grid_data <- grid_data[-1,]
# Make the plot
ggplot(data, aes(x=as.factor(id), y=value, fill = Risks)) +
geom_bar(aes(x=as.factor(id), y=value, fill=Risks), stat="identity", alpha=1, width=0.95) +
geom_col_pattern(
aes(pattern = Cohort),
colour = "black",
pattern_fill = "black",
pattern_angle = 45,
pattern_density = 0.1,
pattern_spacing = 0.01,
position = position_dodge2(preserve = 'single'),
na.rm = T
) +
scale_pattern_manual(
values = c("none", "stripe"),
guide = guide_legend(override.aes = list(fill = "gray")) # <- make lighter
) +
scale_fill_manual(
values = custom_colors,
breaks = legend_order,
guide = guide_legend(override.aes = list(pattern = "none")) # <- hide pattern
) +
ylim(-100,120) +
theme_minimal() +
theme(
legend.position = "right",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
coord_polar() +
geom_text(data=label_data, aes(x=id, y=value+2, label=individual, hjust=hjust), color="black", fontface=1,alpha=1, size=6.5, angle= label_data$angle, inherit.aes = FALSE ) +
# Add base line information
geomtextpath::coord_curvedpolar() +
geomtextpath::geom_textsegment(
data = base_data,
aes(
x = start, y = -.1,
xend = end, yend = -.1,
label = group
),
colour = "black",
linewidth = 2.5,
size = 16 / .pt,
inherit.aes = FALSE,
gap = FALSE,
offset = unit(-24, "pt")
) +
theme(
legend.position="right",
legend.justification="right",
legend.box.spacing = unit(-120, "pt"),# The spacing between the plotting area and the legend box (unit)
legend.margin=margin(0,0,0,0),
legend.title = element_text(size = 16, color = "black", face=2), # Adjust legend title size
plot.margin = margin(r = .5, l = -3.5, t = -3.5, b = -3.5, unit = "cm"),
legend.text = element_text(size = 14, color = "black")
) + # Adjust legend text size
labs(
title = "",
x = NULL, y = "",
fill = "Risk factors"
)