ggplot2:将x轴离散值分组为子组 [英] ggplot2: group x axis discrete values into subgroups
本文介绍了ggplot2:将x轴离散值分组为子组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用ggplot2创建一个条形图,其中x轴的离散值将被分组为子组(请参见所附图片-图片来自网络,我尚无该图的代码).
I would like to create a bar plot with ggplot2 in which the discrete values of the x axis would be grouped into subgroups (see picture attached - the picture is from the web I do not have a code for the plot yet).
感谢您的帮助!
推荐答案
两种方法:
示例数据:
dat <- data.frame(value=runif(26)*10,
grouping=c(rep("Group 1",10),
rep("Group 2",10),
rep("Group 3",6)),
letters=LETTERS[1:26])
head(dat)
value grouping letters
1 8.316451 Group 1 A
2 9.768578 Group 1 B
3 4.896294 Group 1 C
4 2.004545 Group 1 D
5 4.905058 Group 1 E
6 8.997713 Group 1 F
没有多面性:
ggplot(dat, aes(grouping, value, fill=letters, label = letters)) +
geom_bar(position="dodge", stat="identity") +
geom_text(position = position_dodge(width = 1), aes(x=grouping, y=0))
带有小平面:
ggplot(dat, aes(letters,value, label = letters)) +
geom_bar(stat="identity") +
facet_wrap(~grouping, scales="free")
Facetting具有明显的优势,不必为标签的定位而费解.
Facetting has the obvious advantage of not having to muck about with the positioning of the labels.
这篇关于ggplot2:将x轴离散值分组为子组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文