3

In my facet_label i would like to use superscript such as in Figure below 3 should be superscript to m. Also i would like to assign degree sign with C in the temperature subplots. Also, i would like to see zero horizontal line only in temperature plots but not in streamflow and precipitation plots. Below is my code so far and i would appreciate any help.

library(tidyverse)
MonthlyData = data.frame(Month = 1:12, A = runif(12, 1,40), B = runif(12,-25,15), C = runif(12,-15,25), D = runif(12,1,75))
PlotData = gather(data = MonthlyData, key = "Variable", value = "Value", -Month)
PlotData$Variable = factor(PlotData$Variable, labels = c("Streamflow (m3/sec)", "Max temperature (oC)", "Min temperature (oC)", "Precipitation (mm)"))

ggplot(transform(PlotData, Variable = factor(Variable, levels = c("Streamflow (m3/sec)", "Precipitation (mm)", "Max temperature (oC)", "Min temperature (oC)"))), aes(x = Month, y = Value))+
  geom_bar(stat = "identity", width = 0.5) + facet_wrap(~Variable, nrow = 4, scales = "free_y")+ 
  geom_hline(yintercept = 0)+ aes(fill = as.factor(Variable))+
  scale_x_continuous(breaks = c(1:12), labels = c("Jan", "Feb", "Mar","Apr", "May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"))+
  theme_bw()

Here is my plot based on the example data

enter image description here

camille
  • 16,432
  • 18
  • 38
  • 60
Hydro
  • 1,057
  • 12
  • 25
  • https://stackoverflow.com/questions/37554118/ggplot-inserting-space-before-degree-symbol-on-axis-label and https://stackoverflow.com/questions/29995022/r-interpreting-a-subscript-in-a-variable-used-in-ggplot seem to provide a way to go but there may be simpler ways now ... – user20650 Jan 05 '20 at 02:12
  • The degree sign in the first reference question worked, however, i am still struggling with the superscript in the m3/sec one. how about the 0 horizental line across the streamflow and precipitation- I do not want to have that line in these two subplots. I am fine with the temperature 0 horizontal line. – Hydro Jan 05 '20 at 02:24
  • re the line please see https://stackoverflow.com/questions/11846295/how-to-add-different-lines-for-facets – user20650 Jan 05 '20 at 02:48

1 Answers1

2

Using bits of code from ggplot inserting space before degree symbol on axis label, R - Interpreting a subscript in a variable used in ggplot and How to add different lines for facets we can just about get there:

library(ggplot2)
library(tidyr)

MonthlyData = data.frame(Month = 1:12, A = runif(12, 1,40), B = runif(12,-25,15), 
                         C = runif(12,-15,25), D = runif(12,1,75))
PlotData = gather(data = MonthlyData, key = "Variable", value = "Value", -Month)

# To set the order of the facets, then create a factor for the facetting 
# level in the order that you want
PlotData$Variable = factor(PlotData$Variable, levels=c("A", "D", "B", "C"))


# Set labels that are to be parsed
# use instead of changing Variable factor levels
# reorder to order of facet variable
lbs = setNames(c("'Streamflow ('*m^3*'/sec)'", 
                 "'Max Temperature ' (degree*C)", 
                 "'Min Temperature ' (degree*C)", 
                 "Precipitation (mm)"),
                LETTERS[1:4]
       )[levels(PlotData$Variable)]

ggplot(PlotData, aes(x = Month, y = Value, fill = Variable)) +
  geom_bar(stat = "identity", width = 0.5) + 
  # create facet labels from character vector
  facet_wrap(~Variable, nrow = 4, scales = "free_y", 
             labeller=as_labeller(lbs, label_parsed)) + 
  # parse legend labels: reorder
  scale_fill_discrete(labels = parse(text=lbs)) +
  # specify names of facets to add horizontal line to
  geom_hline(data=data.frame(Variable=c("B", "C")), aes(yintercept = 0))

To change the order of the facets see controlling order of facet_grid/facet_wrap in ggplot2?

user20650
  • 24,654
  • 5
  • 56
  • 91
  • I was using `factor` and `levels` to change the order of `facets`- How i would change the order now such that my `D` variable become the second `facet`? – Hydro Jan 05 '20 at 14:51