我想根据特定级别的因子变量的值重新排序堆积条形图。
我想根据填充变量(n.stem.niet
)的级别stemmen
将堆积的条从高到低分组。正常的堆积条形图可以使用:
ggplot(nl.melt, aes(x=naam, y=perc, fill=stemmen)) +
geom_bar(stat="identity", width=.7) +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
coord_flip() +
theme_bw()
给出了一个alfabetically有序的barplot:
我尝试了以下三段代码来重新排序我的数据,但是他们都没有给我我想要的东西:
# 1st approach
nl.melt$stemmen <- factor(nl.melt$stemmen,
levels=c("n.stem.niet","n.stem.afw","n.stem.ja","n.stem.nee","n.stem.onth"),
ordered=TRUE)
# 2nd approach
nl.melt$naam <- reorder(nl.melt$naam, as.character(nl.melt$stemmen), FUN=min)
# 3rd approach
nl.melt$stemmen <- relevel(nl.melt$stemmen,"n.stem.niet")
这应该会产生一个以“Johannes Cornelis van Baalen”为顶栏的条形图,其次是“Peter van Dalen”。
我可能会忽视某些东西,但此刻我被困住了。有什么建议吗?
dput
我的数据:
structure(list(naam = c("Auke Zijlstra", "Auke Zijlstra", "Auke Zijlstra",
"Auke Zijlstra", "Auke Zijlstra", "Bas Eickhout", "Bas Eickhout",
"Bas Eickhout", "Bas Eickhout", "Bas Eickhout", "Bastiaan Belder",
"Bastiaan Belder", "Bastiaan Belder", "Bastiaan Belder", "Bastiaan Belder",
"Corien Wortmann-Kool", "Corien Wortmann-Kool", "Corien Wortmann-Kool",
"Corien Wortmann-Kool", "Corien Wortmann-Kool", "Cornelis de Jong",
"Cornelis de Jong", "Cornelis de Jong", "Cornelis de Jong", "Cornelis de Jong",
"Daniel van der Stoep", "Daniel van der Stoep", "Daniel van der Stoep",
"Daniel van der Stoep", "Daniel van der Stoep", "Emine Bozkurt",
"Emine Bozkurt", "Emine Bozkurt", "Emine Bozkurt", "Emine Bozkurt",
"Esther de Lange", "Esther de Lange", "Esther de Lange", "Esther de Lange",
"Esther de Lange", "Gerben-Jan Gerbrandy", "Gerben-Jan Gerbrandy",
"Gerben-Jan Gerbrandy", "Gerben-Jan Gerbrandy", "Gerben-Jan Gerbrandy",
"Jan Mulder", "Jan Mulder", "Jan Mulder", "Jan Mulder", "Jan Mulder",
"Johannes Cornelis van Baalen", "Johannes Cornelis van Baalen",
"Johannes Cornelis van Baalen", "Johannes Cornelis van Baalen",
"Johannes Cornelis van Baalen", "Judith A. Merkies", "Judith A. Merkies",
"Judith A. Merkies", "Judith A. Merkies", "Judith A. Merkies",
"Judith Sargentini", "Judith Sargentini", "Judith Sargentini",
"Judith Sargentini", "Judith Sargentini", "Kartika Tamara Liotard",
"Kartika Tamara Liotard", "Kartika Tamara Liotard", "Kartika Tamara Liotard",
"Kartika Tamara Liotard", "Lambert van Nistelrooij", "Lambert van Nistelrooij",
"Lambert van Nistelrooij", "Lambert van Nistelrooij", "Lambert van Nistelrooij",
"Laurence Stassen", "Laurence Stassen", "Laurence Stassen", "Laurence Stassen",
"Laurence Stassen", "Lucas Hartong", "Lucas Hartong", "Lucas Hartong",
"Lucas Hartong", "Lucas Hartong", "Marietje Schaake", "Marietje Schaake",
"Marietje Schaake", "Marietje Schaake", "Marietje Schaake", "Marije Cornelissen",
"Marije Cornelissen", "Marije Cornelissen", "Marije Cornelissen",
"Marije Cornelissen", "Patricia van der Kammen", "Patricia van der Kammen",
"Patricia van der Kammen", "Patricia van der Kammen", "Patricia van der Kammen",
"Peter van Dalen", "Peter van Dalen", "Peter van Dalen", "Peter van Dalen",
"Peter van Dalen", "Ria Oomen-Ruijten", "Ria Oomen-Ruijten",
"Ria Oomen-Ruijten", "Ria Oomen-Ruijten", "Ria Oomen-Ruijten",
"Sophia in 't Veld", "Sophia in 't Veld", "Sophia in 't Veld",
"Sophia in 't Veld", "Sophia in 't Veld", "Thijs Berman", "Thijs Berman",
"Thijs Berman", "Thijs Berman", "Thijs Berman", "Toine Manders",
"Toine Manders", "Toine Manders", "Toine Manders", "Toine Manders",
"Wim van de Camp", "Wim van de Camp", "Wim van de Camp", "Wim van de Camp",
"Wim van de Camp"), partij = c("Partij voor de Vrijheid", "Partij voor de Vrijheid",
"Partij voor de Vrijheid", "Partij voor de Vrijheid", "Partij voor de Vrijheid",
"GroenLinks", "GroenLinks", "GroenLinks", "GroenLinks", "GroenLinks",
"Staatkundig Gereformeerde Partij", "Staatkundig Gereformeerde Partij",
"Staatkundig Gereformeerde Partij", "Staatkundig Gereformeerde Partij",
"Staatkundig Gereformeerde Partij", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Christen Democratisch Appel",
"Socialistische Partij", "Socialistische Partij", "Socialistische Partij",
"Socialistische Partij", "Socialistische Partij", "Independent",
"Independent", "Independent", "Independent", "Independent", "Partij van de Arbeid",
"Partij van de Arbeid", "Partij van de Arbeid", "Partij van de Arbeid",
"Partij van de Arbeid", "Christen Democratisch Appel", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Democraten 66", "Democraten 66",
"Democraten 66", "Democraten 66", "Democraten 66", "Volkspartij voor Vrijheid en Democratie",
"Volkspartij voor Vrijheid en Democratie", "Volkspartij voor Vrijheid en Democratie",
"Volkspartij voor Vrijheid en Democratie", "Volkspartij voor Vrijheid en Democratie",
"Volkspartij voor Vrijheid en Democratie", "Volkspartij voor Vrijheid en Democratie",
"Volkspartij voor Vrijheid en Democratie", "Volkspartij voor Vrijheid en Democratie",
"Volkspartij voor Vrijheid en Democratie", "Partij van de Arbeid",
"Partij van de Arbeid", "Partij van de Arbeid", "Partij van de Arbeid",
"Partij van de Arbeid", "GroenLinks", "GroenLinks", "GroenLinks",
"GroenLinks", "GroenLinks", "Independent", "Independent", "Independent",
"Independent", "Independent", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Christen Democratisch Appel",
"Partij voor de Vrijheid", "Partij voor de Vrijheid", "Partij voor de Vrijheid",
"Partij voor de Vrijheid", "Partij voor de Vrijheid", "Partij voor de Vrijheid",
"Partij voor de Vrijheid", "Partij voor de Vrijheid", "Partij voor de Vrijheid",
"Partij voor de Vrijheid", "Democraten 66", "Democraten 66",
"Democraten 66", "Democraten 66", "Democraten 66", "GroenLinks",
"GroenLinks", "GroenLinks", "GroenLinks", "GroenLinks", "Partij voor de Vrijheid",
"Partij voor de Vrijheid", "Partij voor de Vrijheid", "Partij voor de Vrijheid",
"Partij voor de Vrijheid", "ChristenUnie", "ChristenUnie", "ChristenUnie",
"ChristenUnie", "ChristenUnie", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Christen Democratisch Appel",
"Democraten 66", "Democraten 66", "Democraten 66", "Democraten 66",
"Democraten 66", "Partij van de Arbeid", "Partij van de Arbeid",
"Partij van de Arbeid", "Partij van de Arbeid", "Partij van de Arbeid",
"Volkspartij voor Vrijheid en Democratie", "Volkspartij voor Vrijheid en Democratie",
"Volkspartij voor Vrijheid en Democratie", "Volkspartij voor Vrijheid en Democratie",
"Volkspartij voor Vrijheid en Democratie", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Christen Democratisch Appel",
"Christen Democratisch Appel", "Christen Democratisch Appel"),
afk = c("PVV", "PVV", "PVV", "PVV", "PVV", "GL", "GL", "GL",
"GL", "GL", "SGP", "SGP", "SGP", "SGP", "SGP", "CDA", "CDA",
"CDA", "CDA", "CDA", "SP", "SP", "SP", "SP", "SP", "PVV",
"PVV", "PVV", "PVV", "PVV", "PvdA", "PvdA", "PvdA", "PvdA",
"PvdA", "CDA", "CDA", "CDA", "CDA", "CDA", "D66", "D66",
"D66", "D66", "D66", "VVD", "VVD", "VVD", "VVD", "VVD", "VVD",
"VVD", "VVD", "VVD", "VVD", "PvdA", "PvdA", "PvdA", "PvdA",
"PvdA", "GL", "GL", "GL", "GL", "GL", "SP", "SP", "SP", "SP",
"SP", "CDA", "CDA", "CDA", "CDA", "CDA", "PVV", "PVV", "PVV",
"PVV", "PVV", "PVV", "PVV", "PVV", "PVV", "PVV", "D66", "D66",
"D66", "D66", "D66", "GL", "GL", "GL", "GL", "GL", "PVV",
"PVV", "PVV", "PVV", "PVV", "CU", "CU", "CU", "CU", "CU",
"CDA", "CDA", "CDA", "CDA", "CDA", "D66", "D66", "D66", "D66",
"D66", "PvdA", "PvdA", "PvdA", "PvdA", "PvdA", "VVD", "VVD",
"VVD", "VVD", "VVD", "CDA", "CDA", "CDA", "CDA", "CDA"),
stemmen = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L,
5L), class = "factor", .Label = c("n.stem.ja", "n.stem.nee",
"n.stem.onth", "n.stem.niet", "n.stem.afw")), value = c(38,
215, 19, 0, 25, 393, 68, 24, 20, 9, 430, 61, 20, 0, 2, 457,
14, 0, 20, 23, 236, 134, 76, 28, 41, 120, 256, 37, 13, 20,
426, 23, 5, 19, 42, 417, 22, 4, 33, 42, 424, 14, 9, 19, 44,
365, 8, 4, 16, 28, 242, 13, 5, 159, 95, 452, 19, 5, 20, 13,
390, 73, 24, 20, 6, 232, 129, 69, 18, 67, 427, 13, 0, 28,
46, 102, 353, 29, 5, 25, 84, 274, 27, 16, 23, 449, 19, 5,
15, 26, 323, 61, 20, 45, 65, 17, 97, 15, 3, 11, 352, 57,
30, 48, 31, 421, 22, 4, 33, 38, 466, 15, 5, 10, 13, 438,
23, 5, 25, 23, 455, 19, 15, 31, 0, 456, 19, 0, 30, 9), perc = c(12.7946127946128,
72.3905723905724, 6.3973063973064, 0, 8.41750841750842, 76.4591439688716,
13.2295719844358, 4.66926070038911, 3.89105058365759, 1.75097276264591,
83.8206627680312, 11.8908382066277, 3.89863547758285, 0,
0.389863547758285, 88.9105058365759, 2.72373540856031, 0,
3.89105058365759, 4.47470817120623, 45.8252427184466, 26.0194174757282,
14.7572815533981, 5.4368932038835, 7.96116504854369, 26.9058295964126,
57.3991031390135, 8.29596412556054, 2.91479820627803, 4.48430493273543,
82.7184466019417, 4.46601941747573, 0.970873786407767, 3.68932038834951,
8.15533980582524, 80.5019305019305, 4.24710424710425, 0.772200772200772,
6.37065637065637, 8.10810810810811, 83.1372549019608, 2.74509803921569,
1.76470588235294, 3.72549019607843, 8.62745098039216, 86.6983372921615,
1.90023752969121, 0.950118764845606, 3.80047505938242, 6.65083135391924,
47.0817120622568, 2.52918287937743, 0.972762645914397, 30.9338521400778,
18.4824902723735, 88.8015717092338, 3.7328094302554, 0.982318271119843,
3.92927308447937, 2.55402750491159, 76.0233918128655, 14.2300194931774,
4.67836257309941, 3.89863547758285, 1.16959064327485, 45.0485436893204,
25.0485436893204, 13.3980582524272, 3.49514563106796, 13.0097087378641,
83.0739299610895, 2.52918287937743, 0, 5.44747081712062,
8.94941634241245, 19.8443579766537, 68.6770428015564, 5.6420233463035,
0.972762645914397, 4.86381322957198, 19.811320754717, 64.622641509434,
6.36792452830189, 3.77358490566038, 5.42452830188679, 87.3540856031128,
3.69649805447471, 0.972762645914397, 2.91828793774319, 5.05836575875486,
62.84046692607, 11.8677042801556, 3.89105058365759, 8.75486381322957,
12.6459143968872, 11.8881118881119, 67.8321678321678, 10.4895104895105,
2.0979020979021, 7.69230769230769, 67.953667953668, 11.003861003861,
5.79150579150579, 9.26640926640927, 5.98455598455598, 81.2741312741313,
4.24710424710425, 0.772200772200772, 6.37065637065637, 7.33590733590734,
91.5520628683693, 2.94695481335953, 0.982318271119843, 1.96463654223969,
2.55402750491159, 85.2140077821012, 4.47470817120623, 0.972762645914397,
4.86381322957198, 4.47470817120623, 87.5, 3.65384615384615,
2.88461538461538, 5.96153846153846, 0, 88.715953307393, 3.69649805447471,
0, 5.83657587548638, 1.75097276264591)), .Names = c("naam",
"partij", "afk", "stemmen", "value", "perc"), row.names = c(NA,
130L), class = c("grouped_dt", "tbl_dt", "tbl", "grouped_dt",
"tbl_dt", "tbl", "data.table", "data.frame"), .internal.selfref = <pointer: 0x7f9c22002d78>, sorted = "naam", vars = list(
naam))
答案 0 :(得分:2)
分解你的问题,似乎你想:
n.stem.niet
value
的值
naam
的因子级别。试试这个:
naamLevels <- with(nl.melt[nl.melt$stemmen == "n.stem.niet"], naam[order(value)])
nl.melt$naam <- factor(nl.melt$naam, levels = naamLevels)
然后绘制:
ggplot(nl.melt, aes(x=naam, y=perc, fill=stemmen)) +
geom_bar(stat="identity", width=.7) +
scale_x_discrete(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
coord_flip() +
theme_bw()
答案 1 :(得分:1)
这也可以通过
来完成nl.melt$naam <- with(nl.melt, reorder(naam,
ifelse(stemmen=="n.stem.niet", perc,0), FUN=max))
后面跟着相同的绘图命令。