【问题标题】:How I can adjust the scale of axis X with breaks as.integer when I graph missing dates in R ggplot2当我在 R ggplot2 中绘制缺失日期时,如何使用中断 as.integer 调整轴 X 的比例
【发布时间】:2020-07-09 12:09:55
【问题描述】:

当我有大量数据绘制缺失日期时,如何将刻度轴 X 调整为.integer。

我正在使用的代码是下一个(@Stefan 帮助我):

#SET OF DATA
df <- read.table(text="
    Fecha - T - Tmin - Tmax
    2015-07-01 - 11,16 - 7,3 - 17
    2015-07-02 - 11,49 - 8 - 17,1
    2015-07-03 - 11,2 - 8,8 - 15,8
    2015-07-04 - 11,20 - 8,6 - 16
    2015-07-05 - 11,23 - 8,9 - 15,7
    2015-07-06 - 10,40 - 7,7 - 15,4
    2015-07-07 - 10,10 - 8,1 - 14,8
    2015-07-08 - 10,04 - 7,3 - 15,4
    2018-01-01 - 11,08 - 4,9 - 17,8
    2018-01-02 - 11,40 - 4,2 - 16,3
    2018-01-03 - 9,000 - 5,5 - 13,5
    2018-01-04 - 8,584 - 6 - 12,8
    2018-01-05 - 8,679 - 7,3 - 11,9
    2018-01-06 - 8,75 - 6,8 - 13
    2018-01-07 - 9,33 - 6,4 - 15,2
    2018-01-08 - 9,63 - 6,3 - 13,9
", header = TRUE, dec = ",")

初始代码

mmp1 <- df[,!grepl("^X", names(df))]
mmp1$Fecha <- as.Date(mmp1$Fecha)

library(ggplot2)
library(scales)
library(dplyr)
library(tibble)

mmp2 <- mmp1 %>% 
  mutate(
    year_fecha = as.character(lubridate::year(Fecha)),
    Fecha2 = format(Fecha, "%d-%m"),
    Fecha2 = forcats::fct_reorder(Fecha2, Fecha)) %>% 
  arrange(Fecha) %>% 
  rowid_to_column(var = "Fecha3")

# Put the theme code aside
polish <- theme(text = element_text(size=11)) +
  theme(axis.text.x=element_text(angle=45, hjust=1))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(panel.background = element_rect(fill = 'white', colour = 'white', size = 1.2, linetype = 7))+
  theme(text=element_text(family="arial", face="bold", size=12))+
  theme(axis.title.y = element_text(face="bold", family = "arial", vjust=1.5, colour="black", hjust = 0.5, size=rel(1.2)))+
  theme(axis.title.x = element_text(face="bold", family = "arial", vjust=0.5, colour="black", size=rel(1.2)))+
  theme(axis.text.x = element_text(family= "sans",face = "plain", colour="black", size=rel(1.1)))+
  theme(axis.text.y = element_text(family= "sans",face = "plain", colour="black", size=rel(1.1)))+
  theme(axis.line = element_line(size = 1, colour = "black"))+
  theme(legend.title = element_text(colour="black", size=12, face="bold", family = "arial"))+
  theme(legend.key = element_rect(fill = "white"))

# Simple and prefered solution: Facet by e.g. by year
w1 <- ggplot(data = mmp2) +
  geom_line(mapping = aes(x = Fecha, y = Tmin, colour="Min"), size=0.71) +
  geom_line(mapping = aes(x = Fecha, y = T, colour="P"), size=0.71) +
  geom_line(mapping = aes(x = Fecha, y = Tmax, colour="Max"), size=0.71) +
  scale_x_date(date_breaks = "1 day", date_labels = "%d-%m", expand = (c(0.001,0.008)))+
  scale_y_continuous(breaks=seq(-4, 28, 2), limits = c(1,18), expand=c(0,0)) +
  scale_colour_manual(name="Leyenda",
                      values=c(Min="green", P="#56B4E9", Max="Red")) +
  ylab("Temperatura (C)")+
  xlab("Tiempo") +
  guides(colour=guide_legend(order = 2),
         shape=guide_legend(order = 2)) +
  facet_wrap(~year_fecha, scales = "free_x") +
  polish

w1

第一个结果是:

# Hacky solutions with some manual labelling
labs <- select(mmp2, Fecha3, Fecha2) %>% 
  tibble::deframe()

date_lab <- function(x) {
  labs[as.character(x)]
}

# Draw the data as one continuous line
w2 <- ggplot(data = mmp2) +
  geom_line(mapping = aes(x = Fecha3, y = Tmin, colour="Min"), size=0.71) +
  geom_line(mapping = aes(x = Fecha3, y = T, colour="P"), size=0.71) +
  geom_line(mapping = aes(x = Fecha3, y = Tmax, colour="Max"), size=0.71) +
  scale_x_continuous(breaks = as.integer(names(labs)), labels = date_lab, expand = (c(0.001,0.008))) +
  scale_y_continuous(breaks=seq(-4, 28, 2), limits = c(1,18), expand=c(0,0)) +
  scale_colour_manual(name="Leyenda",
                      values=c(Min="green", P="#56B4E9", Max="Red")) +
  ylab("Temperatura (C)")+
  xlab("Tiempo") +
  guides(colour=guide_legend(order = 2),
         shape=guide_legend(order = 2)) +
  polish
w2

第二个结果是:

使用相同的代码但绘制大量数据我遇到了这个问题:

如何调整这个 axix X? 谢谢。

【问题讨论】:

  • 将您的 x 轴转换为 Date,然后使用 scale_x_date 来控制中断和标签。请参阅以下示例:stackoverflow.com/a/50710428/786542 & stackoverflow.com/q/56811184/786542
  • 不,不可能,因为在代码的最后一部分,是其他格式。
  • 嗨约翰_埃里克。第一的。如果他们帮助您解决问题,请将答案标记为已接受。第二。问第二个问题时:不要在帖子中粘贴答案,只需在帖子中添加指向第一个问题/答案的链接。第三。您可以简单地要求我帮助您解决第二个问题,而不是发布第二个问题。由于我已经熟悉问题和答案,因此调整我的答案可能要容易得多。第四。看我对第二个问题的回答。 (:
  • 非常感谢 Stefan,您的帮助很大。我是这个平台和 R 编程的新手,有些东西我不知道。我在第一个问题或问题中做了这个问题,但版主删除了我的问题两次,因此我不得不再次提问。谢谢你,我会把你的建议付诸实践。

标签: r ggplot2 dplyr time-series missing-data


【解决方案1】:

第一个问题是消除数据中的“空白”。正如我所说,最简单的解决方案是通过例如刻面。年。这将允许您使用日期刻度。

您的第二个问题与标签的过度绘制有关。在尝试绘制单日时,这种过度绘制自然会随着日期而出现。有很多天,比如一年或更长时间,根本不可能标记所有天。因此,解决方案是限制绘图的天数。当使用日期刻度时,这可以通过一些辅助函数轻松实现(例如breaks = breaks_width("1 week"))。

为了模仿 hacky 解决方案的这种行为,我添加了以下代码:

breaks <- mmp2 %>% 
  # Plots first, 8th, 15th, ... of day of a month
  mutate(days_to_plot = lubridate::day(Fecha) %in% c(1, 8, 15, 22, 29)) %>% 
  filter(days_to_plot) %>% 
  pull(Fecha3)

这不是一个完美的解决方案,但它大大减少了要绘制的标签。

所以用你的大数据集试试这个:

library(ggplot2)
library(scales)
library(dplyr)
library(tibble)
library(lubridate)

mmp2 <- mmp1 %>% 
  mutate(
    year_fecha = as.character(lubridate::year(Fecha)),
    Fecha2 = format(Fecha, "%d-%m"),
    Fecha2 = forcats::fct_reorder(Fecha2, Fecha)) %>% 
  arrange(Fecha) %>% 
  rowid_to_column(var = "Fecha3")

# Put the seem code aside
polish <- theme(text = element_text(size=11)) +
  theme(axis.text.x=element_text(angle=45, hjust=1))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(panel.background = element_rect(fill = 'white', colour = 'white', size = 1.2, linetype = 7))+
  theme(text=element_text(family="sans", face="bold", size=12))+
  theme(axis.title.y = element_text(face="bold", family = "sans", vjust=1.5, colour="black", hjust = 0.5, size=rel(1.2)))+
  theme(axis.title.x = element_text(face="bold", family = "sans", vjust=0.5, colour="black", size=rel(1.2)))+
  theme(axis.text.x = element_text(family= "sans",face = "plain", colour="black", size=rel(1.1)))+
  theme(axis.text.y = element_text(family= "sans",face = "plain", colour="black", size=rel(1.1)))+
  theme(axis.line = element_line(size = 1, colour = "black"))+
  theme(legend.title = element_text(colour="black", size=12, face="bold", family = "arial"))+
  theme(legend.key = element_rect(fill = "white"))

# Draw the data as one continuous line
# Hacky solutions with some manual labelling
labs <- select(mmp2, Fecha3, Fecha2) %>% 
  tibble::deframe()

date_lab <- function(x) {
  labs[as.character(x)]
}

# Which detas/days should be shown on x-axis
breaks <- mmp2 %>% 
  # Plots first, 8th, 15th, ... of day of a month
  mutate(days_to_plot = day(Fecha) %in% c(1, 8, 15, 22, 29)) %>% 
  filter(days_to_plot) %>% 
  pull(Fecha3)

w4 <- ggplot(data = mmp2) +
  geom_line(mapping = aes(x = Fecha3, y = Tmin, colour="Min"), size=0.71) +
  geom_line(mapping = aes(x = Fecha3, y = T, colour="P"), size=0.71) +
  geom_line(mapping = aes(x = Fecha3, y = Tmax, colour="Max"), size=0.71) +
  scale_x_continuous(breaks = breaks, labels = date_lab, expand = (c(0.001,0.008))) +
  scale_y_continuous(breaks=seq(-4, 28, 2), limits = c(1,18), expand=c(0,0)) +
  scale_colour_manual(name="Leyenda",
                      values=c(Min="green", P="#56B4E9", Max="Red")) +
  ylab("Temperatura (C)")+
  xlab("Tiempo") +
  guides(colour=guide_legend(order = 2),
         shape=guide_legend(order = 2)) +
  polish
w4

由reprex package (v0.3.0) 于 2020 年 3 月 29 日创建

如果您仍然有过度绘图的问题,则增加标签之间的宽度或尝试 ggplot 3.3.0 中引入的新 guide_axis 函数,例如

scale_x_continuous(breaks = breaks, labels = date_lab, expand = (c(0.001,0.008)), guide = guide_axis(n.dodge = 2))

将标签分成两行。

【讨论】:

  • 我有一个问题:如果我的格式为“%Y-%m-%d %H:%M”,我将在 x 轴上有很多日期,因为例如 anyday” 2018-01-01 00:00、2018-01-01 00:15、2018-01-01 00:30 .....2018-01-01 23:15”。在这种格式下,如何在 X-axix 的标签上获得适当的中断?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多