-1

I'm trying to reorder the bars on my bargraph so they are biggest to smallest, ordered by log(Max_N) rather than alphabetically. I've tried using forcats but it brings up an error message. This is my code so far, whats going wrong? I'm trying to teach myself how to use ggplot, so please do point out any errors I've made, because I am self-taught!

library("ggplot2")
library (forcats)
test<-ggplot(all, aes(x=all$Species, y=log(all$Max_N+1))) + 
geom_bar(stat = "identity") +
coord_flip() 

test <- test + labs(title = "",
              subtitle = "",
              caption = "",
              y = "Log(MaxN)", x = "Species",
              tag = "")
test %>%
  mutate(name = fct_reorder(Species, log(Max_N+1)) %>%

Original plot

a) Here's the reproducible example requested (I hope that's what you mean?)

 structure(list(Site = c("Mylor", "Mylor", "Mylor", "Mylor", "Mylor", 
 "Mylor", "Mylor", "Mylor", "Mylor", "Mylor"), Trial = c(1L, 1L, 
 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), Location = c("centre", "edge", 
 "edge", "edge", "edge", "edge", "edge", "centre", "centre", "centre"
 ), Bait = c("whitefish", "whitefish", "whitefish", "whitefish", 
 "whitefish", "whitefish", "whitefish", "whitefish", "whitefish", 
 "whitefish"), ECP = c("yes", "yes", "yes", "yes", "yes", "yes", 
 "yes", "no", "no", "no"), SSP = c("no", "no", "no", "no", "no", 
 "no", "no", "no", "no", "no"), KP = c("no", "no", "no", "no", 
 "no", "no", "no", "no", "no", "no"), OSP = c("no", "no", "no", 
 "no", "no", "no", "no", "no", "no", "no"), Density = 
 c("dense_seagrass", 
  "dense_seagrass", "dense_seagrass", "dense_seagrass", 
 "dense_seagrass", 
 "dense_seagrass", "dense_seagrass", "dense_seagrass", 
 "dense_seagrass", 
 "dense_seagrass"), Viz = c(1.75, 1.75, 1.75, 1.75, 1.75, 1.75, 
 1.75, 4.5, 4.5, 4.5), Species = c("sea_sprat", "corkwing_wrasse", 
 "pollack", "sand_smelt", "unknown_wrasse", "two_spotted_goby", 
 "unknown_wrasse", "mackerel", "sand_smelt", "mullet"), AJ = c("juv", 
 "juv", "juv", "adu", "adu", "adu", "adu", "adu", "adu", "adu"
 ), FG = c("pelagic_neritic", "reef_associated", "pelagic_neritic", 
 "pelagic_neritic", "reef_associated", "reef_associated", 
 "reef_associated", 
 "pelagic_neritic", "pelagic_neritic", "pelagic_neritic"), Max_N = 
 c(1L, 
 1L, 2L, 2L, 2L, 2L, 1L, 1L, 26L, 1L)), row.names = c(NA, 10L), class 
 = "data.frame")

b) so I changed the Species to name as suggested, and it has order some but not others? Changed code to this.

 test<-ggplot(all2, aes(x=name, y=log(Max_N+1))) + 
  geom_bar(stat = "identity") +
  coord_flip()  

plot after changing code to above

Pops
  • 3
  • 2
  • 1
    `mutate` will not work on a ggplot object. Reorder the column before passing it to ggplot, i.e. do `all <- all %>% mutate(name = fct_reorder(Species, log(Max_N+1))`. Additionally, don't use `all$...` inside aes. Simply do `aes(x = Species, y=log(Max_N+1))`. – stefan Feb 13 '22 at 11:13
  • @stefan I have now tried this and run the code, but the same result still comes up. I cleared the environment and did it again to the same end. Any suggestions? – Pops Feb 13 '22 at 11:21
  • 3
    Hm. In that case we need a reproducible example including a snippet of your data or some fake data. To this end have a look at how to make a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). To share your data, you could type `dput(NAME_OF_DATASET)` into the console and copy the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 10))` for the first ten rows of data. – stefan Feb 13 '22 at 11:37
  • 1
    Without your code and graph it's hard to say. But I'd guess that you forget to change the `aes(x = )` from `Species` to `name` (if you saved the `fct_reorder` result to the `name` column) – benson23 Feb 13 '22 at 11:37

1 Answers1

0

Acutally if I were you, I will just have a reorder inside ggplot(aes()), then you don't need to change anything in your data.

If you want to plot this bar chart that sum up all Max_N within a species, you can do a group_by and summarise before piping into ggplot. This will NOT change your original all dataframe.

library(tidyverse)

all %>% group_by(Species) %>% summarize(Max_N = sum(Max_N)) %>% 
  ggplot(all, aes(x = reorder(Species, Max_N), y =log(Max_N+1))) + 
  geom_bar(stat = "identity") +
  coord_flip() + labs(x = "Species", y = "Log(MaxN)")

bar_chart_summarise_reorder

benson23
  • 16,369
  • 9
  • 19
  • 38
  • Thank you so much for all your help, but I've just tried this too and for some reason it changes the order, but not by MaxN. Similar to how Unknown Wrasse hasn't followed the pattern. The data has duplicates for all species as these are recorded for each separate trial. – Pops Feb 13 '22 at 16:55
  • See if my edited answer helps :) – benson23 Feb 14 '22 at 07:10