R中的coplot或散点图的月份排序
Sorting month chronologicaly of coplot or scatterplot in R
给定一个数据帧 df
如下:
df <- structure(list(country = structure(c(4L, 4L, 4L, 4L, 4L, 4L,
1L, 1L, 1L, 1L, 1L, 1L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L,
6L, 6L, 9L, 9L, 9L, 9L, 9L, 9L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 5L,
5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Hong Kong", "Indonesia", "Korea", "Mainland China",
"Malaysia", "Singapore", "Taiwan", "Thailand", "Vietnam"), class = "factor"),
month = structure(c(3L, 2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L,
6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L, 3L,
2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L,
6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L), .Label = c("April",
"February", "January", "June", "March", "May"), class = "factor"),
tourist_arrivals = c(400.16, 28.78, 3.12, 0, 0, 0, 3207.802,
1991.123, 82.285, 4.125, 8.139, 14.606, 812.97, 357.357,
78.259, 2.559, 3.25, 7.491, 1690, 732, 240, 0.75, 0.88, 2.17,
1994.1, 1242.7, 449.9, 26.2, 22.7, 8.8, 3810.16, 2061.99,
819.43, 0, 0, 0, 2164.459, 1397.912, 671.084, 0, 0, 0, 1272.708,
685.212, 83.497, 29.415, 30.861, 36.943, 1450.62, 1030.88,
236.73, 113.03, 56.03, 34.51), cases = c(11719, 68219, 2663,
1784, 203, 644, 13, 82, 619, 323, 47, 121, 10, 29, 283, 107,
13, 5, 13, 85, 828, 15243, 18715, 9023, 5, 11, 191, 63, 58,
27, 14, 42, 1595, 1303, 127, 90, 8, 16, 2742, 3236, 1817,
826, 11, 3139, 6737, 878, 703, 1332, 0, 0, 1528, 8590, 16355,
29912), deaths = c(259, 2614, 448, 1322, 2, 3, 0, 2, 2, 0,
0, 3, 0, 1, 4, 1, 1, 0, 0, 0, 3, 12, 8, 3, 0, 0, 0, 0, 0,
0, 0, 0, 10, 44, 3, 1, 0, 0, 43, 59, 13, 6, 0, 17, 148, 82,
23, 12, 0, 0, 136, 656, 821, 1263), death_rate = c(0.0221008618482806,
0.03831777070904, 0.168231318062336, 0.741031390134529, 0.00985221674876847,
0.0046583850931677, 0, 0.024390243902439, 0.00323101777059774,
0, 0, 0.0247933884297521, 0, 0.0344827586206897, 0.0141342756183746,
0.00934579439252336, 0.0769230769230769, 0, 0, 0, 0.0036231884057971,
0.000787246604999016, 0.000427464600587764, 0.000332483652887066,
0, 0, 0, 0, 0, 0, 0, 0, 0.00626959247648903, 0.0337682271680737,
0.0236220472440945, 0.0111111111111111, 0, 0, 0.0156819839533187,
0.0182323856613103, 0.00715465052283985, 0.00726392251815981,
0, 0.00541573749601784, 0.0219682351194894, 0.0933940774487472,
0.0327169274537696, 0.00900900900900901, 0, 0, 0.0890052356020942,
0.0763678696158324, 0.0501987159889942, 0.0422238566461621
)), class = "data.frame", row.names = c(NA, -54L))
我用scatterplot(tourist_arrivals ~ month|country, data = df)
或coplot(tourist_arrivals ~ month|country, type = 'b', data = df)
画了一个散点图,但是如你所见,month
的顺序不对,请问如何排列?
谢谢。
您可以使用
library(tidyverse)
df %>%
mutate(month = factor(month, levels= c("January", "February", "March",
"April", "May", "June"))) %>%
ggplot(aes(x = month, y = tourist_arrivals)) +
geom_boxplot()
要获得 scatterplot
正确的月份顺序,您可以使用
library(car)
df1 <- df %>%
mutate(month = factor(month, levels= c("January", "February", "March",
"April", "May", "June")))
scatterplot(tourist_arrivals ~ month|country, data = df1)
同样的方法你可以试试
coplot(tourist_arrivals ~ month|country, type = 'b', data = df1)
给定一个数据帧 df
如下:
df <- structure(list(country = structure(c(4L, 4L, 4L, 4L, 4L, 4L,
1L, 1L, 1L, 1L, 1L, 1L, 7L, 7L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L,
6L, 6L, 9L, 9L, 9L, 9L, 9L, 9L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 5L,
5L, 5L, 5L, 5L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Hong Kong", "Indonesia", "Korea", "Mainland China",
"Malaysia", "Singapore", "Taiwan", "Thailand", "Vietnam"), class = "factor"),
month = structure(c(3L, 2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L,
6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L, 3L,
2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L,
6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L, 3L, 2L, 5L, 1L, 6L, 4L), .Label = c("April",
"February", "January", "June", "March", "May"), class = "factor"),
tourist_arrivals = c(400.16, 28.78, 3.12, 0, 0, 0, 3207.802,
1991.123, 82.285, 4.125, 8.139, 14.606, 812.97, 357.357,
78.259, 2.559, 3.25, 7.491, 1690, 732, 240, 0.75, 0.88, 2.17,
1994.1, 1242.7, 449.9, 26.2, 22.7, 8.8, 3810.16, 2061.99,
819.43, 0, 0, 0, 2164.459, 1397.912, 671.084, 0, 0, 0, 1272.708,
685.212, 83.497, 29.415, 30.861, 36.943, 1450.62, 1030.88,
236.73, 113.03, 56.03, 34.51), cases = c(11719, 68219, 2663,
1784, 203, 644, 13, 82, 619, 323, 47, 121, 10, 29, 283, 107,
13, 5, 13, 85, 828, 15243, 18715, 9023, 5, 11, 191, 63, 58,
27, 14, 42, 1595, 1303, 127, 90, 8, 16, 2742, 3236, 1817,
826, 11, 3139, 6737, 878, 703, 1332, 0, 0, 1528, 8590, 16355,
29912), deaths = c(259, 2614, 448, 1322, 2, 3, 0, 2, 2, 0,
0, 3, 0, 1, 4, 1, 1, 0, 0, 0, 3, 12, 8, 3, 0, 0, 0, 0, 0,
0, 0, 0, 10, 44, 3, 1, 0, 0, 43, 59, 13, 6, 0, 17, 148, 82,
23, 12, 0, 0, 136, 656, 821, 1263), death_rate = c(0.0221008618482806,
0.03831777070904, 0.168231318062336, 0.741031390134529, 0.00985221674876847,
0.0046583850931677, 0, 0.024390243902439, 0.00323101777059774,
0, 0, 0.0247933884297521, 0, 0.0344827586206897, 0.0141342756183746,
0.00934579439252336, 0.0769230769230769, 0, 0, 0, 0.0036231884057971,
0.000787246604999016, 0.000427464600587764, 0.000332483652887066,
0, 0, 0, 0, 0, 0, 0, 0, 0.00626959247648903, 0.0337682271680737,
0.0236220472440945, 0.0111111111111111, 0, 0, 0.0156819839533187,
0.0182323856613103, 0.00715465052283985, 0.00726392251815981,
0, 0.00541573749601784, 0.0219682351194894, 0.0933940774487472,
0.0327169274537696, 0.00900900900900901, 0, 0, 0.0890052356020942,
0.0763678696158324, 0.0501987159889942, 0.0422238566461621
)), class = "data.frame", row.names = c(NA, -54L))
我用scatterplot(tourist_arrivals ~ month|country, data = df)
或coplot(tourist_arrivals ~ month|country, type = 'b', data = df)
画了一个散点图,但是如你所见,month
的顺序不对,请问如何排列?
谢谢。
您可以使用
library(tidyverse)
df %>%
mutate(month = factor(month, levels= c("January", "February", "March",
"April", "May", "June"))) %>%
ggplot(aes(x = month, y = tourist_arrivals)) +
geom_boxplot()
要获得 scatterplot
正确的月份顺序,您可以使用
library(car)
df1 <- df %>%
mutate(month = factor(month, levels= c("January", "February", "March",
"April", "May", "June")))
scatterplot(tourist_arrivals ~ month|country, data = df1)
同样的方法你可以试试
coplot(tourist_arrivals ~ month|country, type = 'b', data = df1)