TBP
TBP

Reputation: 94

Slight misalignment of vertical bar in ggplot2 barplot R

I am trying to build a stacked barplot with ggplot2. Below is the dataframe that I am using.

df <- structure(list(Date = structure(c(17532, 17532, 17652, 17652, 
17683, 17683, 17744, 17744, 17775, 17775, 17805, 17805, 17836, 
17836, 17866, 17866, 17897, 17897, 17928, 17928, 17956, 17956, 
17987, 17987, 18017, 18017, 18048, 18048, 18078, 18078, 18109, 
18109, 18140, 18140, 18170, 18170, 18201, 18201, 18231, 18262, 
18262, 18293, 18293, 18322, 18353, 18353, 18383, 18414, 18414, 
18444, 18444, 18475, 18475, 18506, 18506, 18536, 18536, 18567, 
18567), class = "Date"), Type = structure(c(1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Offentlig", "Privat"), class = "factor"), 
    kWh = c(13.06, 150.31, 97.842, 298.27, 124.87, 279.76, 50.762, 
    255.069, 42.122, 437.11, 84.72, 460.36, 55.96, 502.07, 48.3, 
    505.14, 49.36, 407.54, 114.62, 422.54, 138.28, 502.44, 80.73, 
    408.68, 36.55, 429.77, 20.3, 308.68, 59.13, 250.07, 7.84, 
    424.21, 42.52, 389.44, 79.82, 403.61, 17.16, 521.71, 530.92, 
    39.92, 570.01, 25.58, 350.98, 312, 9.55, 430.42, 479.22, 
    11.72, 426.45, 12.25, 198.35, 63.27, 449.41, 59.33, 458.26, 
    114.55, 400.75, 93.91, 442)), row.names = c(NA, -59L), groups = structure(list(
    Date = structure(c(17532, 17652, 17683, 17744, 17775, 17805, 
    17836, 17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078, 
    18109, 18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353, 
    18383, 18414, 18444, 18475, 18506, 18536, 18567), class = "Date"), 
    .rows = structure(list(1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 
        15:16, 17:18, 19:20, 21:22, 23:24, 25:26, 27:28, 29:30, 
        31:32, 33:34, 35:36, 37:38, 39L, 40:41, 42:43, 44L, 45:46, 
        47L, 48:49, 50:51, 52:53, 54:55, 56:57, 58:59), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 31L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

The code snippet below produces the barplot I want.

ggplot(data=df, aes(x = Date, y = kWh, fill = Type)) +
  geom_bar(stat="identity")

Barplot

enter image description here

Somehow there seems to be a slight misalignment in spacing of the bars between the months february 2019 and march 2019. Is this a bug in ggplot2?

Using ggplot2 version 3.3.2, R 4.0.3 on MacOS Catalina with R Studio 1.3.959.

Upvotes: 2

Views: 99

Answers (1)

ClF3
ClF3

Reputation: 171

Your x-axis is scaled in days, not months and therefore the bars have a fixed width in days. And since there are only 28 days between Feb and Mar, this causes the separating line to be thinner.

A possible solution is to change your data to months, and thus force the x-axis to be scaled in months. This can be archived with the as.yearmon function from the package zoo:

library(zoo)
df$Yearmon <- as.yearmon(df$Date)
ggplot(data=df, aes(x = Yearmon, y = kWh, fill = Type)) +
  geom_bar(stat="identity")

Upvotes: 1

Related Questions