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!

3
  • 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)). Commented Apr 9, 2018 at 1:38
  • 1
    Thanks, I have edited my initial post with a link to the ResDF csv file. Commented Apr 9, 2018 at 2:36
  • I agree with @neilfws. Never use $ inside aes, especially not in combination with facet_grid/facet_wrap. See this link. Commented Apr 9, 2018 at 3:18

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.