用 R 分组的条形图
Grouped barplot with R
我正在尝试使用以下数据绘制分组条形图
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 等)我必须为此做什么?
这将解决您的问题。
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")
我正在尝试使用以下数据绘制分组条形图
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 等)我必须为此做什么?
这将解决您的问题。
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")