我不能使值的顺序与图中的条形对齐,有什么想法吗?
图表可以在以下代码中复制,
varFila <- "ano"
varColumna <- "afiliacion"
cuadro <- structure(list(ano = c(2003, 2003, 2008, 2008, 2008, 2010, 2010,
2010, 2011, 2011, 2011, 2012, 2012, 2012, 2013, 2013, 2013, 2014,
2014, 2014, 2015, 2015, 2015, 2016, 2016, 2016), afiliacion = c("No",
"Sí", "No", "No sabe, no informa", "Sí", "No", "No sabe, no informa",
"Sí", "No", "No sabe, no informa", "Sí", "No", "No sabe, no informa",
"Sí", "No", "No sabe, no informa", "Sí", "No", "No sabe, no informa",
"Sí", "No", "No sabe, no informa", "Sí", "No", "No sabe, no informa",
"Sí"), absolutos = c(15813920.9765405, 25370151.0234595, 6691646.58353337,
62228.3882781436, 40713808.6744964, 5021642.47876531, 64089.8398770112,
39993659.6813577, 4442163.41425619, 52990.5986835889, 41138603.9870602,
4253872.40406465, 75933.5541871542, 41838843.0417482, 4019397.6481426,
52718.0698691592, 42594461.2819883, 2735949.6931826, 64852.9868628411,
44403026.3199546, 2497641.98883605, 90736.2778858295, 45176376.7332782,
2131767.15608607, 101303.130011721, 46072080.7139022)), .Names = c("ano",
"afiliacion", "absolutos"), row.names = c(NA, -26L), class = "data.frame")
cuadro1 <- subset(cuadro,!is.na(cuadro[,varColumna]),)
cuadro1 <- ddply(cuadro1, c(varFila), transform,
horizontal = 100 * absolutos / sum(absolutos))
cuadro1 <- ddply(cuadro1, c(varFila), transform,
position =cumsum(horizontal) - 0.5*horizontal)
q <-ggplot(cuadro1,aes(x=factor(cuadro1[,varFila]),y=horizontal,fill=factor(cuadro1[,varColumna]))) + geom_bar(stat = 'identity',position="fill")
q <- q + geom_text(aes(label = sprintf("%1.1f%%", horizontal),y = position/100), size=3)+ theme_bw()+
theme(legend.title=element_blank())+
ylab(" ") + xlab("Año") +
guides(fill = guide_legend(reverse = FALSE))
答案 0 :(得分:0)
您需要以不同的方式分配position
参数
geom_bar(stat = 'identity') +
geom_text(aes(label = sprintf("%1.1f%%", horizontal)), position=position_stack(vjust=0.5))+
请注意,我从position="fill"
移除了geom_bar
并将其添加到geom_text
,以便将标签置于中心位置。