我无法使用该reorder()
函数在带有图例的分组条形图中排序我的数据。这是我的代码:
library(reshape2)
my_data_long <- melt(merged_dt, id.vars = c("Neighborhood.Name"))
ggplot(data=my_data_long, aes(x= reorder(Neighborhood.Name, value), y=value, fill=variable, color=variable, alpha=variable)) +
geom_bar(stat="identity", position ="identity") +
scale_colour_manual(values=c("lightblue4", "red")) +
scale_fill_manual(values=c("lightblue", "pink")) +
scale_alpha_manual(values=c(.3, .8)) +
coord_flip() +
labs(x="Neighborhood Name", y="")
这给出了以下输出:
它似乎对这些条进行了排序,但是仍然有一些蓝色条不合适。我怎样才能解决这个问题?
我的数据集如下所示:
# dput(my_data_long)
setDT(structure(list(Neighborhood.Name = c("la Nova Esquerra de l'Eixample", "Sant Andreu", "la Sagrada Família", "la Vila de Gràcia", "el Raval", "les Corts", "Sant Gervasi - Galvany", "la Dreta de l'Eixample", "l'Antiga Esquerra de l'Eixample", "Sants", "el Poble Sec", "Sant Antoni", "el Camp de l'Arpa del Clot", "el Guinardó", "el Camp d'en Grassot i Gràcia Nova", "el Poblenou", "el Fort Pienc", "el Carmel", "la Marina de Port", "la Sagrera", "la Verneda i la Pau", "el Putxet i el Farró", "el Clot", "Horta", "la Prosperitat", "Sant Martí de Provençals", "el Baix Guinardó", "Vilapicina i la Torre Llobeta", "Porta", "Sants - Badal", "Sant Gervasi - la Bonanova", "la Maternitat i Sant Ramon", "Sant Pere, Santa Caterina i la Ribera", "Sarrià", "el Besòs i el Maresme", "Navas", "Provençals del Poblenou", "la Bordeta", "el Barri Gòtic", "Hostafrancs", "la Barceloneta", "les Tres Torres", "Vallcarca i els Penitents", "el Turó de la Peira", "la Guineueta", "les Roquetes", "el Parc i la Llacuna del Poblenou", "el Congrés i els Indians", "la Salut", "Diagonal Mar i el Front Marítim del Poblenou", "Verdun", "el Bon Pastor", "la Teixonera", "Pedralbes", "la Font de la Guatlla", "Ciutat Meridiana", "la Trinitat Vella", "la Font d'en Fargues", "Can Baró", "la Vila Olímpica del Poblenou", "el Coll", "la Trinitat Nova", "Canyelles", "Sant Genís dels Agudells", "la Vall d'Hebron", "Montbau", "Vallvidrera, el Tibidabo i les Planes", "Torre Baró", "Baró de Viver", "Can Peguera", "Vallbona", "la Clota", "la Nova Esquerra de l'Eixample", "Sant Andreu", "la Sagrada Família", "la Vila de Gràcia", "el Raval", "les Corts", "Sant Gervasi - Galvany", "la Dreta de l'Eixample", "l'Antiga Esquerra de l'Eixample", "Sants", "el Poble Sec", "Sant Antoni", "el Camp de l'Arpa del Clot", "el Guinardó", "el Camp d'en Grassot i Gràcia Nova", "el Poblenou", "el Fort Pienc", "el Carmel", "la Marina de Port", "la Sagrera", "la Verneda i la Pau", "el Putxet i el Farró", "el Clot", "Horta", "la Prosperitat", "Sant Martí de Provençals", "el Baix Guinardó", "Vilapicina i la Torre Llobeta", "Porta", "Sants - Badal", "Sant Gervasi - la Bonanova", "la Maternitat i Sant Ramon", "Sant Pere, Santa Caterina i la Ribera", "Sarrià", "el Besòs i el Maresme", "Navas", "Provençals del Poblenou", "la Bordeta", "el Barri Gòtic", "Hostafrancs", "la Barceloneta", "les Tres Torres", "Vallcarca i els Penitents", "el Turó de la Peira", "la Guineueta", "les Roquetes", "el Parc i la Llacuna del Poblenou", "el Congrés i els Indians", "la Salut", "Diagonal Mar i el Front Marítim del Poblenou", "Verdun", "el Bon Pastor", "la Teixonera", "Pedralbes", "la Font de la Guatlla", "Ciutat Meridiana", "la Trinitat Vella", "la Font d'en Fargues", "Can Baró", "la Vila Olímpica del Poblenou", "el Coll", "la Trinitat Nova", "Canyelles", "Sant Genís dels Agudells", "la Vall d'Hebron", "Montbau", "Vallvidrera, el Tibidabo i les Planes", "Torre Baró", "Baró de Viver", "Can Peguera", "Vallbona", "la Clota"), variable = structure(c(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, 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, 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, 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), .Label = c("Total Population * 10000", "Accidents per Capita * 1000"), class = "factor"), value = c(25.8286, 24.5357, 23.0373, 22.3985, 20.8189, 20.3063, 19.9818, 19.1796, 18.6469, 18.1432, 17.832, 17.1147, 16.9354, 15.7456, 15.1377, 14.0174, 14.0083, 13.7163, 13.0223, 12.7382, 12.6615, 12.5826, 11.7794, 11.6622, 11.4361, 11.4266, 11.3924, 11.2236, 10.7499, 10.7343, 10.6151, 10.4825, 10.1644, 10.119, 9.8342, 9.683, 8.7187, 8.1769, 7.2458, 7.053, 6.8889, 6.7469, 6.722, 6.7157, 6.622, 6.6099, 6.4822, 6.1373, 5.7926, 5.3484, 5.3343, 5.2013, 5.0153, 5.0013, 4.6176, 4.3006, 4.2337, 4.0379, 3.925, 3.8817, 3.1705, 3.1384, 3.0816, 3.0129, 2.4497, 2.2454, 1.8505, 1.0716, 1.0327, 0.9842, 0.549, 0.231, 1.4983390505099, 0.802911675639986, 1.63647649681169, 0.794696073397772, 0.720499161819308, 1.57094103800298, 1.92675334554445, 6.08458987674404, 3.15333916093292, 0.975572115172627, 1.31224764468371, 1.36140277071757, 1.01562407737638, 0.717660806828574, 0.805934851397504, 1.05583061052692, 1.97739911338278, 0.67073481915677, 0.806309177334265, 0.549528190796188, 0.60814279508747, 1.06496272630458, 1.28189890826358, 1.26905729622198, 0.638329500441584, 0.700120770832969, 0.974333766370563, 0.784062154745358, 0.911636387315231, 0.661431113346935, 1.96889336888018, 1.93656093489149, 1.31832670890559, 1.86777349540468, 0.772813243578532, 0.660952184240421, 2.07599756844484, 1.1862686348127, 2.45659554500538, 1.77229547710194, 2.00322257544746, 2.38628110687871, 2.23147872656947, 0.35737153237935, 0.785261250377529, 0.605152876745488, 2.83854247014902, 0.619164779300344, 1.00127749197252, 2.72978834791713, 0.449918452280524, 2.48014919347086, 0.618108587721572, 4.07893947573631, 0.996188496188496, 0.255778263498116, 1.60616009636961, 1.04014462963422, 0.382165605095541, 3.11719092150346, 0.725437628134364, 2.38975274024981, 1.10332294911734, 1.72591191211125, 2.61256480385353, 2.2713102342567, 1.40502566873818, 0.653228816722658, 3.00183983731965, 0.812842918106076, 1.45719489981785, 10.3896103896104)), row.names = c(NA, -144L), class = c("data.table", "data.frame")))
我意识到融化功能使数据框变得混乱,但“值”列仍然正确。但是,通过查看“值”列,它表明 R 输出(如条形图的图像所示)没有正确排列条形图。由于在数据框中,“值”列已正确排序(降序)。