Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.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 我的ggplot图首先显示2018年数据,然后显示2017年数据。我想重新排序_R_Ggplot2 - Fatal编程技术网

R 我的ggplot图首先显示2018年数据,然后显示2017年数据。我想重新排序

R 我的ggplot图首先显示2018年数据,然后显示2017年数据。我想重新排序,r,ggplot2,R,Ggplot2,我有2017年9月至2018年8月的数据 但当我通过ggplot显示它时,它从2018年1月到2018年8月,然后是2017年9月到12月。我先要2017年的数据 这是我使用的代码: ggplot(data = p3, aes(x = month, y = percentage)) + geom_bar(aes(y = percentage*100), stat = "identity")+ geom_text(aes(y = percentage

我有2017年9月至2018年8月的数据

但当我通过ggplot显示它时,它从2018年1月到2018年8月,然后是2017年9月到12月。我先要2017年的数据

这是我使用的代码:

ggplot(data = p3, 
       aes(x = month, 
           y = percentage)) +
  geom_bar(aes(y = percentage*100), stat = "identity")+
  geom_text(aes(y = percentage, label = formattable::percent(percentage)), 
            vjust = 1.5, colour="red")

一种解决方案是将
month
列预先挂起“01/”以转换为日期(即每个月的第一天)。然后您可以使用
缩放x\u日期

library(dplyr)
library(ggplot2)

p3 %>% 
  mutate(Date = as.Date(paste0("01/", month), "%d/%m/%Y")) %>% 
  ggplot(aes(Date, percentage)) + 
    geom_col() + 
    geom_text(aes(y = percentage, 
                  label = formattable::percent(percentage)), 
              vjust = 1.5, 
              colour = "red") +
    scale_x_date(date_breaks = "1 month", 
                 date_labels = "%m/%Y")

我猜
类(p3$month)
将返回“factor”。您需要重新排序因子级别或将列格式化为日期。可能重复:@neilfws我该怎么做?我是R的新手,请帮忙