3

So, I have a dataset which looks like this.

I'm tasked with creating a smooth faceted visualization which shows each coral's bleaching rate at each site which I've successfully done so like this:

(I FULLY realize that this code might be bad and have some mistakes in it and I'd really appreciate it if people could tell me ways to improve it or correct some grave errors in it).

coral_data <- read.csv("file.csv")

#options(warn=-1)

library(ggplot2)

ggplot(coral_data, aes(x=year, y=value, colour=coralType, group=coralType)) +
  geom_smooth(method="lm", se=F) +
  scale_x_continuous(name="Year", breaks=c(2010, 2013, 2016)) + 
  scale_y_discrete(breaks = seq(0, 100, by = 10)) +
  facet_grid(coralType ~ location, scales="free")+
  expand_limits(y=0) +
  labs(x="\nBleaching Rate", y="Year", title="Coral Bleaching for different corals at different sites over the years\n")

enter image description here

But, I also have to order the facets by lattitudes (currently, its like site01, site02, etc but I want the faceted sites to be ordered w.r.t. their lattitude values, be it ascending or descending) but sadly I have no idea as to how I'm going to do that.

Thus, could someone please tell me how to go about doing this?

dealvidit
  • 45
  • 6
  • 2
    [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. We can't run code on a picture of your data, and we can't see the chart you're trying to change. You probably need to make your facetting variable into a factor which you can order however you like; there are lots of SO posts on doing things like that such as https://stackoverflow.com/q/43290483/5325862 – camille Apr 25 '20 at 15:18
  • @camille I'm sorry. I've made the adjustments accordingly. Thank you. – dealvidit Apr 25 '20 at 15:27
  • @camile Also, I did see a lot of posts about it but not many tell you as to how to order the facets based on another column of your dataset. Some of them did, yes, but my apologies, I'm still a bit confused regarding this. – dealvidit Apr 25 '20 at 15:32
  • If you're trying to reorder the factor by one or two other columns, the `forcats` package has helper functions that might work, like `fct_reorder` or `fct_reorder2`. Good illustration on [this post](https://stackoverflow.com/q/53471222/5325862) – camille Apr 25 '20 at 16:18

1 Answers1

3

Consider ordering your data frame by latitude, then re-assign location factor variable by defining its levels to new ordering with unique:

# ORDER DATA FRAME BY ASCENDING LATITUDE
coral_data <- with(coral_data, coral_data[order(latitude),])    
# ORDER DATA FRAME BY DESCENDING LATITUDE
coral_data <- with(coral_data, coral_data[order(rev(latitude)),])

# ASSIGN site AS FACTOR WITH DEFINED LEVELS
coral_data$location <- with(coral_data, factor(as.character(location), levels = unique(location)))

ggplot(coral_data, ...)
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • 1
    Thank you so much! I was actually thinking of ordering the dataframe beforehand but I was still confused about the "factor variable" and how to use it. – dealvidit Apr 25 '20 at 15:48
  • 1
    Great to hear and glad to help! Happy coding! – Parfait Apr 25 '20 at 19:04