1

I have a data frame containing three factors (Scenarios, Emission Target, and Climate Action Year) against which there are several numeric-valued metrics.

What I am looking for is to create a box plot for a given metric analyzed over the "Scenarios" factor and plotted as a function of "Climate Action Year" factor, faceted as a grid by the factor "Emission Target".

Here is what I tried:

a1 <- ggplot(ResDF, aes(ResDF$CAY, ResDF$IncrCost)) + geom_boxplot() + coord_flip() + facet_grid(~ResDF$EmRedTgt, scale="free") + theme_minimal()
b1 <- ggplot(ResDF, aes(ResDF$CAY, ResDF$Etot)) + geom_boxplot() + coord_flip() + facet_grid(~ResDF$EmRedTgt, scale="free") + theme_minimal()
p <- grid.arrange(a1,b1)

Instead of there being 36 box plots, one for each for each climate action year between 2015 and 2050 for each of the emission targets, I see 9 box plots each for each emission target (see attached figure). The data frame I am using can be found here (data frame csv file).

Plot obtained from this code

As a novice to R, I think I'm missing something obvious here. It also makes me wonder what dimension the box plot stat is analyzing. Any directions would be most helpful!

Sarang
  • 13
  • 4
  • It would help to see the data in `ResDF`. Also, there is no need to use `$` inside `aes()`. Just use _e.g._ `ggplot(ResDF, aes(CAY, IncrCost))`. – neilfws Apr 09 '18 at 01:38
  • 1
    Thanks, I have edited my initial post with a link to the ResDF csv file. – Sarang Apr 09 '18 at 02:36
  • I agree with @neilfws. *Never* use `$` inside `aes`, especially not in combination with `facet_grid`/`facet_wrap`. See [this link](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combination-wi/32543753). – Maurits Evers Apr 09 '18 at 03:18

1 Answers1

2

Using $ is creating the problem. Assuming that CAY is already a factor, if you just do this:

ggplot(ResDF, aes(CAY, IncrCost)) + 
  geom_boxplot() + 
  coord_flip() + 
  facet_grid(~EmRedTgt, scale = "free") + 
  theme_minimal()

the result is this:

enter image description here

neilfws
  • 32,751
  • 5
  • 50
  • 63