首页 > 解决方案 > 在ggplot的x轴上以适当的顺序放置时间(月/年)

问题描述

不过,我无法在 ggplot 的 x 轴上以正确的顺序方式放置时间(月/年)。我试图根据以前的帖子来解决它,例如使用unique,但直到现在才修复它。

任何建议将不胜感激。

使用的代码:

library(dplyr);library(string)
#Original date format
#"10/1/2020" "5/1/2020" "9/1/2020" 
data$StopMonthYearNoneStop0_Format<-NULL
data$StopMonthYearNoneStop0_Format<-data$StopMonthYearNoneStop0
data$StopMonthYearNoneStop0_Format<-str_replace(data$StopMonthYearNoneStop0_Format, pattern = "/\\d+\\/", replacement = "/")
data$StopMonthYearNoneStop0_Format<-as.Date( data$StopMonthYearNoneStop0, "%m/%d/%Y")

data...lim<-subset(data, data$STOP.STATUS.afterFiscal.Year==1)
data...lim$StopMonthYearNoneStop0_Format <- factor(data...lim$StopMonthYearNoneStop0_Format, levels = rev(unique(data...lim$StopMonthYearNoneStop0_Format)), ordered=TRUE)
levels(data...lim$StopMonthYearNoneStop0_Format)

data$StopMonthYearNoneStop0_Format<-format(data$StopMonthYearNoneStop0_Format, format="%B %d %Y")

df$StopMonthYearNoneStop0_Format <- factor(df$StopMonthYearNoneStop0_Format, levels=unique(df$StopMonthYearNoneStop0_Format))
data...lim$StopMonthYearNoneStop0_Format <- factor(data...lim$StopMonthYearNoneStop0_Format, 
                                              levels=c( "4/1/2015", "5/1/2015","6/1/2015",
                                                      "7/1/2015", "8/1/2019","9/1/2015",
                                                      "10/1/2015","11/1/2015","12/1/2015",
                                                      #=========================
                                                      "1/1/2016","2/1/2016","3/1/2016",
                                                      "4/1/2016", "5/1/2016","6/1/2016",
                                                      "7/1/2016", "8/1/2016","9/1/2016",
                                                      "10/1/2016","11/1/2016","12/1/2016",
                                                      #=========================
                                                      "1/1/2017","2/1/2017","3/1/2017",
                                                      "4/1/2017", "5/1/2017","6/1/2017",
                                                      "7/1/2017", "8/1/2017","9/1/2017",
                                                      "10/1/2017","11/1/2017","12/1/2017",
                                                      #=========================
                                                      "1/1/2018","2/1/2018","3/1/2018",
                                                      "4/1/2018", "5/1/2018","6/1/2018",
                                                      "7/1/2018", "8/1/2018","9/1/2018",
                                                      "10/1/2018","11/1/2018","12/1/2018",
                                                      #=========================
                                                        "1/1/2019","2/1/2019","3/1/2019",
                                                      "4/1/2019", "5/1/2019","6/1/2019",
                                                      "7/1/2019", "8/1/2019","9/1/2019",
                                                      "10/1/2019","11/1/2019","12/1/2019",
                                                      #=========================
                                                      "1/1/2020","2/1/2020",  "3/1/2020",
                                                      "4/1/2020","5/1/2020",
                                                      "6/1/2020","7/1/2020","8/1/2020",
                                                      "9/1/2020","10/1/2020","11/1/2020"))

> ***Error in `levels<-`(`*tmp*`, value = as.character(levels)) : 
  factor level [53] is duplicated***

data...lim<-subset(data...lim, select= c(STOP.STATUS.afterFiscal.Year, 
                                   StopMonthYearNoneStop0_Format, 
                                   OrgClass_Combined.Gov_3gps ))

df <- data...lim %>% group_by(StopMonthYearNoneStop0_Format) %>% 
   count(OrgClass_Combined.Gov_3gps) %>% mutate(Percent = n / sum(n)*100)

> df
# A tibble: 217 x 4
# Groups:   StopMonthYearNoneStop0_Format [70]
   StopMonthYearNoneStop0_Format OrgClass_Combined.Gov_3gps      n Percent
   <ord>                         <chr>                       <int>   <dbl>
 1 "         "                   FED/NIH/OTHER_GOV           11444   5.59 
 2 "         "                   INDUSTRY                    39241  19.2  
 3 "         "                   OTHER                      153153  74.8  
 4 "         "                   NA                            870   0.425
 5 "4/1/2015 "                   FED/NIH/OTHER_GOV               6   8.45 
 6 "4/1/2015 "                   INDUSTRY                       16  22.5  
 7 "4/1/2015 "                   OTHER                          49  69.0  
 8 "9/1/2015 "                   FED/NIH/OTHER_GOV               7   7.07 
 9 "9/1/2015 "                   INDUSTRY                       25  25.3  
10 "9/1/2015 "                   OTHER                          67  67.7  
# ... with 207 more rows


df$StopMonthYearNoneStop0_Format <- factor(df$StopMonthYearNoneStop0_Format, levels=unique(df$StopMonthYearNoneStop0_Format))

p<- ggplot(df, aes(y = StopMonthYearNoneStop0_Format, x = Percent, fill = OrgClass_Combined.Gov_3gps))+
  geom_bar(stat = "identity")+
  coord_flip()+ 
  labs(y = "Stoppage (month/year)", x = "Percentage",fill = "Organization class")
p+theme_classic()+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))


#################################### by number #############################################
p<- ggplot(df, aes(y = StopMonthYearNoneStop0_Format, x = n, fill = OrgClass_Combined.Gov_3gps))+
  geom_bar(stat = "identity")+
  coord_flip()+ 
  labs(y = "Stoppage (month/year)", x = "Number",fill = "Organization class")
p+theme_classic()+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))



在此处输入图像描述

标签: rdatetimebar-chart

解决方案


将 X 轴类保持为 date 而不是factor.

library(ggplot2)
data$StopMonthYearNoneStop0_Format<- as.Date(data$StopMonthYearNoneStop0, "%m/%d/%Y")

ggplot(data, aes(y = StopMonthYearNoneStop0_Format, x = Percent, fill = OrgClass_Combined.Gov_3gps))+
  geom_bar(stat = "identity")+
  coord_flip()+ 
  labs(y = "Stoppage (month/year)", x = "Percentage",fill = "Organization class") + 
  theme_classic()+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

如果您需要格式化 X 轴,您可以使用?scale_x_date.


推荐阅读