楼主: xlyshuai
46834 2

[问答] ggplot2画图时,如何控制x轴的排序? [推广有奖]

  • 1关注
  • 0粉丝

已卖:409份资源

硕士生

28%

还不是VIP/贵宾

-

威望
0
论坛币
630 个
通用积分
0.2400
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
191 点
帖子
127
精华
0
在线时间
152 小时
注册时间
2008-9-20
最后登录
2022-7-29

楼主
xlyshuai 发表于 2015-4-8 10:17:41 |AI写论文
5论坛币
请问在使用ggplot2画图时,能不能控制X轴的排序?

如下例:以from_ent_level   为X轴,以code_count   为y轴画图,画出的样子如下图,X轴排序是(1,2,3,4,生产出库,终端入库)。我希望的排序是以orderID列为序号显示,即X轴的排序为(生产出库,1,2,3,4,终端入库),请问有什么办法实现?


十分感谢!


以下是程序、生成图、数据:
lab_Y                 <<- paste("出库量(万",vDrugPkgSpec,")\n",sep = "")      ##        y轴标题

p1 <- ggplot(data = df_report_1_toPPT2, aes(x=from_ent_level,y=df_report_1_toPPT2$code_count))     ## 图形数据
p1 <- p1 + geom_bar( stat="identity" , width = 0.5, fill = "cornflowerblue")   ##  图层,柱状大小.颜色
p1 <- p1 + geom_text(label=label_Y,colour = "blue", vjust=-1)        ## 柱状上侧数字.颜色
p1 <- p1 + labs(x="\n生产、各流通级别、终端",y=lab_Y,title = "生产、各流通级别、终端流通量和纯销比\n") ## 图形标题.x/y轴标题
p1 <- p1 + theme(axis.text.x =  element_text(angle = 00, hjust = 0.5, size=12*1.33, color="black"))  ## x坐标轴文字大小.方向.颜色
p1 <- p1 + theme(axis.text.y =  element_text(angle = 00, hjust = 1, size=12*1.33, color="black"))   ## y坐标轴文字大小.方向.颜色
p1 <- p1 + theme(axis.title.x = element_text(size = 12*1.33, angle = 00)) ## x坐标轴标题大小.方向.颜色
p1 <- p1 + theme(axis.title.y = element_text(size = 12*1.33, angle = 90)) ##   y坐标轴标题大小.方向.颜色
p1 <- p1 + scale_y_continuous(limits=c(0, max(aex_Y)*1.1),labels = comma) # 调整y坐标显示方式
p1 <- p1 + theme( plot.title = element_text(size = 16*1.33, face = "bold"))


p1
p.jpeg

  orderID  from_ent_level   code_count  
       1       生产出库             325920      
       3              1                  20280         
       4              2                  50760           
       5              3                  7813           
       6              4                 620               
       9       终端购进            13292        

最佳答案

dataorz 查看完整内容

方法1方法2
关键词:ggplot2 gplot plot GPL Order 如何

沙发
dataorz 发表于 2015-4-8 10:17:42
  1. df_report_1_toPPT2$orderID <- as.integer(rownames(df_report_1_toPPT2))
  2. p1 <- ggplot(data = df_report_1_toPPT2, aes(x=orderID,y=df_report_1_toPPT2$code_count))
  3. p1 <- p1 + geom_bar( stat="identity" , width = 0.5, fill = "cornflowerblue")
  4. p1 + scale_x_continuous(breaks=df_report_1_toPPT2$orderID, labels=df_report_1_toPPT2$from_ent_level)
复制代码
方法1
  1. df_report_1_toPPT2$from_ent_level <- factor(as.integer(rownames(df_report_1_toPPT2)),labels=df_report_1_toPPT2$from_ent_level)
  2. p1 <- ggplot(data = df_report_1_toPPT2, aes(x=from_ent_level,y=df_report_1_toPPT2$code_count))
  3. p1 + geom_bar( stat="identity" , width = 0.5, fill = "cornflowerblue")
复制代码
方法2

藤椅
xlyshuai 发表于 2015-4-8 14:27:42
dataorz 发表于 2015-4-8 13:48
方法1方法2
十分感谢!两种方法都很棒!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-5 10:35