在Likert地块中用R重新排序组 [英] Reordering groups in Likert plots with R
问题描述
考虑响应表,称为项目
- 这里, A
, B
等是列名称,而不是数据的一部分:
ABCD
5 4 5 4
3 3 3 4
2 2 2 2
2 2 2 3
5 3 6 7
3 3 5 4
以下代码:
选择= c(非常低,低,相当低,既不低也不高,相当高, 高,非常高)
for(i in 1:ncol(items)){
items [,i] = factor(items [,i],levels = 1:7,labels = options,ordered = TRUE)
}
现在,我们将其转换为 likert
数据并绘制它,它使用 ggplot
来覆盖内置的绘图函数:
plot(likert(items),ordered = FALSE)
这给了我:
酷。所有订购。但是 A
, B
等作为条件描述符是没有意义的,我想重写它们:
names(items)= c(LT,ST,SemTag,SemTagContext)
这给了我错误的订单:
< img src =https://i.stack.imgur.com/Z1LHq.pngalt =>
看看 ST
是第一个,尽管它是 B
的名字?我的订单已变更为 B
, D
, C
, A
。
如何保存订单并返回 D
, C
, B
, A $ c $使用我的新组名:
SemTagContext
, SemTag
, ST
,
LT
?
注意:上面的数据表缩短了。图中的条宽并不反映这个简短的数据示例,而是我所具有的完整数据集。
/ p>
#附加需求
库(ggplot2)
库(reshape2)
库(RColorBrewer)
创建汇总表
table_summary = likert(物品)
重塑结果
结果=融化(table_summary $ results,id.vars =' Item')
#重新排序结果
结果$ Item = factor(results $ item,levels = c(LT,ST,SemTag,SemTagContext))
#一些默认
ymin = 0
text.size = 3
ggplot(aes(y = value,x = Item,group = Item))+
geom_bar(stat ='identity',aes(fill = variable))+
ylim(c(-5,105))+
coord_flip()+
scale_fill_manual ('Response',values = brewer.pal(7,RdYlGn),
breaks = levels(results $ variable),
labels = levels(results $ variable))+
geom_text (数据= table_summary $ summary,y = ymin,aes(x = Item,
label = paste(round(low),'%',sep ='')),
size = text.size,hjust = 1 )+
geom_text(data = table_summary $ summary,y = 100,aes(x = Item,
label = paste(round(high),'%',sep ='')),
size = text.size,hjust = - 。2)+
ylab('Percentage')+ xlab('')
这会产生正确的顺序:
I'm using the jbryer/likert
package to plot Likert data.
Consider the response table, called items
—here, A
, B
, and so on are the column names, not part of the data:
A B C D
5 4 5 4
3 3 3 4
2 2 2 2
2 2 2 3
5 3 6 7
3 3 5 4
And the following code:
choices = c("Very low", "Low", "Rather low", "Neither low nor high", "Rather high", "High", "Very high")
for(i in 1:ncol(items)) {
items[,i] = factor(items[,i], levels=1:7, labels=choices, ordered=TRUE)
}
Now, we transform this into the likert
data and plot it, which overrides the built-in plot function with one that uses ggplot
:
plot(likert(items), ordered=FALSE)
This gives me:
Cool. All ordered. But A
, B
, etc. are not meaningful as descriptors for the conditions, and I'd like to override them:
names(items) = c("LT", "ST", "SemTag", "SemTagContext")
Which gives me the wrong order:
See how ST
comes first, although it's the name for B
? My ordering got changed to B
, D
, C
, A
.
How can I make it preserve the order and return the bars in D
, C
, B
, A
—or, with my new group names: SemTagContext
, SemTag
, ST
, LT
?
Note: The data table above is shortened. The bar widths in the plot don't reflect this short data example, but the full dataset I have. The problem is the same though.
I decided to re-implement this myself, as jon suggested:
# additional requirements
library(ggplot2)
library(reshape2)
library(RColorBrewer)
# create summary table
table_summary = likert(items)
# reshape results
results = melt(table_summary$results, id.vars='Item')
# reorder results
results$Item = factor(results$Item, levels=c("LT", "ST", "SemTag", "SemTagContext"))
# some defaults
ymin = 0
text.size = 3
ggplot(results, aes(y=value, x=Item, group=Item)) +
geom_bar(stat='identity', aes(fill=variable)) +
ylim(c(-5,105)) +
coord_flip() +
scale_fill_manual('Response', values=brewer.pal(7, "RdYlGn"),
breaks=levels(results$variable),
labels=levels(results$variable)) +
geom_text(data=table_summary$summary, y=ymin, aes(x=Item,
label=paste(round(low), '%', sep='')),
size=text.size, hjust=1) +
geom_text(data=table_summary$summary, y=100, aes(x=Item,
label=paste(round(high), '%', sep='')),
size=text.size, hjust=-.2) +
ylab('Percentage') + xlab('')
This produces the correct order:
这篇关于在Likert地块中用R重新排序组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!