简体   繁体   English

如何使用日期重新排序图表的轴

[英]how to reorder the axes of a chart with the date

I have these two data:我有这两个数据:

1.table_range 1.table_range

"code";"sp";"min";"max"
"EI12";"EI";2015-06-01;2015-08-01
"EI21";"EI";2015-01-01;2018-11-01
"EI25";"EI";2019-03-01;2019-03-01
"EI26";"EI";2019-03-01;2020-02-01
"EI27";"EI";2019-10-01;2019-11-01
"EI30";"EI";2016-01-01;2018-05-01
"EI35";"EI";2017-09-01;2017-12-01
"EI38";"EI";2017-02-01;2019-11-01
"EI43";"EI";2018-08-01;2019-01-01
"EI44";"EI";2018-04-01;2018-04-01
"EI45";"EI";2018-09-01;2020-01-01
"EI7";"EI";2014-05-01;2016-04-01
"EI9";"EI";2012-12-01;2015-03-01

2.table_sum 2.table_sum

"mes_ano";"code";"sp";"count"
2012-12-01;"EI9";"EI";"1"
2014-05-01;"EI7";"EI";"1"
2015-01-01;"EI21";"EI";"1"
2015-03-01;"EI9";"EI";"1"
2015-06-01;"EI12";"EI";"1"
2015-08-01;"EI12";"EI";"1"
2016-01-01;"EI30";"EI";"2"
2016-04-01;"EI7";"EI";"1"
2016-09-01;"EI21";"EI";"1"
2016-09-01;"EI30";"EI";"1"
2017-01-01;"EI30";"EI";"2"
2017-02-01;"EI38";"EI";"1"
2017-09-01;"EI35";"EI";"1"
2017-10-01;"EI21";"EI";"1"
2017-11-01;"EI21";"EI";"1"
2017-12-01;"EI35";"EI";"1"
2018-02-01;"EI21";"EI";"1"
2018-04-01;"EI21";"EI";"1"
2018-04-01;"EI30";"EI";"1"
2018-04-01;"EI44";"EI";"2"
2018-05-01;"EI21";"EI";"1"
2018-05-01;"EI30";"EI";"1"
2018-07-01;"EI21";"EI";"1"
2018-08-01;"EI43";"EI";"3"
2018-09-01;"EI43";"EI";"1"
2018-09-01;"EI45";"EI";"1"
2018-10-01;"EI21";"EI";"1"
2018-11-01;"EI21";"EI";"1"
2018-11-01;"EI43";"EI";"1"
2019-01-01;"EI43";"EI";"3"
2019-03-01;"EI25";"EI";"2"
2019-03-01;"EI26";"EI";"1"
2019-05-01;"EI38";"EI";"1"
2019-07-01;"EI26";"EI";"1"
2019-08-01;"EI26";"EI";"1"
2019-10-01;"EI27";"EI";"1"
2019-11-01;"EI26";"EI";"1"
2019-11-01;"EI27";"EI";"1"
2019-11-01;"EI38";"EI";"1"
2019-12-01;"EI45";"EI";"1"
2020-01-01;"EI45";"EI";"2"
2020-02-01;"EI26";"EI";"1"

and this graph和这个图

lin_temp <- ggplot(data = table_sum, aes(x = mes_ano, y = code)) +
  geom_segment(data = table_range, size = 1.2, alpha= 0.8,
               aes(x = min, xend = max, y = code, yend = code))+
  geom_point(aes(shape = , color=count), size=2)

when I make the graph, the lines are mixed and not in sequence, like this https://i.ibb.co/LgQY838/1.png当我制作图表时,线条混合而不是按顺序排列,就像这样https://i.ibb.co/LgQY838/1.png

I used this How do you specifically order ggplot2 x axis instead of alphabetical order?我用过这个How do you special order ggplot2 x axis 而不是字母顺序? to get sorted y axis manually like this https://i.ibb.co/DfLSwMJ/2.png像这样手动对 y 轴进行排序https://i.ibb.co/DfLSwMJ/2.png

There is a faster way to do sort by date since I have more than 100 codes?因为我有 100 多个代码,所以有更快的按日期排序的方法吗?

You could use your date information to let ggplot handle the sorting.您可以使用日期信息让 ggplot 处理排序。 You only need to tell R that you'r column code is an ordered factor.您只需要告诉 R 您的列code是一个有序因子。

We make sure the sorting is right when we extract the levels:我们在提取级别时确保排序正确:

code_levels <- table_range[order(table_range$min), "code"]

Then we use the ordered levels when we make code to a factor:然后我们在对因子进行code时使用有序级别:

table_range$code <- factor(table_range$code, levels=code_levels )

Edit:编辑:

Add an example:添加示例:

MRE:雷:

table_range <- structure(list(code = structure(c(4L, 3L, 11L, 12L, 13L, 5L, 7L, 6L, 9L, 8L, 10L, 2L, 1L), .Label = c("EI9", "EI7", "EI21", "EI12", "EI30", "EI38", "EI35", "EI44", "EI43", "EI45", "EI25", "EI26", "EI27"), class = "factor"),
                              sp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "EI", class = "factor"), 
                              min = structure(c(16587, 16436, 17956, 17956, 18170, 16801, 17410, 17198, 17744, 17622, 17775, 16191, 15675), class = "Date"), 
                              max = structure(c(16648, 17836, 17956, 18293, 18201, 17652,  17501, 18201, 17897, 17622, 18262, 16892, 16495), class = "Date")),
                         row.names = c(NA, -13L), class = "data.frame")

table_sum <- structure(list(mes_ano = structure(c(15675, 16191, 16436, 16495, 16587, 16648, 16801, 16892, 17045, 17045, 17167, 17198, 17410, 17440, 17471, 17501, 17563, 17622, 17622, 17622, 17652, 17652, 17713, 17744, 17775, 17775, 17805, 17836, 17836, 17897, 17956, 17956, 18017, 18078, 18109, 18170, 18201, 18201, 18201, 18231, 18262, 18293), class = "Date"),
                            code = structure(c(13L, 12L, 2L, 13L, 1L, 1L, 6L, 12L, 2L, 6L, 6L, 8L, 7L, 2L, 2L, 7L, 2L, 2L, 6L, 10L, 2L, 6L, 2L, 9L, 9L, 11L, 2L, 2L, 9L, 9L, 3L, 4L, 8L, 4L, 4L, 5L, 4L, 5L, 8L, 11L, 11L, 4L), .Label = c("EI12", "EI21", "EI25", "EI26", "EI27", "EI30", "EI35", "EI38", "EI43", "EI44", "EI45", "EI7", "EI9"), class = "factor"),
                            sp = 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, 1L, 1L, 1L, 1L), .Label = "EI", class = "factor"),
                            count = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,   2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,   2L, 1L), .Label = c("1", "2", "3"), class = "factor")),
                       row.names = c(NA, -42L), class = "data.frame")

Making ordered factor:制作有序因子:

code_levels <- table_range[order(table_range$min), "code"]
table_range$code <- factor(table_range$code, levels=code_levels)

Reproducing plot:再现情节:

library(ggplot2)
ggplot(data = table_sum, aes(x = mes_ano, y = code)) +
geom_segment(data = table_range, size = 1.2, alpha= 0.8,
             aes(x = min, xend = max, y = code, yend = code))+
geom_point(aes(shape = , color=count), size=2)

在此处输入图片说明

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

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