我下面有这个数据:

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")

推荐答案

你让这件事变得比你需要的要困难得多.使用geontextPATH包中的coord_curvedpolar将很好地排列放射状标签.您还可以使用geom_textpath代替geom_text,这样您就不需要计算标签的Angular .

library(geomtextpath)

ggplot(data_melted, aes(x = SN_types, y = new_value, 
                        fill = variable, label = new_value)) +
  geom_col(position = position_dodge(width = 0.9)) +
  geom_vline(xintercept = seq(1.5, 19.5, by = 3), linetype = "dashed") +
  geom_textpath(position = position_dodge(width = 1), vjust = 1, size = 5) + 
  scale_fill_manual(values = c('deepskyblue4', 'orangered'), 
                    labels = c("SJLIFE", "CCSS")) + 
  scale_y_continuous(breaks = seq(0, 100, by = 10)) + 
  coord_curvedpolar() +
  theme_minimal() +
  theme(panel.grid.major.y = element_line(color = "gray", linetype = "dotted"), 
        panel.grid.minor = element_blank(),
        axis.line = element_blank(),  
        axis.text = element_text(size = 12, color = "black"), 
        axis.title = element_text(size = 14, color = "black"), 
        legend.title = element_text(size = 14, color = "black"), 
        legend.text = element_text(size = 12, color = "black")) + 
  labs(x = NULL, y = "(%)", fill = "Cohort")

enter image description here

请注意,您没有包括 colored颜色 darker_sjlifedarker_ccss的定义,因此我只是 Select 了几种随机 colored颜色 来替换它们.

R相关问答推荐

根据固定值范围在tible中添加新行

在ggplot的注释表格中突出显示最大值

获取一个数据库框架的摘要,该数据库框架将包含一列数据库框架,

如何删除gggvenn与gggplot绘制的空白?

如何在R中合并两个基准点?

如何在观测缺失的地方添加零

以更少间隔的较小表中的聚合离散频率表

try 将 colored颜色 编码添加到ggploly的标题中

Select 季度月值

以相同的方式对每个表进行排序

从非重叠(非滚动)周期中的最新数据向后开窗并在周期内计数

QY数据的处理:如何定义QY因素的水平

根据列表中项目的名称合并数据框和列表

在多页PDF中以特定布局排列的绘图列表不起作用

我将工作代码重构为一个函数--现在我想不出如何传递轴列参数

为什么在写入CSV文件时Purrr::Pwalk不起作用

随机 Select 的非NA列的行均数

通过R:文件名未正确写入[已解决]将.nc文件转换和导出为.tif文件

如何使用grepl()在数据帧列表中 Select 特定字符串?

将y轴格式更改为R中的百分比