R,ggplot2,分面直方图,降序,因子 x 轴标签
R, ggplot2, facetted histogram, decending order, factor x-axis labels
我有一个有趣的问题,我需要生成一个多面直方图系列,我想为每个面板按降序排列。下面的代码复制了这个问题。
library(ggplot2)
set.seed(1)
nGroup = 10; nSample = 10
df <- data.frame(Group=rep(1:nSample,nGroup),
Category = factor(sample(LETTERS,
nGroup*nSample,
replace=TRUE),
levels=LETTERS),
Amount = runif(nGroup*nSample))
ggplot(df,aes(x=Category,y=Amount)) +
geom_bar(stat='identity') +
facet_wrap(~Group,scales='free_x') +
labs(title="Some Random Histogram with Facets")
产生以下输出:
我试过以下方法:
library(ggplot2)
set.seed(1)
nGroup = 10; nSample = 10
df <- data.frame(Group=rep(1:nSample,nGroup),
Category = factor(sample(LETTERS,
nGroup*nSample,
replace=TRUE),
levels=LETTERS),
Amount = runif(nGroup*nSample))
ggplot(df,aes(x=reorder(Category,-Amount),y=Amount)) +
geom_histogram(stat='identity') +
facet_wrap(~Group,scales='free_x') +
labs(title="Some Random Histogram with Facets")
略有改善(见下文),但远未达到应有的水平。
如有任何建议,我们将不胜感激?我完全理解 x 轴标签不会在面板之间对应,这对我的应用程序来说很好。
更新:修复标签。
Mamoun 的回答完成了工作,但是,对于那些有兴趣修复 x 轴标签以删除交互后缀的人,可以这样做(包括其他一些所需的格式选项):
library(ggplot2)
set.seed(1)
nGroup = 10; nSample = 10
df <- data.frame(Group=factor(rep(1:nSample,nGroup)),
Category = factor(sample(LETTERS,
nGroup*nSample,
replace=TRUE),
levels=LETTERS),
Amount = runif(nGroup*nSample))
df <- ddply(df,c("Group","Category"),function(d){ data.frame(Amount=sum(d$Amount)) })
df$Interaction = interaction(df$Category,df$Group)
df$XLabel = as.character(df$Category)
ggplot(df,aes(x=reorder(Interaction,-Amount),y=Amount,fill=Amount)) +
geom_bar(stat='identity',color="black",width=1.0) +
scale_x_discrete(breaks=df$Interaction,labels=df$XLabel) +
facet_wrap(~Group,scales='free_x') +
theme_bw() + theme(legend.position=c(1,0),legend.justification=c(1,0)) +
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust=0.5, size=6)) +
scale_fill_gradient(low="blue",high="red") +
labs(title="Some Random Histogram with Facets",x="Category")
根据需要生成以下内容:
你可以像这样在 groups
和 Category
之间使用 reorder
和 interaction
ggplot(df,aes(x=reorder(interaction(Category, Group), -Amount, sum),y=Amount)) +
geom_bar(stat='identity') +
facet_wrap(~Group,scales='free_x') +
labs(title="Some Random Histogram with Facets")
我有一个有趣的问题,我需要生成一个多面直方图系列,我想为每个面板按降序排列。下面的代码复制了这个问题。
library(ggplot2)
set.seed(1)
nGroup = 10; nSample = 10
df <- data.frame(Group=rep(1:nSample,nGroup),
Category = factor(sample(LETTERS,
nGroup*nSample,
replace=TRUE),
levels=LETTERS),
Amount = runif(nGroup*nSample))
ggplot(df,aes(x=Category,y=Amount)) +
geom_bar(stat='identity') +
facet_wrap(~Group,scales='free_x') +
labs(title="Some Random Histogram with Facets")
产生以下输出:
我试过以下方法:
library(ggplot2)
set.seed(1)
nGroup = 10; nSample = 10
df <- data.frame(Group=rep(1:nSample,nGroup),
Category = factor(sample(LETTERS,
nGroup*nSample,
replace=TRUE),
levels=LETTERS),
Amount = runif(nGroup*nSample))
ggplot(df,aes(x=reorder(Category,-Amount),y=Amount)) +
geom_histogram(stat='identity') +
facet_wrap(~Group,scales='free_x') +
labs(title="Some Random Histogram with Facets")
略有改善(见下文),但远未达到应有的水平。
如有任何建议,我们将不胜感激?我完全理解 x 轴标签不会在面板之间对应,这对我的应用程序来说很好。
更新:修复标签。
Mamoun 的回答完成了工作,但是,对于那些有兴趣修复 x 轴标签以删除交互后缀的人,可以这样做(包括其他一些所需的格式选项):
library(ggplot2)
set.seed(1)
nGroup = 10; nSample = 10
df <- data.frame(Group=factor(rep(1:nSample,nGroup)),
Category = factor(sample(LETTERS,
nGroup*nSample,
replace=TRUE),
levels=LETTERS),
Amount = runif(nGroup*nSample))
df <- ddply(df,c("Group","Category"),function(d){ data.frame(Amount=sum(d$Amount)) })
df$Interaction = interaction(df$Category,df$Group)
df$XLabel = as.character(df$Category)
ggplot(df,aes(x=reorder(Interaction,-Amount),y=Amount,fill=Amount)) +
geom_bar(stat='identity',color="black",width=1.0) +
scale_x_discrete(breaks=df$Interaction,labels=df$XLabel) +
facet_wrap(~Group,scales='free_x') +
theme_bw() + theme(legend.position=c(1,0),legend.justification=c(1,0)) +
theme(axis.text.x = element_text(angle = 90, hjust = 0.5, vjust=0.5, size=6)) +
scale_fill_gradient(low="blue",high="red") +
labs(title="Some Random Histogram with Facets",x="Category")
根据需要生成以下内容:
你可以像这样在 groups
和 Category
之间使用 reorder
和 interaction
ggplot(df,aes(x=reorder(interaction(Category, Group), -Amount, sum),y=Amount)) +
geom_bar(stat='identity') +
facet_wrap(~Group,scales='free_x') +
labs(title="Some Random Histogram with Facets")