R语言 如何删除ggplot2中geom_text的重复标签用于冲积地块?

lx0bsm1f  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(115)

我做了一个冲积图,并希望标签的节点部分的图表与百分比。当我使用“geom_text”函数时,我得到了重复的标签,其中一个完全位于我想要的位置,但另一个在下面图像截图的最右边。我怎样才能去掉最右边的那个复制品呢?

以下是我目前为止的代码:

ggplot(data = survey_3,
               aes(axis1 = leave, axis2 = stringr::str_wrap(Q58,40), y = freq)) + #add intention to leave and responses as axes 1 & 2 & the y-axis as frequency
  geom_alluvium(aes(fill = Q58)) + #response is the fill of the alluvial plot band
  geom_stratum() +
  scale_x_discrete(limits=c("leave", "Q58"), expand=c(0.1, 0.1))+ #formatting to make it look nicer
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum)), fontface="bold") + 
  geom_flow()+
  geom_text(stat="flow", aes(label=percent), nudge_x = 0.5, fontface="bold", size=5)+
  theme_void()+
  guides(fill=guide_legend(title=""))+#modify title of legend
  ggtitle("Future Plans of Nurses Intending to Leave\n Current Position Within Next Year" )+
  theme(plot.title=element_text(hjust=0.5, face="bold", size=15), 
        legend.position = "none")#label title and axes & suppress legend

下面是我的dataframe:

survey_3<-data.frame(leave=c("Plans to leave w/in 6 months or a year",
                                  "Plans to leave w/in 6 months or a year",
                                  "Plans to leave w/in 6 months or a year",
                                  "Plans to leave w/in 6 months or a year",
                                  "Plans to leave w/in 6 months or a year"),
                     Q58=c("I plan to pursue additional education",
                           "I plan to take a non-nursing position",
                           "I plan to take another nursing position in my current institution",
                           "I plan to retire/stop working",
                           "I plan to take another nursing position outside of my current institution"),
                     freq=c(139, 299, 307, 514, 957),
                     percent=c("1.97%", "4.24%", "4.35%", "7.28%", "13.56%"))
zzoitvuj

zzoitvuj1#

问题是您为每个轴获取一个标签。解决这个问题的一个选择是使用stat="identity"添加标签,这需要在x上Map一个值,即x = 1.5

p +
  geom_text(
    aes(
      x = 1.5,
      label = percent
    ),
    position = position_stack(vjust = .5),
    fontface = "bold"
  )

第二个选项是使用coord_cartesian设置限制来隐藏重复的标签:

p +
  geom_text(
    stat = "flow", aes(
      label = percent
    ),
    nudge_x = 0.5,
    fontface = "bold"
  ) +
  coord_cartesian(xlim = c(1, 2))

基本图

library(ggplot2)
library(ggalluvial)

p <- ggplot(
  data = survey_3,
  aes(axis1 = leave, axis2 = stringr::str_wrap(Q58, 40), y = freq)
) +
  geom_alluvium(aes(fill = Q58)) +
  geom_stratum() +
  scale_x_discrete(limits = c("leave", "Q58"), expand = c(0.1, 0.1)) +
  geom_text(
    stat = "stratum",
    aes(
      label = after_stat(stratum)
    ), fontface = "bold"
  ) +
  geom_flow() +
  theme_void() +
  guides(fill = guide_legend(title = "")) + # modify title of legend
  ggtitle("Future Plans of Nurses Intending to Leave\n Current Position Within Next Year") +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold", size = 15),
    legend.position = "none"
  )

相关问题