ggplot2 geom_bar ...如何保持data.frame的顺序 [英] ggplot2 geom_bar ... how to keep order of data.frame

查看:1385
本文介绍了ggplot2 geom_bar ...如何保持data.frame的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于geom_bar中数据顺序的问题。



这是我的数据集:

  SM_P,手掌上有黑色斑点,16.2 
DM_P,手掌弥漫性黑变,78.6
SM_T,躯干斑点黑变,57.3
DM_T,躯干弥漫性黑色素沉着,20.6
LEU_M,隐性黑变病,17
WB_M,全身黑变,8.4
SK_P,手掌有斑点角化,35.4
DK_P,手掌有弥漫性角化,23.5
SK_S,脚底有斑点角化,66
DK_S ,单纯弥漫性角化,52.8
CH_BRON,背角角化,39
LIV_EN,慢性支气管炎,6
DOR,肝脏肿大,2.4
CARCI,癌,1

我分配以下colnames:

  colnames(df)< -c(abbr,derma,prevalence)#分配行和列名称

然后我绘制:

  ggplot(data = df,aes(x = derma ,y =流行率))+ geom_bar(stat =identity)+ coord_flip()

p>

为什么ggplot2会随机更改数据的顺序。我希望我的数据顺序与我的data.frame保持一致。



非常感谢任何帮助!

解决方案

发布为答案,因为评论线程变长。您必须通过使用您映射的变量的因子水平来指定订单,其中 aes(x = ...)

 #锁定系数等级顺序
df $ derma < - factor(df $ derma,levels = df $ derma)

#plot
ggplot(data = df,aes(x = derma,y = prevalence))+
geom_bar(stat =identity)+ coord_flip()

结果与 df 中的顺序相同:

 #或按流行程度排序:
df $ derma < - factor(df $ derma ,levels = df $ derma [order(df $ prevalence)])

/ p>






<我读了da ta像这样:

  read.table(text = 
SM_P,手掌上发现黑色素沉着,16.2
DM_P,手掌弥漫性黑变,78.6
SM_T,躯干斑点黑变,57.3
DM_T,躯干弥漫性黑色素沉着,20.6
LEU_M,隐性黑变病,17
WB_M,整体8.4
SK_P,手掌发现角化,35.4
DK_P,手掌发散角化,23.5
SK_S,单根发现角化,66
DK_S,鞋底弥漫性角化,52.8
CH_BRON,背角角化病,39
LIV_EN,慢性支气管炎,6
DOR,肝脏肿大,2.4
CARCI,癌症,1,标题= F,sep =' ,')
colnames(df)< - c(abbr,derma,prevalence)#分配行和列名称


I have a question concerning the order of data in my geom_bar.

This is my dataset:

  SM_P,Spotted melanosis on palm,16.2
  DM_P,Diffuse melanosis on palm,78.6
  SM_T,Spotted melanosis on trunk,57.3
  DM_T,Diffuse melanosis on trunk,20.6
  LEU_M,Leuco melanosis,17
  WB_M,Whole body melanosis,8.4
  SK_P,Spotted keratosis on palm,35.4
  DK_P,Diffuse keratosis on palm,23.5
  SK_S,Spotted keratosis on sole,66
  DK_S,Diffuse keratosis on sole,52.8
  CH_BRON,Dorsal keratosis,39
  LIV_EN,Chronic bronchities,6
  DOR,Liver enlargement,2.4
  CARCI,Carcinoma,1

I assign the following colnames:

  colnames(df) <- c("abbr", "derma", "prevalence") # Assign row and column names

Then I plot:

  ggplot(data=df, aes(x=derma, y=prevalence)) + geom_bar(stat="identity") + coord_flip()

Why does ggplot2 randomly change the order of my data. I would like to have the order of my data in align with my data.frame.

Any help is much appreciated!

解决方案

Posting as answer because comment thread getting long. You have to specify the order by using the factor levels of the variable you map with aes(x=...)

# lock in factor level order
df$derma <- factor(df$derma, levels = df$derma)

# plot
ggplot(data=df, aes(x=derma, y=prevalence)) + 
    geom_bar(stat="identity") + coord_flip()

Result, same order as in df:

# or, order by prevalence:
df$derma <- factor(df$derma, levels = df$derma[order(df$prevalence)])

Same plot command gives:


I read in the data like this:

read.table(text=
"SM_P,Spotted melanosis on palm,16.2
DM_P,Diffuse melanosis on palm,78.6
SM_T,Spotted melanosis on trunk,57.3
DM_T,Diffuse melanosis on trunk,20.6
LEU_M,Leuco melanosis,17
WB_M,Whole body melanosis,8.4
SK_P,Spotted keratosis on palm,35.4
DK_P,Diffuse keratosis on palm,23.5
SK_S,Spotted keratosis on sole,66
DK_S,Diffuse keratosis on sole,52.8
CH_BRON,Dorsal keratosis,39
LIV_EN,Chronic bronchities,6
DOR,Liver enlargement,2.4
CARCI,Carcinoma,1", header=F, sep=',')
colnames(df) <- c("abbr", "derma", "prevalence") # Assign row and column names

这篇关于ggplot2 geom_bar ...如何保持data.frame的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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