2

I am trying to plot the two different type of production for five different groups. I can generate the plot and it already looks ok, although the ordering in the final ggplot is not as I would like to have it. I found the solution with arrange(), but although the ordering in this step is correct the final result is different again. I would like to have the grouping that for each group (1, 2, 3, 4, NA) the two different types of production are right on top of each other.

library(zoo)
library(data.table)
library(ggplot2)
library(dplyr)
DT <- structure(list(Year.Quarter = structure(c(2015, 2015, 2015, 2015, 
                                          2015, 2015.25, 2015.25, 2015.25, 2015.25, 2015.25, 2015.5, 2015.5, 
                                          2015.5, 2015.5, 2015.5, 2015.75, 2015.75, 2015.75, 2015.75, 2015.75, 
                                          2016, 2016, 2016, 2016, 2016, 2016.25, 2016.25, 2016.25, 2016.25, 
                                          2016.25), class = "yearqtr")
                                        , Group = c(2L, 1L, 4L, 3L, NA, 2L, 
                                                  1L, 4L, 3L, NA, 2L, 1L, 4L, 3L, NA, 2L, 1L, 4L, 3L, NA, 2L, 1L, 
                                                  4L, 3L, NA, 2L, 1L, 4L, 3L, NA)
                                        , Conventional.Prod = c(11.78, 7.31, 7.34, 9.44, 28.72, 11.32, 5.27, 7.47, 8.08, 27.14, 11.49, 
                                                                4.65, 7.63, 7.07, 25.93, 10.69, 3.68, 6.96, 6.72, 18.31, 9.28, 
                                                                 3.69, 6.86, 6.34, 19.14, 9.25, 3.69, 6.9, 6.16, 17.7)
                                       , Unconventional.Prod = c(15.22, 10.69, 7.66, 15.56, 30.28, 15.68, 10.73, 7.53, 15.92, 29.86, 
                                                        13.51, 10.35, 7.37, 15.93, 28.07, 13.31, 10.32, 7.04, 16.28, 
                                25.69, 12.72, 9.31, 7.14, 16.66, 25.86, 12.75, 9.31, 7.1, 16.84, 24.3))
                        , .Names = c("Year.Quarter", "Group", "Conventional.Prod", "Unconventional.Prod"), row.names = c(NA, -30L), class = c("data.table", 
                                                      "data.frame"))
data.table::melt(DT,
                 , id.vars = c("Year.Quarter", "Group")
                 , measure.vars = c("Conventional.Prod", "Unconventional.Prod")
) %>% arrange(Year.Quarter, Group, variable) %>%  ggplot(data = ., aes(x = Year.Quarter, y = value, color = variable, fill = as.factor(Group))) +
        geom_area(stat = "identity", position = "fill") +
        #geom_line(aes(x = Calendar.Data.Year.and.Quarter ,y = value)) +
        theme(legend.title=element_blank()) + 
        scale_x_yearqtr(format = "%Y-Q%q",n = 8, expand = c(0,0))

The ordering after the arrange step is as intended:

Year.Quarter     Group      variable       value
1:      2015 Q1     1   Conventional.Prod  7.31
2:      2015 Q1     1 Unconventional.Prod 10.69
3:      2015 Q1     2   Conventional.Prod 11.78
4:      2015 Q1     2 Unconventional.Prod 15.22
5:      2015 Q1     3   Conventional.Prod  9.44
6:      2015 Q1     3 Unconventional.Prod 15.56
7:      2015 Q1     4   Conventional.Prod  7.34
8:      2015 Q1     4 Unconventional.Prod  7.66
9:      2015 Q1    NA   Conventional.Prod 28.72
10:     2015 Q1    NA Unconventional.Prod 30.28

But the ordering in the final plot is somehow reversed again, so that the production is the major group. Plot with wrong grouping

1
  • I am not sure if the problem might be solvable by using aes_group_order, but not sure how to properly implement this. Commented Aug 7, 2017 at 9:01

2 Answers 2

2

Would you be interested in something like this? It's not exactly how you expected but it gives a nice visualization to your data.

data.table::melt(DT,
                 , id.vars = c("Year.Quarter", "Group")
                 , measure.vars = c("Conventional.Prod", "Unconventional.Prod")
) %>% ggplot(data = ., aes(x = Year.Quarter, y = value, fill = as.factor(Group))) +
  scale_x_yearqtr(format = "%Y-Q%q") +
  geom_bar(stat = "identity",position = "dodge") +
  facet_grid(. ~ variable) +
  theme_bw()

Hope this helps!

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

5 Comments

Thanks a alot, it's a nice representation. But I am more interested to show the diverging pattern between the two production types per group. So that's why I would like to have the production right next to each other for each group.
How about replacing geom_bar code in my approach with geom_area(stat = "identity")? Also see the difference after adding position = "fill" option in it (notice the change in y-axis). I think it should fulfill your requirement to some extent.
I like the idea of a facet plot, I'll try to go with that, although I changed facet_grid( variable ~.) + to facet_grid( variable ~.) + in order to make it easier to compare the development over time.
Glad that you liked the solution!
Although I don't want to mark it as an answer, since it's not really solving the question, but just adds a possible data visualization.
1

Specifying fill before color in the call to ggplot() is a quick way to do what I think you want:

# Not repeating all the code from your example, but change this line:
ggplot(data = dat, aes(x = Year.Quarter, y = value, fill = as.factor(Group), color = variable))

enter image description here

1 Comment

Well, yeah that's what I was looking for. I tried nearly all possible combinations of fill and color, but I never changed the order. It just never occured to me that this might make a difference...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.