ggplot2: yearmon scale and geom_bar
Asked Answered
S

2

8

More than a solution I'd like to understand the reason why something which should be quite easy, it's actually not.

[I am borrowing part of the code from a different post which touched on the issue but it ended up with a solution I didn't like]

library(ggplot2)
library(xts)
library(dplyr)
library(scales)

csvData <- "dt,status
2015-12-03,1
2015-12-05,1
2015-12-05,0
2015-11-24,1
2015-10-17,0
2015-12-18,0
2016-06-30,0
2016-05-21,1
2016-03-31,0
2015-12-31,0"

tmp <- read.csv(textConnection(csvData))
tmp$dt <- as.Date(tmp$dt)
tmp$yearmon <- as.yearmon(tmp$dt)
tmp$status <- as.factor(tmp$status)

### Not good. Why?
ggplot(tmp, aes(x = yearmon, fill = status)) + 
  geom_bar() + 
  scale_x_yearmon()

### Almost good but long-winded and ticks not great
chartData <- tmp %>%
  group_by(yearmon, status) %>%
  summarise(count = n()) %>%
  as.data.frame()
ggplot(chartData, aes(x = yearmon, y = count, fill = status)) + 
  geom_col() + 
  scale_x_yearmon()

The first plot is all wrong; the second is almost perfect (ticks on the X axis are not great but I can live with that). Isn't geom_bar() supposed to perform the count job I have to manually perform in the second chart?

FIRST CHART poor plot

SECOND CHART better plot

My question is: why is the first chart so poor? There is a warning which is meant to suggest something ("position_stack requires non-overlapping x intervals") but I really fail to understand it. Thanks.

MY PERSONAL ANSWER

This is what I learned (thanks so much to all of you!):

  • Even if there is a scale_#_yearmon or scale_#_date, unfortunately ggplot treats those object types as continuous numbers. That makes geom_bar unusable.
  • geom_histogram might do the trick. But you lose control on relevant parts of the aestethics.
  • bottom line: you need to group/sum before you chart
  • Not sure (if you plan to use ggplot2) xts or lubridate are really that useful for what I was trying to achieve. I suspect for any continuous case - date-wise - they will be perfect.

All in, I ended with this which does perfectly what I am after (notice how there is no need for xts or lubridate):

library(ggplot2)
library(dplyr)
library(scales)

csvData <- "dt,status
2015-12-03,1
2015-12-05,1
2015-12-05,0
2015-11-24,1
2015-10-17,0
2015-12-18,0
2016-06-30,0
2016-05-21,1
2016-03-31,0
2015-12-31,0"

tmp <- read.csv(textConnection(csvData))
tmp$dt <- as.Date(tmp$dt)
tmp$yearmon <- as.Date(format(tmp$dt, "%Y-%m-01"))
tmp$status <- as.factor(tmp$status)

### GOOD
chartData <- tmp %>%
  group_by(yearmon, status) %>%
  summarise(count = n()) %>%
  as.data.frame()

ggplot(chartData, aes(x = yearmon, y = count, fill = status)) + 
  geom_col() + 
  scale_x_date(labels = date_format("%h-%y"),
               breaks = seq(from = min(chartData$yearmon), 
                            to = max(chartData$yearmon), by = "month"))

FINAL OUTPUT final plot

Systemize answered 18/5, 2017 at 7:36 Comment(5)
I'm not seeing any difference between the first and second plot. Can you post the images of your plots and point out what was wrong about the first plot?Sentimentalize
Done as requested. Could it be some sort of issue on platform/package version? I am on WIN10; R version 3.4.0 (2017-04-21); ggplot2 2.2.1Systemize
I would use instead: ggplot(tmp, aes(x = floor_date(dt, "month"), fill = status)) + geom_bar() + scale_x_date(labels = date_format("%Y-%b")), which gives better x-axis.Dobbs
floor_date() is from the lubridate package, is it? Anyway: I get exactly the same result as per FIRST CHART above with that.Systemize
With ggplot2 3.3.0 it doesn't workJacelynjacenta
T
2

You could also aes(x=factor(yearmon), ...) as a shortcut fix.

Tawannatawdry answered 30/10, 2020 at 7:23 Comment(0)
S
1

The reason why the first plot is screwed is basically ggplot2 does not exactly what the yearmon is. As you see here it is just a num internally with labels.

> as.numeric(tmp$yearmon)
[1] 2015.917 2015.917 2015.917 2015.833 2015.750 2015.917 2016.417 2016.333 2016.167 2015.917

So when you plot without the previous aggregation, the bar is spread out. You need to assign appropriate binwidth using geom_histogram() like this:

ggplot(tmp, aes(x = yearmon, fill = status)) + 
  geom_histogram(binwidth = 1/12) + 
  scale_x_yearmon()

1/12 corresponds with 12 months in each year.

For a plot after aggregation, as @ed_sans suggest, I also prefer lubridate as I know better on how to change ticks and modify axis labels.

chartData <- tmp %>%
  mutate(ym = floor_date(dt,"month")) %>%
  group_by(ym, status) %>%
  summarise(count = n()) %>%
  as.data.frame()

ggplot(chartData, aes(x = ym, y = count, fill = status)) + 
  geom_col() + 
  scale_x_date(labels = date_format("%Y-%m"),
               breaks = as.Date("2015-09-01") + 
                 months(seq(0, 10, by = 2)))
Salta answered 18/5, 2017 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.