使用嵌套的 ifelse 绘制图例
Plot legend using nested ifelse
问题来了,我想只用R的基本函数画图。这个想法是制作一个散点图,其中每个点代表一个国家。该数据集经常更新,并非所有国家/地区每次都存在。
我想定义每个国家的颜色。到目前为止一切顺利,我可以使用一些嵌套的 ifelse 语句来做到这一点。然而,如果你 运行 下面的例子几次你会发现颜色并不总是与定义的一致。我试图解决它,但我就是无法修复它。我确定这很愚蠢,但目前我无法解决。
dat <- data.frame(Country=sample(c("Scotland","Sweeden","Ireland","Netherlands"),
10,replace=T),x =sample( 1:10,10),y= sample( 1:10,10))
plot(dat$x,dat$y,pch=20,cex=2,col=ifelse(dat$Country=="Scotland","darkblue",
ifelse(dat$Country=="Netherlands","orange",ifelse(dat$Country=="Ireland","green",
ifelse(dat$Country=="France","red",ifelse(dat$Country=="Sweeden","yellow","Black"))))))
legend("bottomleft",legend=unique(dat$Country),col=ifelse(dat$Country=="Scotland",
"darkblue",ifelse(dat$Country=="Netherlands","orange",ifelse(dat$Country=="Ireland",
"green",ifelse(dat$Country=="France","red",ifelse(dat$Country=="Sweeden","yellow",
"Black"))))),pch=20,cex=1, bty = "n")
在此先感谢您的帮助。
帕特劳
通过将颜色添加到不同的 data.frame,您可以轻松访问它们而无需 if else:
dat <- data.frame(Country=sample(c("Scotland","Sweeden","Ireland","Netherlands"),
10,replace=T),x =sample( 1:10,10),y= sample( 1:10,10))
colorFrame <- data.frame(Country=c("Scotland","Sweeden","Ireland","Netherlands","France"),
Color = c("darkblue","yellow","green","orange","red"))
dat <- merge(dat,colorFrame)
plot(dat$x,dat$y,pch=20,cex=2,col=as.vector(dat$Color))
legend("bottomleft",legend=colorFrame[which(colorFrame$Country %in% unique(dat$Country)),]$Country,col=as.vector(colorFrame[which(colorFrame$Country %in% unique(dat$Country)),]$Color),pch=20,cex=1, bty = "n")
问题来了,我想只用R的基本函数画图。这个想法是制作一个散点图,其中每个点代表一个国家。该数据集经常更新,并非所有国家/地区每次都存在。
我想定义每个国家的颜色。到目前为止一切顺利,我可以使用一些嵌套的 ifelse 语句来做到这一点。然而,如果你 运行 下面的例子几次你会发现颜色并不总是与定义的一致。我试图解决它,但我就是无法修复它。我确定这很愚蠢,但目前我无法解决。
dat <- data.frame(Country=sample(c("Scotland","Sweeden","Ireland","Netherlands"),
10,replace=T),x =sample( 1:10,10),y= sample( 1:10,10))
plot(dat$x,dat$y,pch=20,cex=2,col=ifelse(dat$Country=="Scotland","darkblue",
ifelse(dat$Country=="Netherlands","orange",ifelse(dat$Country=="Ireland","green",
ifelse(dat$Country=="France","red",ifelse(dat$Country=="Sweeden","yellow","Black"))))))
legend("bottomleft",legend=unique(dat$Country),col=ifelse(dat$Country=="Scotland",
"darkblue",ifelse(dat$Country=="Netherlands","orange",ifelse(dat$Country=="Ireland",
"green",ifelse(dat$Country=="France","red",ifelse(dat$Country=="Sweeden","yellow",
"Black"))))),pch=20,cex=1, bty = "n")
在此先感谢您的帮助。
帕特劳
通过将颜色添加到不同的 data.frame,您可以轻松访问它们而无需 if else:
dat <- data.frame(Country=sample(c("Scotland","Sweeden","Ireland","Netherlands"),
10,replace=T),x =sample( 1:10,10),y= sample( 1:10,10))
colorFrame <- data.frame(Country=c("Scotland","Sweeden","Ireland","Netherlands","France"),
Color = c("darkblue","yellow","green","orange","red"))
dat <- merge(dat,colorFrame)
plot(dat$x,dat$y,pch=20,cex=2,col=as.vector(dat$Color))
legend("bottomleft",legend=colorFrame[which(colorFrame$Country %in% unique(dat$Country)),]$Country,col=as.vector(colorFrame[which(colorFrame$Country %in% unique(dat$Country)),]$Color),pch=20,cex=1, bty = "n")