0

I am having trouble ordering my data in my grouped bar plot with legend using the reorder()function. Here is my code:

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="") 

This gives the following output:

enter image description here

It seems to somewhat order the bars however there are still some of the blue bars out of place. How can I fix this?

My data set looks like this:

# 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")))

I realize the melt function made the data frame messy but the column "value" remains correct. However by looking at the "value" column, it suggests that the R output (as seen in the image of the bar chart) does not order the bars correctly. Since in the data frame the "value" column is sorted correctly (descending).

user438383
  • 5,716
  • 8
  • 28
  • 43
Iceidic
  • 1
  • 1
  • Please do not post an image of code/data/errors: it breaks screen-readers and it cannot be copied or searched (ref: https://meta.stackoverflow.com/a/285557 and https://xkcd.com/2116/). Please just include the code, console output, or data (e.g., `data.frame(...)` or the output from `dput(head(x))`) directly. – r2evans Jan 19 '22 at 14:19
  • Please be more attentive with tags. For instance, even if you're *using* RStudio, the [tag:rstudio] tag explicitly says *"DO NOT use this tag for general R programming problems, just use the R tag. ONLY use for RStudio-specific questions"*, and this question is not about how to use the IDE. Also, while you are `melt`ing data, your issue has nothing to do with that step, so including the [tag:melt] is a red herring. I have no idea what [tag:grouped-list] means. Bottom line, please don't add extra tags. – r2evans Jan 19 '22 at 14:21
  • 1
    Most if not all questions about how to get `ggplot2` to change the order of a categorical axis is resolved by using `factor` and manually controlling the order of its `levels`. – r2evans Jan 19 '22 at 14:23
  • thank you @r2evans . I am sorry for not following the posting guidelines correctly, I will be sure to watch out for these the next time. I have tried using the factor function and have had no luck using this. Hence why I came here to see if anyone has any other ideas that might help. – Iceidic Jan 19 '22 at 14:28
  • No worries on the posts, it's a common new-user thing. I do not mean to come across harshly or in a judgemental thing, just pointing out something that causes some "noise" (minor) in the forum. For the use of `factor`, please go through the dupe-link (perhaps also https://stackoverflow.com/q/12774210/3358272 and https://stackoverflow.com/q/18401931/3358272). If after that you cannot figure it out, then come back with code that attempts that. @ping me and I can reopen the question if it isn't still a dupe. Good luck! – r2evans Jan 19 '22 at 14:34
  • 1
    Thanks! Of course I completely understand. The problem with the links you shared is that these are not grouped bar charts i.e. they are not molten together using the "melt" function. Therefore the tipps they use there do not work for my code. I will see what I can do and do as you said if it wont work out. – Iceidic Jan 19 '22 at 14:38
  • @r2evans Hello, I have restated the question slightly and changed some of the formatting. I also included my attempt at solving the issue and stated my reasoning as to why it does not work. I would appreciate it if my question would be considered to be reopened. Thank you! – Iceidic Jan 19 '22 at 15:32
  • 1
    *Please* use `dput(.)` for your sample data, it makes a difference. – r2evans Jan 19 '22 at 15:34
  • @r2evans added dput(.) as requested. – Iceidic Jan 19 '22 at 15:40
  • 1
    Let me be more clear. The purpose of using `dput(.)` is that it is the easiest way to give us unambiguous data (not hiding factors, POSIXt, or similar classes, since these classes could also be `character` when all we have is console output). (It is also the fastest way for us to actually get the data. Many reasons hamper that.) In your case, you prefixed every line with `##`, which defeats us using it. Plus the `.internal.selfref=` included in the `structure` is not usable, so as a courtesy I tend to remove it and wrap in `setDT`. More the former issue than the latter. I've fixed it. – r2evans Jan 19 '22 at 16:03
  • Thank you so much. I am a beginner with R and was a little confused, but now it makes sense. Thanks for your help :) – Iceidic Jan 19 '22 at 16:05
  • The issue with the ordering is that you are using `position="identity"`. If you change to `position="stack"`, you will see that all bars are ordered correctly. See, for instance, that while there are two values for `"la Clota"`, the plot only shows one because the second is eclipsed by the first. – r2evans Jan 19 '22 at 16:07
  • This may not *resolve* your issue, but I think it starts to *explain* it. Knowing that `"la Clota"` has both a mid-range value and a very-low value, do you think that it should be shown twice on the y-axis? If not, then how do you propose it orders things? Normal options for `position=` are: `"identity"`, `"stack"`, `"dodge"`, `"dodge2"`, and `"fill"`. – r2evans Jan 19 '22 at 16:07
  • 1
    You are a legend. Been working on this for hours and changing the ```position="identity"```to ```position="stack"``` resolved it. It works perfectly now. Thank you so much! I hope you have a nice rest of the day! – Iceidic Jan 19 '22 at 16:15

0 Answers0