ggplot2:按字母顺序在图例中排序,而不遵循数据集中的出现顺序 [英] ggplot2: Order in legend alphabetically instead of following the order of appearance in the dataset

查看:724
本文介绍了ggplot2:按字母顺序在图例中排序,而不遵循数据集中的出现顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从AESRD 2013 - SCO Bitumen - 7y.csv 此文件夹中制作一个ggplot2线图. .该文件是根据我的意愿在网站上自动创建的.这些是显示一些生产值的几个时间序列,每个值均根据"Compilation"列进行命名.因此,我根据编译"进行了分组.

I am producing a ggplot2 line plot from AESRD 2013 - SCO Bitumen - 7y.csv in this folder. The file is automatically created on a website according to my wishes. These are several time series showing some production values, each named according to the column "Compilation". So I grouped according to "Compilation".

在同一文件夹(请参见上文)中可用的文件plotter.r中查看我的代码摘录.

See this excerpt of my code in the file plotter.r available in the same folder (see above).

# "dt" is the dataframe derived from the csv file. 
# "thinned" is some vector of x-values that tells where to draw the special symbols.

p = ggplot(dt, aes(Date, Value, colour= Compilation, group = Compilation, size = plotParameter), guide=FALSE)
p = p + geom_point(data=dt[thinned,],aes(as.Date(Date), Value, colour= Compilation, shape = Compilation), size = 5)
p = p + scale_shape_manual(values = seq(0,20))
p = p + geom_line(guide = FALSE)
p = p + scale_colour_manual(values=cbPalette) #cbPalette is already defined
p = p + scale_size(range=c(0.5, 2), guide=FALSE)
p = p + scale_y_continuous(labels = comma)
p = p + ylab("Barrels per day") + xlab("")
p = p + theme(legend.text = element_text(size = 8, hjust = 5, vjust= -5)) 
plot(p)

这是令人讨厌的事情:图例对我的编辑进行了重新排序 按字母顺序

Here comes the nasty thing: The legend reorders my compilations alphabetically!

我特意设计了我的csv文件,以便每个编译都以一定的逻辑顺序显示(首先是最重要的系列,然后是某些性能参数的顺序).因此,图例的正确顺序将只是根据unique(dt$Compilation).

I have purposely designed my csv-file so that each compilation shows up in a certain logical order (the most important series first, then following in order of some performance parameter). So the right order of the legend would simply be according to unique(dt$Compilation).

到目前为止,我的步骤是将列Order引入csv文件并进行尝试(不成功),并以各种方式更改我的代码.没有成功.

My steps until now have been to introduce the column Order into the csv-file and experiment with that (unsuccessfully), and to change my code in all kinds of ways. With no success.

当然,我已经Google搜索并检查了Stackoverflow上大多数可用的线程.我遇到了因式分解和重新排序,但是除了它们在数据集中出现的顺序之外,我的编译没有逻辑"顺序. *叹气*

Of course, I have googled and checked most available threads on Stackoverflow. I have encountered factorization and reordering, but there is no "logical" order for my compilations except for the order they appear in the dataset. *Sigh*

任何人都可以在哪里上指出我要插入什么吗?

Can anyone point me on where to insert what?

(奖励点:如何消除符号图例中的那些水平线?)

(Bonus point: How do I get rid of those horizontal lines in the symbol legend?)

推荐答案

在两个比例尺(scale_color_manualscale_shape_manual)中均应用breaks.如果我们只做一个,它们将不匹配,并且ggplot会将它们分成两个图例,而不是合并它们

apply breaks in both scales(scale_color_manual and scale_shape_manual) . If we did just one, they wouldn't match, and ggplot would split them into two legends, rather than merging them

一个这样的例子是:

> library(ggplot2)
> ggplot(mtcars, aes(wt, mpg, shape=factor(cyl))) + geom_point() + theme_bw()
> library(ggplot2)
> bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
> bp

bp + scale_fill_discrete(breaks=c("trt1","ctrl","trt2"))

撤消

bp + scale_fill_discrete(breaks = rev(levels(PlantGrowth$group)))

也尝试

bp + scale_fill_discrete(breaks = unique(levels(PlantGrowth$group)))

这篇关于ggplot2:按字母顺序在图例中排序,而不遵循数据集中的出现顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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