ggplot2:在循环中添加行并保留颜色映射 [英] ggplot2: adding lines in a loop and retaining colour mappings
问题描述
$ b $ h1> EX1
economics2 < - economics
p>
经济学2 $失业 - - 经济学$失业+ 1000
经济学3 - - 经济学
经济学3 $失业 - - 经济学$失业+ 2000
经济学4 < - 经济学
经济学4 $失业 - 经济学$失业+ 3000
b < - ggplot()+
geom_line(aes(x =日期,y =失业,color = as.character(1) =经济学2)+
geom_line(aes(x =日期,y =失业,color = as.character(2)),data = economics3)+
geom_line(aes(x = date,y = (b)
EX2
#经济学2,经济学3,经济学4从EX1中重用。
b < - ggplot()
econ< - list(economics2,economics3,economics4)
for(i in 1:3){
b < - b + geom_line aes(x =日期,y =失业,color = as.character(i)),data = econ [[i]]
}
print(b)
$ bimg src =https://i.stack.imgur.com/Y7XS5.pngalt =EX2>
解决方案 div>这不是使用ggplot的好方法。试试这种方式:
econ < - list(e1 = economics2,e2 = economics3,e3 = economics4)
df < - cbind(cat = rep(名称(econ),sapply(econ,nrow)),do.call(rbind,econ))
ggplot(df,aes(date,unemploy,color = cat) )+ geom_line()
这将您的三个版本的
economics
放入一个长格式的数据框中(所有数据在1列中,第二列,在这个例子中cat
,标识源代码)。一旦你这样做了,ggplot
会处理其他事情。没有循环。
注释中指出,循环失败的具体原因是使用
aes(...)
将表达式存储在ggplot对象中,并且在调用print(...)
时计算该表达式。此时i
为3。
请注意,这不适用于
数据= ...
参数,所以你可以这样做:
b = ggplot( )
for(i in 1:3){
b <-b + geom_line(aes(x = date,y = unemploy,color = cat),
data = cbind(cat = as.character(i),econ [[i]]))
}
print(b)
但是,这仍然是使用ggplot的错误方法。
When running the following two pieces of code, I unexpectedly get different results. I need to add lines in a loop as in EX2, but all lines end up having the same colour. Why is this?
EX1
economics2 <- economics economics2$unemploy <- economics$unemploy + 1000 economics3 <- economics economics3$unemploy <- economics$unemploy + 2000 economics4 <- economics economics4$unemploy <- economics$unemploy + 3000 b <- ggplot() + geom_line(aes(x = date, y = unemploy, colour = as.character(1)), data=economics2) + geom_line(aes(x = date, y = unemploy, colour = as.character(2)), data=economics3) + geom_line(aes(x = date, y = unemploy, colour = as.character(3)), data=economics4) print(b)
EX2
#economics2, economics3, economics4 are reused from EX1. b <- ggplot() econ <- list(economics2, economics3, economics4) for(i in 1:3){ b <- b + geom_line(aes(x = date, y = unemploy, colour = as.character(i)), data=econ[[i]]) } print(b)
解决方案This is not a good way to use ggplot. Try this way:
econ <- list(e1=economics2, e2=economics3, e3=economics4) df <- cbind(cat=rep(names(econ),sapply(econ,nrow)),do.call(rbind,econ)) ggplot(df, aes(date,unemploy, color=cat)) + geom_line()
This puts your three versions of
economics
into a single data.frame, in long format (all the data in 1 column, with a second column,cat
in this example, identifying the source). Once you've done that,ggplot
takes care of everything else. No loops.The specific reason your loop failed, as pointed out in the comment, is that using
aes(...)
stores the expression in the ggplot object, and that expression is evaluated when you callprint(...)
. At that pointi
is 3.Note that this does not apply to the
data=...
argument, so you could have done something like this:b=ggplot() for(i in 1:3){ b <- b + geom_line(aes(x=date,y=unemploy,colour=cat), data=cbind(cat=as.character(i),econ[[i]])) } print(b)
But, this is still the wrong way to use ggplot.
这篇关于ggplot2:在循环中添加行并保留颜色映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文