在甜甜圈图中调整文本

Adapting text inside in Donut Plot

我正在尝试使用 ggplot 2 绘制甜甜圈图。您可以在下面看到我的数据以及我的图。

test_data<-structure(list(KindOfParticipants = c("Participants", "Non-participants", 
                                          "Unknown group"), variable = structure(c(1L, 1L, 1L), .Label = "Total", class = "factor"), 
                          value = c(111L, 5937L, 18L), fraction = c(0.0431091877496671, 
                                                                    0.953894806924101, 0.00299600532623169), ymax = c(0.0431091877496671, 
                                                                                                                      0.997003994673768, 1), ymin = c(0, 0.0431091877496671, 0.997003994673768
                                                                                                                      ), labelPosition = c(0.0215545938748336, 0.520056591211718, 
                                                                                                                                           0.998501997336884), label = c("Participants\n value: 111", "Non-Participants\n value: 5937", 
                                                                                                                                                                         "Unknown group\n value: 110")), row.names = c(NA, -3L), class = "data.frame")

现在我想用这些数据绘制甜甜圈图。

plot_1_test<-ggplot(test_data, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=KindOfParticipants)) +
  geom_rect() +
  geom_text( x=1, aes(y=labelPosition, label=label, color=variable), size=6) + # x here controls label position (inner / outer)
  scale_fill_brewer(palette=3) +
  scale_color_brewer(palette=3) +
  coord_polar(theta="y") +
  xlim(c(-1, 4)) +
  theme_void() +
  theme(legend.position = "none")+ 
  ggtitle("Structure of participants or non participants or unkown groups")
plot_1_test

但是这些代码行给了我一个甜甜圈图,如下图所示:

所以对于这个情节,我有两个问题主要与圆圈内的文字有关

首先我想在圆圈内有可读的文本,现在有 文本重叠,其次是在圆圈内有较深的颜色。

谁能帮我解决这个问题?

您可以使用 hjust 调整您的标签,如下所示。

require(ggplot2)

plot_1_test <- ggplot(test_data, aes(ymax=ymax, ymin=ymin, 
                      xmax=4, xmin=3, 
                      fill=KindOfParticipants)) +
  geom_rect()+ 
  # geom_label(x=3.5,aes(y=labelPosition, label=label),hjust=c(0,1,2))+
  geom_text(x=1, aes(y=labelPosition, label=label, 
                     color=KindOfParticipants), 
            size=5,
            hjust=c(-0.2,0.5,1)) + # x here controls label position (inner / outer)
  scale_fill_brewer(palette=3) +
  scale_color_brewer(palette=3)+
  coord_polar(theta="y")+
  xlim(c(-1, 4))+
  theme_void() +
  theme(legend.position = "none")+ 
  ggtitle("Structure of participants or non participants or unkown groups")
plot_1_test

reprex package (v2.0.1)

于 2022-05-03 创建