2014-01-28 133 views
5

我正在从AESRD 2013 - SCO Bitumen - 7y.csvin this folder生成ggplot2线图。该文件根据我的意愿在网站上自动创建。这些是几个时间序列,显示了一些生产值,每个值根据“汇编”栏命名。所以我按照“汇编”分组。ggplot2:按照字母顺序排列而不是按照数据集中外观的顺序排列

看到我的代码在文件plotter.r的摘录可在同一文件夹中(见上文)。

# "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) 

来了讨厌的事:图例重新排序我的合辑字母enter image description here

我特意设计我的CSV文件,使每个编译在一定的逻辑顺序显示出来(最重要的系列,然后再在一些性能参数的顺序如下)。所以传说的正确顺序将根据unique(dt$Compilation)

我到现在为止的步骤是将列Order引入到csv文件中,并试验(不成功),并以各种方式更改我的代码。没有成功。

当然,我已经搜索并检查了Stackoverflow上的大部分可用线程。我遇到了因式分解和重新排序,但我的汇编没有“逻辑”顺序,除了,它们出现在数据集中的顺序。 *叹气*

任何人都可以点我在哪里插入什么

(奖励点:如何摆脱象征传奇的水平线?)

+1

申请'breaks'两个'scales'(scale_color_manual和scale_shape_manual)。如果我们只做了一个,它们就不会匹配,ggplot会将它们分成两个传说,而不是合并它们。 – RUser

+0

你解决了!如果你把它作为答案(而不是评论)发布,我可以用一些声誉来奖励你! – fridde

+0

刚刚在下面添加了答案 - 您可以继续投票并将其批准为答案 – RUser

回答

4

适用于breaks这两个尺度(scale_color_manualscale_shape_manual)。如果我们这样做只有一个,他们将不匹配,并且ggplot将它们分成两个传说,而不是将它们合并

就是这样一个例子:

> 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 

enter image description here

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

enter image description here

要反转

也尽量

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

也会探索用于图例主题的'bp + opts(legend.key =)' – RUser

+0

有关'group'中项目出现的顺序,此处的最后一行应为:'bp + scale_fill_discrete(breaks = unique(PlantGrowth $ group))'? –

2

为了解决顺序:你可能想通过调用绘图之前解决的dt$Compilation因子水平某物像dt <- transform(dt, Compilation=factor(Compilation, levels=unique(Compilation)))

说到水平线:你想要p = p + guides(size=FALSE)

相关问题