我正在尝试创建一个堆积的条形图,在条形图的中间有标签。由于某些原因,标签在第二列(eIC)的位置放置错误。
这是代码:
Role.Type=c("Other", "eIC", "eIC", "Manager", "eIC", "eIC", "Design", "eIC", "eIC", "Other", "Product", "Other", "Manager", "eIC", "eIC")
Role=c('Agile Coach', 'Android', 'Backend', 'Eng Manager', 'Data', 'Data Scientist', 'Design', 'iOS', 'ML', 'Operations', 'Product', 'Quality', 'Director', 'User Research', 'Web')
People=c(3, 3, 20, 11, 10, 7, 2, 2, 21, 1, 11, 3, 2, 2, 5)
df <- data.frame(Role.Type, Role, People)
g <- ggplot(df, aes(Role.Type, People)) +
geom_col(aes(fill=Role), position='stack') +
geom_text(aes(label=Role), size=3, position=position_stack(vjust=.5)) +
scale_y_continuous(breaks=seq(0, 100, by=10)) +
labs(title="Team Role Breakdown", y="People", x="Role Type")
g
您只需要在第一个ggplot()函数中放入“ fill = Role”即可。这样可以在文本标签的正确位置给出您想要的图:
g <- ggplot(df, aes(Role.Type, People, fill = Role)) +
geom_col(position='stack') +
geom_text(aes(label=Role), size=3, position=position_stack(vjust=.5)) +
scale_y_continuous(breaks=seq(0, 100, by=10)) +
labs(title="Team Role Breakdown", y="People", x="Role Type")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句