防止ggplot2自动排序x轴 [英] Prevent ggplot2 from automatically ordering x-axis

查看:936
本文介绍了防止ggplot2自动排序x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下样本数据集,它是用户在应用程序中选择创建的子集。

  cons_mergedAll<  - 结构(清单(计数= c(487L,463L,560L,578L,563L,557L,
65L,48L,324L,447L,166L,108L,351L,301L,389L,384L,333L,
345L ,417L,384L,316L,336L,381L,379L,230L,252L),type = c(open,
closed,open,closed,open,closed 打开,关闭,
打开,关闭,打开,关闭,打开,关闭,打开,
关闭,打开,关闭,打开,关闭,打开,关闭,
打开,关闭,打开,关闭),month = c(1,1 ,2,2,3,
3,4,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,$ b 2015年03月,2015年03月,2015年04月,2015年02月,2015年02月 ,2014 04,2015 04,2015 04,
2014 05,2014 05,2014 06,2014 06,2014 07,2014 07
2014 08,2014 08,2014 09,2014 09,2014 10,2014 10,
2014 (2015年1月),
2015年1月,2015年2月,2015年2月,2015年3月,2014年12月 2015年3月2014年4月
2014年4月2015年4月2015年4月2014年5月2014年5月2014年6月
2014 2014年6月2014年7月2014年7月2014年8月2014年8月2014年9月2014年b $ b 2014年9月2014年10月2014年10月2014年10月 2014年11月,2014年11月,2014年12月,
2014年12月)),.Names = c(count,type,month,month_yr,
month_yr2),row.names = c(1L,40L,4L,43L,7L,46L,10L,49L,
13L,52L,15L,54L,18L,57L,21L,60L,24L,63L ,27L,66L,30L,
69L,33L,72L,36L,75L),class =data.frame)

我试图用 ggplot2 来做一个条形图,并且防止 x-bar 但是使用数据集的命令
我该如何处理它

以下是我使用的示例代码

  cons_mergedAll < -  cons_mergedAll [order(cons_mergedAll $ month,cons_ mergedAll $ yr_report)] 
cons_mergedAll $ month_yr2< - factor(cons_mergedAll $ month_yr2,ordered = T)
ggplot(cons_mergedAll,aes(x = month_yr2,y = count,fill = type))+ geom_bar(stat =identity,position =dodge)+
ylab(门票数量)+ xlab(月)

但按字母顺序依次排列 - 我已经检查过这里这里,但仍然没有排序从2014年4月到2015年4月

解决方案

这可能是一个选项

 #根据您的要求添加一个包含订单的列
cons_mergedAll $ order = c(1:length(cons_mergedAll $ count))

#使用添加的列重新排序()ggplot中的数据
ggplot(cons_mergedA ll,aes(reorder(month_yr2,order),count,fill = type))+
geom_bar(stat =identity,position =dodge)+
ylab(门票数量) +
xlab(月)


I have the following sample data set which is a subset created by user selects in an application

cons_mergedAll <- structure(list(count = c(487L, 463L, 560L, 578L, 563L, 557L, 
65L, 48L, 324L, 447L, 166L, 108L, 351L, 301L, 389L, 384L, 333L, 
345L, 417L, 384L, 316L, 336L, 381L, 379L, 230L, 252L), type = c("open", 
"closed", "open", "closed", "open", "closed", "open", "closed", 
"open", "closed", "open", "closed", "open", "closed", "open", 
"closed", "open", "closed", "open", "closed", "open", "closed", 
"open", "closed", "open", "closed"), month = c(1, 1, 2, 2, 3, 
3, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 
12, 12), month_yr = c("2015 01", "2015 01", "2015 02", "2015 02", 
"2015 03", "2015 03", "2014 04", "2014 04", "2015 04", "2015 04", 
"2014 05", "2014 05", "2014 06", "2014 06", "2014 07", "2014 07", 
"2014 08", "2014 08", "2014 09", "2014 09", "2014 10", "2014 10", 
"2014 11", "2014 11", "2014 12", "2014 12"), month_yr2 = c("2015 Jan", 
"2015 Jan", "2015 Feb", "2015 Feb", "2015 Mar", "2015 Mar", "2014 Apr", 
"2014 Apr", "2015 Apr", "2015 Apr", "2014 May", "2014 May", "2014 Jun", 
"2014 Jun", "2014 Jul", "2014 Jul", "2014 Aug", "2014 Aug", "2014 Sep", 
"2014 Sep", "2014 Oct", "2014 Oct", "2014 Nov", "2014 Nov", "2014 Dec", 
"2014 Dec")), .Names = c("count", "type", "month", "month_yr", 
"month_yr2"), row.names = c(1L, 40L, 4L, 43L, 7L, 46L, 10L, 49L, 
13L, 52L, 15L, 54L, 18L, 57L, 21L, 60L, 24L, 63L, 27L, 66L, 30L, 
69L, 33L, 72L, 36L, 75L), class = "data.frame")

I am trying to do a bar plot using ggplot2 and prevent automatic ordering on x-bar but use the order on the dataset How can i approach it

Here is the sample code I am using

   cons_mergedAll <- cons_mergedAll[order(cons_mergedAll$month, cons_mergedAll$yr_report),]
   cons_mergedAll$month_yr2 <- factor(cons_mergedAll$month_yr2, ordered = T)
   ggplot(cons_mergedAll , aes( x=month_yr2 ,y=count, fill=type )) + geom_bar( stat="identity",position="dodge") +
     ylab("Number of Tickets") +   xlab("Month")

But sorts by alphabetical order still -- I have checked here and here but still doesnt sort from 2014-Apr to 2015-Apr

解决方案

This could be an option

# Add a column with order, as per your requirement
cons_mergedAll$order = c(1:length(cons_mergedAll$count))

# use the added column to reorder() your data in ggplot
ggplot(cons_mergedAll , aes(reorder(month_yr2, order) ,count, fill=type )) +
geom_bar( stat="identity",position="dodge") +
ylab("Number of Tickets") +
xlab("Month")

这篇关于防止ggplot2自动排序x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆