我有一个包含以下条目的矩阵:
dput(MilDis[1:200,])
structure(list(hhDomMil = c("HED", "ETB", "HED", "ETB", "PER",
"BUM", "EXP", "TRA", "TRA", "PMA", "MAT", "MAT", "KON", "ETB",
"PMA", "PMA", "HED", "BUM", "BUM", "HED", "PMA", "PMA", "HED",
"TRA", "BUM", "EXP", "BUM", "PMA", "ETB", "MAT", "ETB", "ETB",
"KON", "MAT", "TRA", "BUM", "BUM", "TRA", "TRA", "PMA", "PMA",
"PMA", "MAT", "ETB", "TRA", "BUM", "TRA", "MAT", "BUM", "ETB",
"TRA", "TRA", "BUM", "KON", "ETB", "ETB", "ETB", "BUM", "KON",
"ETB", "ETB", "PMA", "TRA", "PER", "PER", "MAT", "HED", "KON",
"TRA", "TRA", "TRA", "EXP", "TRA", "BUM", "MAT", "MAT", "TRA",
"PMA", "HED", "PER", "TRA", "PER", "EXP", "PER", "BUM", "KON",
"BUM", "ETB", "ETB", "TRA", "PER", "ETB", "KON", "KON", "BUM",
"ETB", "BUM", "MAT", "BUM", "KON", "KON", "ETB", "MAT", "KON",
"PER", "ETB", "ETB", "KON", "PMA", "PER", "HED", "HED", "PMA",
"MAT", "PMA", "PER", "PMA", "TRA", "TRA", "MAT", "BUM", "BUM",
"KON", "ETB", "ETB", "ETB", "PMA", "TRA", "TRA", "PMA", "PER",
"KON", "PER", "BUM", "KON", "ETB", "ETB", "BUM", "TRA", "ETB",
"PMA", "HED", "MAT", "TRA", "BUM", "PMA", "BUM", "ETB", "TRA",
"TRA", "TRA", "PER", "EXP", "HED", "BUM", "EXP", "HED", "BUM",
"MAT", "DDR", "BUM", "MAT", "KON", "HED", "HED", "TRA", "BUM",
"PMA", "PMA", "PMA", "KON", "KON", "MAT", "ETB", "MAT", "TRA",
"MAT", "ETB", "ETB", "TRA", "MAT", "ETB", "TRA", "HED", "BUM",
"MAT", "TRA", "PMA", "BUM", "BUM", "EXP", "ETB", "EXP", "EXP",
"MAT", "TRA", "KON", "BUM", "BUM", "HED"), kclust = c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, 8, 8, 5, 2, 9, 3, 5, 3,
10, 3, 7, 4, 7, 1, 1, 5, 3, 1, 4, 4, 7, 1, 7, 8, 7, 4, 4, 1,
8, 4, 3, 1, 1, 4, 2, 3, 9, 9, 2, 8, 2, 3, 2, 5, 5, 8, 7, 7, 4,
1, 1, 1, 2, 5, 5, 2, 3, 4, 3, 7, 4, 1, 1, 7, 5, 1, 2, 9, 8, 5,
7, 2, 7, 1, 5, 5, 5, 3, 10, 9, 7, 10, 9, 7, 8, 6, 7, 8, 4, 9,
9, 5, 7, 2, 2, 2, 4, 4, 8, 1, 8, 5, 8, 1, 1, 5, 8, 1, 5, 9, 7,
8, 5, 2, 7, 7, 10, 1, 10, 10, 8, 5, 4, 7, 7, 9)), .Names = c("hhDomMil",
"kclust", "order"), row.names = c(NA, 200L), class = "data.frame")
我想创建一个像这样的堆积条形图。
唯一的问题是,我希望堆栈的顺序适合这个(ETB,PMA,PER,KON,TRA,DDR,BUM,MAT,HED,EXP) - 矩阵中的订单号我也有一些美学问题。我在这里搜索了一个解决方案,但没有一个订购建议对我有用......: - \
非常感谢你的帮助! 多米尼克
更新
以下是我用来绘制情节的代码:
mycols <- c('#FFFD00', '#97CB00', '#3168FF', '#FF0200', '#FB02FE', \
'#CCFCCC', '#FE9900', '#98CBF8', '#00CCFF', '#00FD03') # Set milieu colors
ggplot(MilDis) +
geom_bar(aes(kclust, fill=factor(hhDomMil), \
colour=mycols), position='fill', binwidth=1, colour='white') +
scale_fill_manual(values = mycols)
更新2:
我就是这样做的:
mycols <- c('#3168FF', '#00CCFF', '#98CBF8', '#CCFCCC', '#00FD03',\
'#97CB00', '#FFFD00', '#FE9900', '#FB02FE', '#FF0200') # Set milieu colors
ggplot(MilDis) +
geom_bar(aes(factor(kclust), fill=reorder(hhDomMil,order)),\
position='fill') +
scale_fill_manual(values = mycols)
结果如下:
谢谢大家的帮助!
答案 0 :(得分:12)
通过在将数据传递给ggplot()
之前正确格式化数据,可以轻松解决这个问题。关键是要明确设置hhDomMil
因子的级别。假设您的数据位于dat
:
dat <- transform(dat, hhDomMil = factor(hhDomMil,
levels = c("ETB", "PMA", "PER", "KON",
"TRA", "DDR", "BUM", "MAT",
"HED", "EXP")))
将hhDomMil
作为到位的内容修复dat
,和将级别设置为您想要的顺序:
> head(dat$hhDomMil)
[1] HED ETB HED ETB PER BUM
Levels: ETB PMA PER KON TRA DDR BUM MAT HED EXP
注意当R将hhDomMil
强制转换为因子时,正在讨价还价:
> head(factor(as.character(dat$hhDomMil)))
[1] HED ETB HED ETB PER BUM
Levels: BUM DDR ETB EXP HED KON MAT PER PMA TRA
默认设置是按字母顺序对级别进行排序,这就是为什么情节会在您显示时出现的原因。
我能给出的最佳建议是首先正确格式化您的数据,然后再尝试绘制它 - 不要依赖自动或即时转换来实现这一点;不可避免地,它不会是你想要的。
答案 1 :(得分:12)
我看到您的数据框中有一个order
列,我收集的是您的订单。因此你可以做到。
p0 = qplot(factor(kclust), fill = reorder(hhDomMil, order), position = 'fill',
data = df1)
以下是此代码中处理您的问题的元素
reorder
factor(kclust)
factor(kclust)
我记得你之前的一个问题,hhDomMil
对应不同的群体,我怀疑你的排序是在分组之后。在这种情况下,您可能希望使用该信息来选择调色板,以便更轻松地遵循图形。这是一种方法。
mycols = c(brewer.pal(3, 'Oranges'), brewer.pal(3, 'Greens'),
brewer.pal(2, 'Blues'), brewer.pal(2, 'PuRd'))
p0 + scale_fill_manual(values = mycols)
答案 2 :(得分:7)
如果你将hhDomMil重新定义为这样一个因素:
o<-c("ETB" "PMA" "PER" "KON" "TRA" "DDR" "BUM" "MAT" "HED" "EXP")
d$hh<-factor(d$hhDomMil,levels=o)
那么你的情节将按你喜欢的顺序排列:
ggplot(d,(aes(x=kclust, fill=hh))) +geom_bar(position="fill")