ggplot2条形图R中的竖杆轻微错位

ggplot2条形图R中的竖杆轻微错位,r,ggplot2,R,Ggplot2,我正在尝试使用ggplot2构建堆叠条形图。下面是我正在使用的数据帧 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, 1

我正在尝试使用ggplot2构建堆叠条形图。下面是我正在使用的数据帧

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"))
条形图

2019年2月至2019年3月期间,钢筋间距似乎略有偏差。这是ggplot2中的错误吗


在MacOS Catalina和R Studio 1.3.959上使用ggplot2版本3.3.2、R 4.0.3。

您的x轴以天为单位缩放,而不是以月为单位缩放,因此条形图的宽度以天为单位固定。由于2月和3月之间只有28天,这导致分离线变薄

一种可能的解决方案是将数据更改为月份,从而强制以月份为单位缩放x轴。可使用软件包
zoo
中的
as.yearmon
功能将其存档:

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

df$Yearmon@Damandeep谢谢。我改变了问题,偏差发生在2019年2月和3月之间。这几个月有数据,工作愉快。非常感谢。
library(zoo)
df$Yearmon <- as.yearmon(df$Date)
ggplot(data=df, aes(x = Yearmon, y = kWh, fill = Type)) +
  geom_bar(stat="identity")