防止ggplot2图例重新排序标签 [英] Prevent ggplot2 legend from reordering labels

查看:1414
本文介绍了防止ggplot2图例重新排序标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于这个情节来说,我们有一个列(类别),它是一个特定的订单(它应该在图例中拼写order我为每个图层使用了不同的数据子集。将数据合并为图例时,该因素的顺序会发生变化。



关于如何防止重新排序的任何想法?

 库(ggplot2)
库(dplyr)
库(tidyr)

#设置一些数据
set。 seed(12345)
count = 5
data = data.frame(
location = LETTERS [1:count],
o = runif(count),r = runif(count) ,d = runif(count),e = runif(count),R = runif(count)

data = data%>%
arrange(o)%>%
mutate(rank = 1:count)%>%
gather('category','value',o:R)

#安排类别
#注意这里的命令
data $ category = factor(data $ category,levels = c('o','r','d','e','R'))

#获取子集
subsetO = data%>%filter(category =='o')
subsetNotO = data%>%filter(category!='o')

#确认子集具有与原始
all相同的因子水平(水平(subsetO $ category)= = data(数据$ category))

ggplot(data = data,aes(x = location,fill = category))+
geom_bar(data = subsetO,aes(y = value) ,stat ='identity',position ='stack')+
geom_bar(data = subsetNotO,aes(y = -value),stat ='identity',position ='stack')



编辑:我已经在重新考虑了列(这是许多假设重复的解决方案)。 为了让您的问题得到答案,您可以使用 scale_fill_discrete 来单独订购颜色。

  ggplot(data = data,aes(x = location,fill = category))+ 
geom_bar(data = subsetO,aes(y = value),stat ='identity ',position ='stack')+
geom_bar(data = subsetNotO,aes(y = -va lue),stat ='identity',position ='stack')+
scale_fill_discrete(breaks = data $ category)

很多这类问题都可以通过阅读以下网站来回答食谱R - 图


I have a column ("category") that is factored with a specific order (it should spell "order" in the legend).

For the plot, I'm using a different subset of the data for each layer. When merging the data back together for the legend the factor's order changes.

Any ideas on how to prevent this reordering?

library(ggplot2)
library(dplyr)
library(tidyr)

# make some data
set.seed(12345)
count = 5
data = data.frame(
  location = LETTERS[1:count],
  o=runif(count), r=runif(count), d=runif(count), e=runif(count), R=runif(count)
)
data = data %>%
  arrange(o) %>%
  mutate(rank = 1:count) %>%
  gather('category', 'value', o:R)

# arrange the factor for category
# NOTICE THE ORDER HERE
data$category = factor(data$category, levels=c('o', 'r', 'd', 'e', 'R'))

# get subsets
subsetO = data %>% filter(category=='o')
subsetNotO = data %>% filter(category!='o')

# confirm that the subset has the same factor levels as the original
all(levels(subsetO$category) == levels(data$category))

ggplot(data = data, aes(x=location, fill=category)) +
  geom_bar(data = subsetO, aes(y=value), stat='identity', position='stack') +
  geom_bar(data = subsetNotO, aes(y=-value), stat='identity', position='stack')

Edit: I have in already re-factored the column (which is the solution in many of the supposed duplicates)

解决方案

To also provide an answer to your question, you can order the colours individually with a scale_fill_discrete.

ggplot(data = data, aes(x=location, fill=category)) +
  geom_bar(data = subsetO, aes(y=value), stat='identity', position='stack') +
  geom_bar(data = subsetNotO, aes(y=-value), stat='identity', position='stack') + 
  scale_fill_discrete(breaks = data$category)

A lot of these kind of questions can be answered by reading the following website Cookbook for R - Graphs

这篇关于防止ggplot2图例重新排序标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆