【问题标题】:Grouped barplot with R [duplicate]带 R 的分组条形图 [重复]
【发布时间】:2015-11-24 07:43:24
【问题描述】:

我正在尝试使用以下数据绘制分组条形图

groups  gents   ladies
rd  3.62    2.12
ab  2.38    1.55
dh  1.98    1.65
na  2.71    1.52
pg  2.25    1.8
ac  2.37    1.77
nb  2.28    1.68
it  2.3     1.46
ha  3.06    2.05
th  2.22    1.63
hy  2.66    1.59
ad  2.83    1.85
hy  4.16    2.53
mj  2.83    1.98
ng  3.1     2.32

这是我的代码。

> library(reshape2)
> library(ggplot2)
> df.long<-melt(data)
Using groups as id variables
> ggplot(df.long,aes(x=groups,y=value,fill=variable))+ labs(x = "groups", y = "connections") +
+     geom_bar(stat="identity",position="dodge")

使用我的代码,我按字母顺序(ab、ac、ad、dh)获得条形图的 X 轴(组)。我想按我的数据顺序获得 X 轴(rd ,ab,dh 等)我该怎么做?

【问题讨论】:

  • @Axeman 我的问题不同,因为我不需要根据频率对 x 轴进行排序。我需要根据我的组列的数据对 x 轴进行排序。

标签: r ggplot2 bar-chart reshape2


【解决方案1】:

这会解决你的问题。

     library(reshape2)
     library(ggplot2)
     df.long<-melt(data)
     df.long$groups <- factor(df.long$groups, levels=unique(df.long$groups))
     ggplot(df.long,aes(x=groups,y=value,fill=variable))+ labs(x = "groups", y = "connections") + geom_bar(stat="identity",position="dodge")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 2015-06-28
    • 2018-10-27
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 2016-11-07
    相关资源
    最近更新 更多