【问题标题】:How to remove gaps on a date axis in ggplot2如何在ggplot2中删除日期轴上的间隙
【发布时间】:2020-04-20 20:33:34
【问题描述】:

如何清理这些没有数据的空白空间?

我的数据集的一个 sn-p:

contratos.dolar <- read.table(text = "
DATA TOTAL IE II PJF PJNF PF VAR 
1 2020-02-28 1003124 178481 -168172 -11901 5497 -3905 <NA> 
2 2020-03-02 643282 140910 -127170 -28232 18187 -3695 -37571 
3 2020-03-03 665899 162927 -138690 -34084 14577 -4770 22017 
4 2020-03-04 688097 195154 -151717 -47994 9912 -5355 32227 
5 2020-03-05 739802 255604 -178552 -82204 8707 -3555 60450 
6 2020-03-06 739802 255604 -178552 -82204 8707 -3555 0", header = TRUE)

我的代码:

ggplot(contratos.dolar, aes(x = DATA, y = as.numeric(IE), fill = IE > 0)) +
  geom_bar(stat='identity') +
  labs(title = "Dolar futuro - Contratos em aberto", subtitle = "Investidor Estrangeiro", caption = format.Date(hoje, "%d/%m/%Y")) +
  xlab("") +
  ylab("") +
  scale_x_date(date_labels = "%d/%m", expand = c(0,0), limits = c(Sys.Date() - 40, NA), breaks = "2 day") +
  scale_y_continuous(labels = scales::comma, n.breaks = 7) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1), plot.caption = element_text(size=10, face="bold.italic")) +
  guides(fill = FALSE)

我得到的情节图片:

【问题讨论】:

  • 嗨@Fabio。你能提供你的数据的sn-p吗?只需在控制台中输入dput(contratos.dolar) 并将输出复制到您的帖子中。由于您的数据集可能很大,因此请使用 dput(head(contratos.dolar, 20)) 来复制例如前 20 行。
  • > head(contratos.dolar) 数据总计 IE II PJF PJNF PF VAR 1 2020-02-28 1003124 178481 -168172 -11901 5497 -3905 2 2020-03-02 643282 140910 - 127170 -28232 18187 -3695 -37571 3 2020-03-03 665890 -34084 1457-03-04 6880-03-04 6880-03-04 688097 195154 -151717-47994 -151717 -47994 -151717 -47994 -151717 -47994 -151717 -47994 9912-5355 32227 5 2020-03-05 739802 255604 -178552 -178552 -82204 8707 -3555 60450 6 2020-03-06 739802 255604 -178552 -82204 8707 -3555 0

标签: r date ggplot2 axis


【解决方案1】:

要消除日期轴中的空白,您必须将日期列转换为因子。为了只显示第二个日期,我添加了一个辅助函数。试试这个:

contratos.dolar <- read.table(text = "
DATA TOTAL IE II PJF PJNF PF VAR 
1 2020-02-28 1003124 178481 -168172 -11901 5497 -3905 <NA> 
2 2020-03-02 643282 140910 -127170 -28232 18187 -3695 -37571 
3 2020-03-03 665899 162927 -138690 -34084 14577 -4770 22017 
4 2020-03-04 688097 195154 -151717 -47994 9912 -5355 32227 
5 2020-03-05 739802 255604 -178552 -82204 8707 -3555 60450 
6 2020-03-06 739802 255604 -178552 -82204 8707 -3555 0", header = TRUE)

contratos.dolar$DATA <- as.Date(contratos.dolar$DATA, "%Y-%m-%d")
hoje <- Sys.Date()

contratos.dolar$DATA1 <- factor(format(contratos.dolar$DATA, "%d %m"))
contratos.dolar$DATA1 <- forcats::fct_reorder(contratos.dolar$DATA1, contratos.dolar$DATA)

mybreaks <- function(x) {
  x[seq_along(x) %% 2 == 1]  
}

library(ggplot2)

ggplot(contratos.dolar, aes(x = DATA1, y = as.numeric(IE), fill = IE > 0)) +
  geom_bar(stat='identity') +
  labs(title = "Dolar futuro - Contratos em aberto", subtitle = "Investidor Estrangeiro", caption = format.Date(hoje, "%d/%m/%Y")) +
  xlab("") +
  ylab("") +
  scale_y_continuous(labels = scales::comma, n.breaks = 7) +
  scale_x_discrete(breaks = mybreaks) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1), plot.caption = element_text(size=10, face="bold.italic")) +
  guides(fill = FALSE)

reprex package (v0.3.0) 于 2020-04-20 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多