Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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中的coplot或散点图进行排序_R_Dplyr_Scatter Plot - Fatal编程技术网

按时间顺序对R中的coplot或散点图进行排序

按时间顺序对R中的coplot或散点图进行排序,r,dplyr,scatter-plot,R,Dplyr,Scatter Plot,给定数据帧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,

给定数据帧
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))
df您可以使用

library(tidyverse)

df %>% 
  mutate(month =  factor(month, levels= c("January", "February", "March", 
                                             "April", "May", "June"))) %>% 
ggplot(aes(x = month, y = tourist_arrivals)) + 
geom_boxplot()
library(car)
df1 <- df %>% 
  mutate(month =  factor(month, levels= c("January", "February", "March", 
                                          "April", "May", "June"))) 

scatterplot(tourist_arrivals ~ month|country, data = df1) 

要以正确的月份顺序获取散点图,可以使用

library(tidyverse)

df %>% 
  mutate(month =  factor(month, levels= c("January", "February", "March", 
                                             "April", "May", "June"))) %>% 
ggplot(aes(x = month, y = tourist_arrivals)) + 
geom_boxplot()
library(car)
df1 <- df %>% 
  mutate(month =  factor(month, levels= c("January", "February", "March", 
                                          "April", "May", "June"))) 

scatterplot(tourist_arrivals ~ month|country, data = df1) 

重新排列月份的快捷方式是使用
month.name
,例如
df$month感谢您的建议@stefan。我尝试过你的解决方案,它给出了所有月份的名称,但在当前问题中,只有6个月。。。。在这种情况下,
droplevels(因子(df$month,levels=month.name))