r语言 - 如何从最好到最差对密钥进行排序?



我创建了一个条形图,每个变量最多有四个数据点。我已经成功地策划了它。我目前遇到的唯一问题是密钥不符合我想要的顺序。理想情况下,我想要从最好到最差的键,或者在这种情况下"优秀"到"不太好"。

我需要更改代码的哪一部分才能使顺序从最佳变为最差?

df <- read.csv("//ecfle35/STAFF-HOME$/MaxEmery/open event feedback/October/Q3.csv")
df %>%
#First the dataset needs to be long not wide
gather(review,
count,
Excellent:Not.so.good,
factor_key = T) %>%
#Lets get ride of N/A
filter(count != 'N/A') %>%
#convert count from string to number
#Remove the annoying full stop in the middle of text
mutate(count = as.integer(count),
review = gsub('\.', ' ', review)) %>%
ggplot(aes(
x = Faculty,
y = count,
fill = review
)) +
geom_bar(position = 'dodge',
stat = 'identity') +
scale_y_continuous(breaks = seq(0, 22, by = 2)) +
labs(title = 'Teaching Staff Ratings',
x = 'Faculty',
y = 'Count') +
theme(axis.text.x = element_text(angle = 90))

下面是它目前的外观图片 -

我的情节图形

当您在不指定水平的情况下转换为因子时,将根据字母顺序确定水平。在管道中添加一个mutate()以重新调平review列,然后再将其发送到ggplot()

最新更新