我做了一个冲积图,并希望标签的节点部分的图表与百分比。当我使用“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%"))
1条答案
按热度按时间zzoitvuj1#
问题是您为每个轴获取一个标签。解决这个问题的一个选择是使用
stat="identity"
添加标签,这需要在x
上Map一个值,即x = 1.5
。第二个选项是使用
coord_cartesian
设置限制来隐藏重复的标签:基本图