首页 > 解决方案 > 如何在 y 轴上重新排序时间,使午夜位于中心

问题描述

我正在使用geom_raster以分钟(y 轴)为单位的时间(x 轴)绘制日期(x 轴),用于创建动作图。我希望 y 轴从中午 12 点开始,一直到午夜,然后回到中午(中午 12 点、上午 11 点、上午 10 点、上午 9 点、上午 8 点、上午 7 点、上午 6 点、上午 5 点、上午 4 点、上午 3 点、上午 2 点、上午 1 点、下午 0 点、晚上 11 点, 10pm, 9pm, 8pm, 7pm, 6pm, 5pm, 4pm, 3pm, 2pm, 1pm 12pm),所以午夜将在中心。我完全不知道该怎么做。

我试图将数据变成一个因素并对其进行排序:

x <- seq(11.55, 0, -0.05)
y <- seq(23.55, 12.00, -0.05)
z <- c(x,y)

但问题是我有很多重复值,因为我正在处理 24 天的数据......

这是我的代码,没有重新排列 y 轴:

ggplot(LIA_20, aes(x = date, y = time))+
  geom_raster(data = LIA_20, aes(fill = Sv_mean))+
  scale_fill_gradientn(colours = matlab.like(100), na.value = "white",
                       limits = c(-140, -70))+
  scale_x_continuous(expand = c(0,0), limits = c(2, 23), breaks = 3:22)+
  scale_y_discrete(expand = c(0,0))+

  coord_fixed(ratio = 0.00005)+
  theme(panel.background = element_blank(), 
        legend.position = "bottom")+
  labs(x = "Day (May 2018)", y = "Time of day, UTC", fill = "Sv (dB)")+ 
  guides(fill = guide_colourbar(title.position = "bottom", 
                                title.hjust = 0.5))

标签: rggplot2

解决方案


推荐阅读