ggplot2:在前面加一行,但保存颜色

[英]ggplot2: Bring one line to the front, but save the colors


Consider the following code:

请考虑以下代码:

library(ggplot2)
foo <- data.frame(x=1:10,A=1:10,B=10:1)
ggplot(melt(foo,id.vars="x"),aes(x,value,color=variable))+geom_line(size=5)

enter image description here

I want to bring the red line (A) to the front, on top of B (see the cross point), while the colors and the order they appear in the legend do not change. Is there any way?

我想将红线(A)置于B的顶部(参见交叉点),而颜色和它们在图例中出现的顺序不会改变。有什么办法吗?

4 个解决方案

#1


14  

Try this,

尝试这个,

last_plot() + aes(group=rev(variable))

#2


4  

Replotting the red line using a subsetted dataframe does the trick.

使用子集化数据帧重新绘制红线就可以了。

library(ggplot2)
foo <- data.frame(x=1:10,A=1:10,B=10:1)
require(reshape2)
fooM <- melt(foo,id.vars="x")
p<-ggplot()
p<-p+geom_line(data=fooM[fooM$variable!="A",],aes(x,value,color=variable),size=5)
p<-p+geom_line(data=fooM[fooM$variable=="A",],aes(x,value,color=variable),size=5)
p

EDIT: Note that ggplot applies layers sequentially on top of each other - this can be best leveraged when constructing the plot line by line.

编辑:请注意,ggplot将图层顺序地应用于彼此之上 - 在逐行构造图表时可以最好地利用这些图层。

EDIT2: @tonytonov is right that one may want to avoid plotting identical things twice. Modified my answer to plot everything but A the first time, then only A. Result remains the same and is now also compatible with transparency or big data ;)

编辑2:@tonytonov是对的,人们可能想避免两次绘制相同的东西。修改了我的答案来绘制除A之外的所有内容,第一次,然后只有A.结果保持不变,现在也兼容透明度或大数据;)

#3


2  

The solution with replotting is fine unless you have reasons to avoid it. I can think of at least two: using alpha (transparency) or performance issues (you need to do it in one run, big data).

重新绘制的解决方案很好,除非您有理由避免它。我至少可以想到两个:使用alpha(透明度)或性能问题(你需要在一次运行中执行它,大数据)。

Here's what I propose:

这是我的建议:

require(scales)
# give the desired order here, I just use reverse
# separate function since we apply it over both levels & colors
shuffle <- function(x) rev(x)
foo <- data.frame(x=1:10, A=1:10, B=10:1, C=2.5, D=7.5)
melt_foo <- melt(foo, id.vars="x") 
# original plot
ggplot(melt_foo, aes(x, value, color=variable)) + 
  geom_line(size=5)

enter image description here

orig_order <- levels(melt_foo$variable)
new_order <- shuffle(orig_order)
# grabbing default colours
orig_colors <- hue_pal()(length(new_order))
new_colors <- shuffle(orig_colors)
melt_foo$variable <- factor(melt_foo$variable, levels=new_order)
# levels _and_ colours reversed, legend appearance stays the same
ggplot(melt_foo, aes(x, value, color=variable)) + 
  geom_line(size=5) + 
  scale_colour_manual(values=new_colors, labels=orig_order, breaks=orig_order) 

enter image description here

#4


0  

The order of your A,B inside data.frame(x=1:10,A=1:10,B=10:1) is causing this layering issue because it is drawing A (1:10) first.

数据框内的A,B的顺序(x = 1:10,A = 1:10,B = 10:1)导致了这种分层问题,因为它首先绘制A(1:10)。

If you use data.frame(x=1:10,A=10:1,B=1:10) is will put the ascending line on top (since B is drawn last).

如果你使用data.frame(x = 1:10,A = 10:1,B = 1:10),则将升序线放在最上面(因为B是最后绘制的)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/01/14/fd0722ea6c81338f7b6c8ac839c91330.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号