我正试图制作一个只有边框的条形图,但我对这个传说有些困难。到目前为止,我有这个情节:
正如你所看到的,它有两个传说,但我想只将它们组合在一起:一个只有彩色边框的白色方块。有办法做到这一点吗?
代码:
piramide <- structure(list(Sexo = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Homens",
"Mulheres"), class = "factor"), Idade = structure(c(1L, 1L, 2L,
2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L,
10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L,
17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 1L, 1L, 2L,
2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L,
10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L,
17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L), .Label = c("0-4",
"5-9", "10-14", "15-19", "20-24", "25-29", "30-34", "35-39",
"40-44", "45-49", "50-54", "55-59", "60-64", "65-69", "70-74",
"75-79", "80-84", "85-89", "90-94", "95-99", "100+"), class = "factor"),
Perc = c(1.59237282003039, 2.0105209519813, 1.60793110934221,
2.09744619627121, 1.87169838627976, 2.53244709549787, 2.64925103118894,
3.91463341526287, 4.45980172226644, 4.93263293567532, 4.33389784785085,
5.56046023590708, 3.94310892632336, 4.72610174397569, 3.61856863738331,
4.58530663629279, 3.47709675085028, 4.54334272525628, 4.22570374122585,
4.33389784785085, 3.87304418200348, 4.26297127143788, 3.11956717223188,
3.9970330704103, 2.77523829506624, 3.43765829654823, 2.68044481745699,
2.75273174614661, 2.36449815471452, 2.53469516216054, 1.89699358551646,
2.11665098777046, 1.24617828667346, 1.7512120992836, 0.708890354295306,
1.02576163253492, 0.256279599544392, 0.442868514364281, 0.058075055452311,
0.124104493812866, 0.014237755530244, 0.0191764961285187,
-1.64990230841595, -2.11992686289791, -1.59454374412041,
-2.10606378514478, -1.92886605398365, -2.65758947305317,
-2.52659382010276, -3.80747557100893, -4.14972139807511,
-4.34251543672442, -3.83257898207542, -5.04667486793545,
-3.35711288292069, -4.24741298212606, -3.22273681163615,
-3.68083448234518, -2.84680512338085, -3.37472273844494,
-3.08509681673761, -3.19632390187423, -2.67370061746898,
-3.06606845647297, -2.14278220730172, -2.72487155365801,
-1.78646364126851, -2.24003184021999, -1.56203165277861,
-1.71901005861495, -1.35483484203585, -1.38034590057168,
-0.935570409447875, -1.06375280411028, -0.479212876925844,
-0.760185252189015, -0.228928121815239, -0.399811853245532,
-0.0618218332234279, -0.126637238584557, -0.0172351777471375,
-0.024241985671901, -0.00180910340835082, -0.00262274443978179
), Ano = c(2010, 2000, 2010, 2000, 2010, 2000, 2010, 2000,
2010, 2000, 2000, 2010, 2000, 2010, 2010, 2000, 2010, 2000,
2010, 2000, 2000, 2010, 2000, 2010, 2000, 2010, 2000, 2010,
2010, 2000, 2000, 2010, 2000, 2010, 2000, 2010, 2000, 2010,
2000, 2010, 2000, 2010, 2010, 2000, 2010, 2000, 2010, 2000,
2010, 2000, 2010, 2000, 2000, 2010, 2000, 2010, 2010, 2000,
2010, 2000, 2000, 2010, 2000, 2010, 2000, 2010, 2000, 2010,
2000, 2010, 2000, 2010, 2000, 2010, 2000, 2010, 2000, 2010,
2000, 2010, 2000, 2010, 2010, 2000)), .Names = c("Sexo",
"Idade", "Perc", "Ano"), row.names = c(NA, -84L), class = "data.frame")
require(ggplot2)
ggplot(data=piramide, aes(x = Idade, fill = Sexo, colour = Sexo, y = Perc)) + geom_bar(position = 'identity', stat = 'identity') + coord_flip() + scale_y_continuous(breaks = seq(-40, 40, 2), labels = abs(seq(-40, 40, 2))) + ylab('Percentual') + xlab('Faixa etária') + scale_colour_manual('', values = c("dodgerblue1", "pink")) + theme(legend.position = "bottom") + geom_text(aes(label = ifelse(Perc != 0,paste0(round(abs(Perc), 1),'%'), ''), y = ifelse(Sexo == 'Homens', -.1, .1), hjust=ifelse(Sexo == 'Homens', 1, 0)), size = 3) + facet_wrap(~Ano) + theme_bw() + scale_fill_manual(values=c('white', 'white'))
答案 0 :(得分:1)
要加入图例,您应在scale_fill_manual()
和scale_color_manual()
内设置相同的图例名称。
+ scale_colour_manual('', values = c("dodgerblue1", "pink"))
+ scale_fill_manual('', values=c('white', 'white'))