繁体   English   中英

删除不均匀 x 轴时间序列 plot ggplot2 中的空列

[英]Remove empty columns in uneven x-axis time series plot ggplot2

 df=structure(list(Date = structure(c(16752, 16766, 16773, 16792, 
    16811, 16822, 16839, 16853, 16864, 16870, 16892, 16898, 16906
    ), class = "Date"), Snow.Depth = c(8L, 15L, 20L, 25L, 30L, 30L, 
    27L, 30L, 32L, 35L, 45L, 45L, 38L)), row.names = c(NA, -13L), class = "data.frame")

library(ggplot2)
library(scales)
library(lubridate)


df$Date=ymd(df$Date)

class(df$Date)

ggplot(df, aes(x=Date, y=Snow.Depth)) + 
  geom_bar(stat = "identity",fill = "#fe9929", show.legend=T)+
  scale_x_date(breaks = df$Date)+
  # scale_x_continuous("", breaks=1:13, 
  #                    labels = as.Date(c("2015-11-13", "2015-11-27", "2015-12-04", "2015-12-23","2016-01-11",
  #                                       "2016-01-22","2016-02-08","2016-02-22","2016-03-04","2016-03-10","2016-04-01",
  #                                       "2016-04-07","2016-04-15")))+
  theme(axis.text.x=element_text(angle = 90,size = 12,colour = "black",face="bold",hjust=1)) + 
  scale_y_continuous(limits=c(0,45),breaks = seq(0,45, by = 5))+
  ylab("Daily Mean Snow Depth (cm)") + 
  xlab("Date")+
  theme(strip.text.x = element_text(size = 15, colour = "black", angle = 0,face="bold"))+
  theme(axis.text = element_text(size=14, color="black"))+
  theme(text = element_text(size=18),
        axis.text.x = element_text(angle=90, hjust=1))+#scale_color_manual(name="Legend",labels=c("Above Normal","Below Normal"),values=c("#4575b4","#d73027"))+
  theme(legend.direction ="horizontal",legend.position = "bottom")+
  guides(fill=guide_legend(nrow=1))

p+theme(panel.background = element_rect(fill='#000029'))

如何删除以红色表示的空间?

删除以红色形状表示的空间

也许使用 x 作为索引来折叠所有x = 1:nrow(df)并取消您在scale_x_continuos之前放置的手动比例。

但是,我认为该图表的信息量较少,因为您不知道每个条形之间经过了多少时间。


ggplot(df, aes(x= 1:nrow(df), y=Snow.Depth)) + 
  geom_bar(stat = "identity",fill = "#fe9929", show.legend=T)+
  #scale_x_date(breaks = df$Date) +
  scale_x_continuous("", breaks=1:13,
                     labels = as.Date(c("2015-11-13", "2015-11-27", "2015-12-04", "2015-12-23","2016-01-11",
                                        "2016-01-22","2016-02-08","2016-02-22","2016-03-04","2016-03-10","2016-04-01",
                                        "2016-04-07","2016-04-15")))+
  theme(axis.text.x=element_text(angle = 90,size = 12,colour = "black",face="bold",hjust=1)) + 
  scale_y_continuous(limits=c(0,45),breaks = seq(0,45, by = 5))+
  ylab("Daily Mean Snow Depth (cm)") + 
  xlab("Date")+
  theme(strip.text.x = element_text(size = 15, colour = "black", angle = 0,face="bold"))+
  theme(axis.text = element_text(size=14, color="black"))+
  theme(text = element_text(size=18),
        axis.text.x = element_text(angle=90, hjust=1))+#scale_color_manual(name="Legend",labels=c("Above Normal","Below Normal"),values=c("#4575b4","#d73027"))+
  theme(legend.direction ="horizontal",legend.position = "bottom")+
  guides(fill=guide_legend(nrow=1))

p+theme(panel.background = element_rect(fill='#000029'))

最简单的方法是将日期转换为字符或因子:

library(tidyverse)

df <- structure(list(Date = structure(c(
  16752, 16766, 16773, 16792, 16811, 16822,
  16839, 16853, 16864, 16870, 16892, 16898, 16906
), class = "Date"), Snow.Depth = c(
  8L, 15L, 20L, 25L, 30L, 30L,
  27L, 30L, 32L, 35L, 45L, 45L, 38L
)), row.names = c(NA, -13L), class = "data.frame")


df %>%
  ggplot(aes(x = as.character(Date), y = Snow.Depth)) +
  geom_col(fill = "#fe9929") +
  scale_y_continuous(limits = c(0, 45), breaks = seq(0, 45, by = 5)) +
  labs(y = "Daily Mean Snow Depth (cm)", x = "Date") +
  theme(
    strip.text.x = element_text(size = 15, colour = "black", angle = 0, face = "bold"),
    axis.text = element_text(size = 14, color = "black"),
    text = element_text(size = 18),
    axis.text.x = element_text(angle = 90, hjust = 1, size = 12, colour = "black", face = "bold"),
    legend.direction = "horizontal",
    legend.position = "bottom",
    panel.background = element_rect(fill = "#000029")
  ) +
  guides(fill = guide_legend(nrow = 1))

但是,正如评论中提到的,这可能会产生误导。

另一个潜在的可视化选择使用组合线/点 plot:

df %>%
  ggplot(aes(x = Date, y = Snow.Depth)) +
  geom_line(color = "#fe9929", size = 2) +
  geom_point(color = "#fe9929", size = 5) +
  geom_point(color = "#000029", size = 3) +
  scale_y_continuous(limits = c(0, 45), breaks = seq(0, 45, by = 5)) +
  scale_x_date(date_labels = "%b '%y") +
  labs(y = "Daily Mean Snow Depth (cm)", x = "Date") +
  theme(
    strip.text.x = element_text(size = 15, colour = "black", angle = 0, face = "bold"),
    axis.text = element_text(size = 14, color = "black"),
    text = element_text(size = 18),
    legend.direction = "horizontal",
    legend.position = "bottom",
    panel.background = element_rect(fill = "#000029")
  ) +
  guides(fill = guide_legend(nrow = 1))

代表 package (v1.0.0) 于 2021 年 3 月 26 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM