-1

I have the following code:

figg4 <- lala4 %>% gather(key, value, -Species_Name) %>%  
  mutate (Species_Name = factor(Species_Name, 
levels=c('Dasyprocta punctata',
'Cuniculus paca','Large Rats',
 'Heteromys unknown', 'Sciurus variegatoides',
 'Sciurus granatensis','Dasypus novemcinctus',
'Didelphis marsupialis',  'Philander opossum',
'Metachirus nudicaudatus', 'Nasua narica',
 'Procyon lotor', 'Eira barbara',
'Galictis vittata', 'Leopardus pardalis'))) %>% 
 ggplot(aes(x=Species_Name, y=value, 
             fill=key)) + coord_flip() + geom_col (position = "stack") +
theme(panel.background = element_blank()) + bbc_style() +
  labs(title = "Species occupancy by Site Type")+
  scale_fill_manual(values = c("#333333","#1380A1", "#FAAB18"))

I get bar graph which is listing the names in the reverse order, I want to make them appear in the order that I have written the levels in... how do I do so?

I tried using fct_reorder from forcats by adding the following code

mutate(name = fct_reorder(Species_Name, desc(value)))

But that did not change the order. I am quite new to r and not sure of how to do this. Would be grateful for any help

Here is dput output for the source:

dput(lala4) structure(list(Species_Name = structure(c(9L, 12L, 13L, 14L, 19L, 22L, 27L, 46L, 41L, 42L, 10L, 15L, 32L, 33L, 24L), .Label = c("Buteo platypterus", "Canis latrans", "Cathartes aura", "Catharus unknown", "Catharus ustulatus", "Cebus capucinus", "Chordeiles unknown", "Conepatus semistriatus", "Crax rubra", "Crypturellus cinnamomeus", "Cuniculus paca", "Dasyprocta punctata", "Dasypus novemcinctus", "Didelphis marsupialis", "Eira barbara", "Galictis vittata", "Geotrygon montana", "Geotrygon violacea", "Heteromys unknown", "Holcosus quadrilineatus", "Large Rats", "Leopardus pardalis", "Leopardus wiedii", "Leptotila unknown", "Melozone unknown", "Metachirus nudicaudatus", "Nasua narica", "Odocoileus virginianus", "Panthera onca", "Parkesia noveboracensis", "Pecari tajacu", "Penelopina nigra", "Philander opossum", "Piaya cayana", "Procyon lotor", "Puma concolor", "Puma yagouaroundi", "Sciurus granatensis", "Sciurus variegatoides", "Setophaga unknown", "Sylvilagus sp ", "Tamandua mexicana", "Tapirus bairdii", "Tayassu pecari", "Tigrisoma fasciatum", "Tinamus major"), class = "factor"), Forest Area (<5ha) = c(0.067307692, 0.134615385, 0.173076923, 0.144230769, 0.019230769, 0.086538462, 0.192307692, 0.009615385, 0.163461538, 0.038461538, 0, 0.019230769, 0, 0.163461538, 0.153846154), Forest Area (5-27ha) = c(0.067307692, 0.317307692, 0.269230769, 0.096153846, 0.038461538, 0.105769231, 0.192307692, 0.115384615, 0.134615385, 0.057692308, 0, 0.096153846, 0, 0.076923077, 0.173076923), Forest Area (>350ha) = c(0.163461538, 0.384615385, 0.278846154, 0.201923077, 0.105769231, 0.067307692, 0.144230769, 0.298076923, 0.028846154, 0.048076923, 0.086538462, 0.038461538, 0.019230769, 0.028846154, 0.125)), row.names = c(NA, 15L), class = "data.frame")

  • 2
    How about: `ggplot(aes(x=forcats::fct_rev(Species_Name)...`? – Ben Mar 06 '20 at 03:35
  • 1
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data and some output – camille Mar 06 '20 at 04:03
  • Perhaps, this answer https://stackoverflow.com/a/42713157/3817004 gives some hints. – Uwe Mar 06 '20 at 06:18

1 Answers1

2

You need to redefine the factor as an ordered factor first.

Try just fixing the code where you define the factor by adding

ordered = TRUE

This should probably work:

figg4 <- lala4 %>% gather(key, value, -Species_Name) %>%  
  mutate (Species_Name = factor(Species_Name, 
levels=c('Dasyprocta punctata',
'Cuniculus paca','Large Rats',
 'Heteromys unknown', 'Sciurus variegatoides',
 'Sciurus granatensis','Dasypus novemcinctus',
'Didelphis marsupialis',  'Philander opossum',
'Metachirus nudicaudatus', 'Nasua narica',
 'Procyon lotor', 'Eira barbara',
'Galictis vittata', 'Leopardus pardalis'), ordered = TRUE)) %>% 
 ggplot(aes(x=Species_Name, y=value, 
             fill=key)) + coord_flip() + geom_col (position = "stack") +
theme(panel.background = element_blank()) + bbc_style() +
  labs(title = "Species occupancy by Site Type")+
  scale_fill_manual(values = c("#333333","#1380A1", "#FAAB18"))

I can't run it because I don't have the lala4 data to test though.

Alison Bennett
  • 285
  • 1
  • 8
  • 20
  • Thank you Alison, I did try it but it dint work. I instead manually created a reverse order and added that reversed order --- which gave me the graph in the order that I wanted it to be but just in case you were interested I added the dput output of the dataframe. – Keerthi Krutha Mar 07 '20 at 00:43
  • Interesting, I'm not sure about the dput. Perhaps if you defined Species_Name as an ordered factor in the lala4 data before you call ggplot it might work. Anyway, you found a solution to your problem. Perhaps someone else knows why. It's much easier for people to help if you provide a reproducable example - then people can test it themselves and provide you with a clear solution. Look at the note by @camille above. – Alison Bennett Mar 07 '20 at 03:28
  • Will do that Alison :)! Thank you for the response. – Keerthi Krutha Mar 13 '20 at 00:05