[英]Maintain order of columns (geom_col) when using facet_grid and a factor to determine columns' fill colour
[英]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.