我的ggplot图显示了2018年的第一个和2017年的数据。我想重新排序

问题描述 投票:1回答:1

plot

我有从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")
r ggplot2
1个回答
2
投票

一种解决方案是通过预先挂起的“01 /”将month列转换为日期(即每个月的第一天)。然后你可以使用scale_x_date

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")
© www.soinside.com 2019 - 2024. All rights reserved.