我下面有这个数据:
data_melted <- structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("SJLIFE", "CCSS"), class = "factor"),
SN_types = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L), levels = c("Any SN", "SMN", "NMSC",
"Breast cancer", "Thyroid cancer", "Meningioma", "Sarcoma"
), class = "factor"), AF_by = c("Combined", "Combined", "Combined",
"Combined", "Combined", "Combined", "Combined", "Combined",
"Combined", "Combined", "Combined", "Combined", "Combined",
"Combined"), value = c(0.536, 0.467, 0.688, 0.704, 0.866,
0.423, 0.316, 0.435, 0.323, 0.577, 0.743, 0.664, 0.444, 0.362
), new_value = c(54, 47, 69, 70, 87, 42, 32, 44, 32, 58,
74, 66, 44, 36)), row.names = c(6L, 12L, 18L, 24L, 30L, 36L,
42L, 48L, 54L, 60L, 66L, 72L, 78L, 84L), class = "data.frame")
我正在使用这个代码来制作情节.然而,我很难将文本正确对齐.我希望每个条顶部的文本与每个条呈90度角,保持放射状图的角位置.我该怎么做?
angle_increment.AF <- 360 / 14
angle_increment.SN_type <- 360 / 7
ggplot(data_melted, aes(x = SN_types, y = new_value, fill = variable, label = new_value)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
geom_vline(xintercept = seq(1.5, 19.5, by = 3), color = "black", linetype = "dashed") + # If you want to include a vertical line
geom_hline(yintercept = 100) +
geom_text(aes(angle = 0 + (angle_increment.AF * (as.numeric(as.factor(SN_types)) - 1))), # Adjust angle of text labels
position = position_dodge(width = 1), vjust = 0.9, size = 5, color = "black") + # Adjust label position and size
scale_fill_manual(values = c(darker_sjlife, darker_ccss), labels = c("SJLIFE", "CCSS")) + # Specify colors for SJLIFE and CCSS bars
scale_y_continuous(breaks = seq(0, 100, by = 10)) + # Custom grid lines from 0 to 1 by 0.25
coord_polar() +
theme_minimal() +
theme(panel.grid.major.y = element_line(color = "gray", linetype = "dotted"), # Adjust major grid lines
panel.grid.minor = element_blank(), # Remove minor grid lines
axis.line = element_blank(), # Remove axis lines
axis.text.x = element_text(hjust = 0.5, angle = angle_increment, color = "black"), # Center SN type labels within grid
axis.text = element_text(size = 12, color = "black"), # Adjust axis text size
axis.title = element_text(size = 14, color = "black"), # Adjust axis title size
legend.title = element_text(size = 14, color = "black"), # Adjust legend title size
legend.text = element_text(size = 12, color = "black")) + # Adjust legend text size
labs(title = "",
x = NULL, y = "(%)",
fill = "Cohort")