繁体   English   中英

在R中的cols中使用年份作为facet_grid中的因子时,如何正确地将月份作为轴排序?

[英]how to order month correctly as axis when using year as factor in facet_grid in cols in R?

我在轴中使用日期,但我也使用年份作为列中的分面网格的因子,但轴不是完全有序的。

##source
options(stringsAsFactors = FALSE)
fecha<-c('2016-11-30','2016-12-31','2017-01-31','2017-02-28','2017-03-31','2017-04-30','2017-05-31','2017-06-30','2017-07-31','2017-08-31','2017-09-30','2017-10-31','2017-11-30','2017-12-31','2018-01-31','2018-02-28','2018-03-31','2018-04-30','2018-05-31','2018-06-30','2018-07-31','2018-08-31','2018-09-30','2018-10-31','2018-11-30','2018-12-31','2019-01-31','2019-02-28','2019-03-31','2019-04-30','2019-05-31','2019-06-30','2019-07-31','2019-08-31','2019-09-30','2019-10-31','2019-11-30','2019-12-31','2017-01-31','2017-02-28','2017-03-31','2017-04-30','2017-05-31','2017-06-30','2017-07-31','2017-08-31','2017-09-30','2017-10-31','2017-11-30','2017-12-31','2018-01-31','2018-02-28','2018-03-31','2018-04-30','2018-05-31','2018-06-30','2018-07-31','2018-08-31','2018-09-30','2018-10-31','2018-11-30','2018-12-31','2019-01-31','2019-02-28','2019-03-31','2019-04-30','2019-05-31','2019-06-30','2019-07-31','2019-08-31','2019-09-30','2019-10-31','2019-11-30','2019-12-31','2016-11-30','2016-12-31')
mifactor<-c('Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor1','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2','Factor2')
valor<-c('0.91','0.93','0.89','0.92','0.93','0.93','0.93','0.92','0.85','0.93','0.94','0.92','0.87','0.86','0.95','0.94','0.88','0.85','0.95','0.91','0.87','0.85','0.9','0.88','0.89','0.88','0.87','0.86','0.94','0.87','0.91','0.92','0.94','0.86','0.92','0.88','0.88','0.85','0.93','0.86','0.95','0.85','0.9','0.91','0.95','0.92','0.87','0.85','0.93','0.89','0.85','0.88','0.93','0.94','0.85','0.88','0.86','0.95','0.87','0.87','0.95','0.91','0.9','0.95','0.93','0.93','0.94','0.9','0.95','0.95','0.86','0.9','0.9','0.92','0.86','0.89')
df<-data.frame(fecha,mifactor,valor)


##data preparation
df$fecha<-as.Date(df$fecha)
df$mifactor<-as.factor(df$mifactor)
df$valor<-as.double(df$valor)


##graph
require(ggplot2);
ggplot(aes(x=fecha,y=valor, color=mifactor), data=df)+
 geom_line()+scale_x_date(date_labels = "%b",breaks="1 months")+
facet_grid(cols=vars(as.factor(format(fecha,"%Y"))),rows=vars(mifactor),scales="free")+
theme(axis.text.x = element_text(angle = 90))
      
      

请看图片。

在此处输入图像描述

您希望将月份和年份显示为分类变量,因此最好的方法是避免在 x 轴上完全使用连续刻度。 只需在开始使用dplyr::mutate之前创建适当的因子,确保月份的顺序正确:

require(ggplot2)
require(dplyr)

df %>% 
  mutate(ano = as.factor(format(fecha, "%Y")),
         mes = factor(format(fecha, "%b"), levels = month.abb)) %>%
  ggplot(aes(x = mes, y = valor, color = mifactor,  group = ano)) +
  geom_line() +
  facet_grid(rows = vars(mifactor), cols = vars(ano)) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.3))

在此处输入图像描述


数据:

df <- structure(list(fecha = structure(c(17135, 17166, 17197, 17225, 
17256, 17286, 17317, 17347, 17378, 17409, 17439, 17470, 17500, 
17531, 17562, 17590, 17621, 17651, 17682, 17712, 17743, 17774, 
17804, 17835, 17865, 17896, 17927, 17955, 17986, 18016, 18047, 
18077, 18108, 18139, 18169, 18200, 18230, 18261, 17197, 17225, 
17256, 17286, 17317, 17347, 17378, 17409, 17439, 17470, 17500, 
17531, 17562, 17590, 17621, 17651, 17682, 17712, 17743, 17774, 
17804, 17835, 17865, 17896, 17927, 17955, 17986, 18016, 18047, 
18077, 18108, 18139, 18169, 18200, 18230, 18261, 17135, 17166
), class = "Date"), mifactor = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Factor1", "Factor2"), class = "factor"), 
    valor = c(0.91, 0.93, 0.89, 0.92, 0.93, 0.93, 0.93, 0.92, 
    0.85, 0.93, 0.94, 0.92, 0.87, 0.86, 0.95, 0.94, 0.88, 0.85, 
    0.95, 0.91, 0.87, 0.85, 0.9, 0.88, 0.89, 0.88, 0.87, 0.86, 
    0.94, 0.87, 0.91, 0.92, 0.94, 0.86, 0.92, 0.88, 0.88, 0.85, 
    0.93, 0.86, 0.95, 0.85, 0.9, 0.91, 0.95, 0.92, 0.87, 0.85, 
    0.93, 0.89, 0.85, 0.88, 0.93, 0.94, 0.85, 0.88, 0.86, 0.95, 
    0.87, 0.87, 0.95, 0.91, 0.9, 0.95, 0.93, 0.93, 0.94, 0.9, 
    0.95, 0.95, 0.86, 0.9, 0.9, 0.92, 0.86, 0.89)), row.names = c(NA, 
-76L), class = "data.frame")

暂无
暂无

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

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