数据示例(daten),见下文:
bundesland gender freq percent_freq label_hight labels
4 Baden-Württemberg w 125 71.428571 35.71429 71.4 %
5 Baden-Württemberg m 43 24.571429 83.71429 24.6 %
6 Baden-Württemberg k.A. 7 4.000000 98.00000 4 %
7 Bayern w 122 68.156425 34.07821 68.2 %
8 Bayern m 43 24.022346 80.16760 24 %
9 Bayern k.A. 14 7.821229 96.08939 7.8 %
10 Berlin w 41 63.076923 31.53846 63.1 %
11 Berlin m 22 33.846154 80.00000 33.8 %
12 Berlin k.A. 2 3.076923 98.46154 3.1 %
我使用此数据绘制堆积的条形图。
p <- ggplot(daten, aes(x=bundesland, y=percent_freq, fill=gender)) +
geom_bar(stat="identity") + # contour colour
guides(fill=guide_legend(reverse=TRUE)) + # reverse legend
geom_text(aes(y=label_hight, label=labels))+ # add the labels
theme_hc() +
scale_fill_tableau() +
guides(colour = guide_legend(override.aes = list(linetype = 0 )),
fill = guide_legend(override.aes = list(linetype = 0 )),
linetype = guide_legend()) + theme(legend.title=element_blank())+
coord_flip()
现在我想在percent_freq之后订购情节,其中gender = w。我知道Reorder stacked barplot x based on fill values with ggplot2,所以我试过了:
daten$bundesland <- reorder(daten$bundesland, data$gender, function(x) max(table()[1]))
但我明白了:
Error in tapply(X = X, INDEX = x, FUN = FUN, ...) :
Argumente müssen dieselbe Länge haben
(参数必须具有相同的长度)
我做错了什么?或者有更好的方法来做到这一点吗?
数据:
structure(list(bundesland = structure(c(2L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 5L), .Label = c("", "Baden-Württemberg", "Bayern",
"Berlin", "Brandenburg", "Bremen", "Hamburg/", "Hessen", "Mecklenburg-Vorpommern",
"Niedersachsen", "Nordrhein-Westfalen", "Rheinland-Pfalz", "Saarland",
"Sachsen", "Sachsen-Anhalt", "Schleswig-Holstein", "Thüringen"
), class = "factor"), gender = structure(c(1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L), .Label = c("w", "m", "k.A."), class = "factor"),
freq = c(125L, 43L, 7L, 122L, 43L, 14L, 41L, 22L, 2L, 109L
), percent_freq = c(71.4285714285714, 24.5714285714286, 4,
68.1564245810056, 24.0223463687151, 7.82122905027933, 63.0769230769231,
33.8461538461538, 3.07692307692308, 81.3432835820896), label_hight = c(35.7142857142857,
83.7142857142857, 98, 34.0782122905028, 80.1675977653631,
96.0893854748603, 31.5384615384615, 80, 98.4615384615385,
40.6716417910448), labels = structure(c(6L, 4L, 5L, 8L, 7L,
9L, 12L, 11L, 10L, 15L), .Label = c("10.8 %", "8.1 %", "81.1 %",
"24.6 %", "4 %", "71.4 %", "24 %", "68.2 %", "7.8 %", "3.1 %",
"33.8 %", "63.1 %", "13.4 %", "5.2 %", "81.3 %", "11.1 %",
"77.8 %", "18.2 %", "81.8 %", "1.1 %", "15.9 %", "83 %",
"33.3 %", "66.7 %", "11.6 %", "23.3 %", "65.1 %", "27.3 %",
"6.5 %", "66.2 %", "3 %", "69.7 %", "14.3 %", "52.4 %", "16.1 %",
"72.8 %", "14.5 %", "7.2 %", "78.3 %", "16.2 %", "21.2 %",
"62.6 %", "10.7 %", "16.7 %", "72.6 %"), class = "factor")), .Names = c("bundesland",
"gender", "freq", "percent_freq", "label_hight", "labels"), row.names = 4:13, class = "data.frame")
答案 0 :(得分:1)
在重新排序之前,您需要将数据子集化为女性。
female <- subset(daten, gender == "w")
female$bundesland <- reorder(female$bundesland, female$percent_freq)
daten$bundesland <- factor(daten$bundesland, levels = levels(female$bundesland))