簡體   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