Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 缩放x日期删除轴上的额外月份(ggplot2 2.2.0.9)_R_Ggplot2 - Fatal编程技术网

R 缩放x日期删除轴上的额外月份(ggplot2 2.2.0.9)

R 缩放x日期删除轴上的额外月份(ggplot2 2.2.0.9),r,ggplot2,R,Ggplot2,我使用的是带有时间刻度的折线图,最后一个点是12月30日。然而,使用scale_x_date,1月份会显示在量表上,即使该月份有数据点 天平应该在最后一个月停止,但我相信这是因为那天。有没有办法让量表与数据中的实际月份保持一致 我正在使用此设置比例的格式: #with lubridate data$month <- ymd(data$month) #in the plot scale_x_date(date_breaks = "1 month", date_labels ="%b %y"

我使用的是带有时间刻度的折线图,最后一个点是12月30日。然而,使用scale_x_date,1月份会显示在量表上,即使该月份有数据点

天平应该在最后一个月停止,但我相信这是因为那天。有没有办法让量表与数据中的实际月份保持一致

我正在使用此设置比例的格式:

#with lubridate
data$month <- ymd(data$month)

#in the plot
scale_x_date(date_breaks = "1 month", date_labels ="%b %y")
#带润滑油

数据$P>>我不认为这是一个好的情节,但给你。您可能希望将休息时间设置为每月的第15天:

library(ggplot2)
ggplot(data, aes(month, num, group = 1)) + 
  geom_line(position = "identity", color="#6baed6") + 
  #scale_y_continuous(expand = (c(0.1,0)), labels = comma) +
  scale_x_date(breaks = seq(as.Date("2016-07-15"), 
                            as.Date("2016-12-15"), by = "1 month"), date_labels ="%b %y") + 
  labs(x = NULL, y = NULL) + 
  theme_bw() +
  theme(strip.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.ticks=element_blank(),
        axis.text=element_text(size=12)) 

如果您添加参数
expand=c(0,0)
,是否有效?是否有效?但现在点数跳过一个月并偏移。您的解决方案可行,但仍有其他问题。一个月没有一个点,即使它在数据集中。您可以尝试将“扩展”设置为非常小但>0。证明一些数据(使用
dput
)将有助于使问题可再现。让我添加一个可再现的示例。我不确定您在x轴上想要的断点是什么。请澄清。如果你有2016-12-30,但没有在轴上显示1月的日期,这看起来会很尴尬。这很有效。谢谢我很好奇,有什么更好的情节呢?你可以在轴上打勾,并在轴上显示完整的日期。或者,在每个月的第一天绘制一个带有轴刻度的图,刻度之间有轴标签。公平评论。我不包括完整的日期,因为这是一个持续的时间序列,这一天将变得无关紧要,因为数据是在同一天之后收集的,这将使图表更难阅读。在我看来,仅仅展示一个月就足以看到发生了什么。
library(ggplot2)
ggplot(data, aes(month, num, group = 1)) + 
  geom_line(position = "identity", color="#6baed6") + 
  #scale_y_continuous(expand = (c(0.1,0)), labels = comma) +
  scale_x_date(breaks = seq(as.Date("2016-07-15"), 
                            as.Date("2016-12-15"), by = "1 month"), date_labels ="%b %y") + 
  labs(x = NULL, y = NULL) + 
  theme_bw() +
  theme(strip.background=element_blank(),
        panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.ticks=element_blank(),
        axis.text=element_text(size=12))