在剧情和图例之间同步颜色 [英] Synchronise colors between plot and legend

查看:89
本文介绍了在剧情和图例之间同步颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此命令可以从igraph生成网络图:

I have this command to produce a network graph from igraph:

plot(ig, layout=layout.fruchterman.reingold(ig),vertex.label=NA,vertex.label.color="black",vertex.frame.color="black",edge.color="black", vertex.color=rainbow(9)[data2$Group], main="MB Network")

data2是另一个对象,其中包含有关igraph对象ig中的顶点分组的信息.我想按顶点分组为顶点着色,然后为图例的组颜色准则添加图例,所以我使用了以下命令:

The data2 is another object containing information on groupings of the vertices in the igraph object ig. I wanted to color the vertices by their groupings and then put a legend for group color guidelines so I used the command:

legend("topleft",legend=unique(data2$Group),col=rainbow(9)[data2$Group])

但是,图例中使用的颜色与绘图的颜色不匹配.我该如何纠正?

However, the colors used in the legend does not match with the colors of the plot. How should I correct this?

推荐答案

我同意@Resh的观点,即RColorBrewer提供了出色的调色板!

I agree with @Resh that RColorBrewer provides excellent color palettes !

这是著名的三次方图上的一个例子

Here is an example on a famous cubical graph

require(igraph)
require(RColorBrewer)

我将首先在外部定义调色板,我在调色板中选择了四种颜色,称为强调".然后,根据称为组"的因子,将每种颜色与八个顶点相关联.由于颜色是绿色,紫色,橙色和黄色,因此特意将因子级别设置为G,P,O和Y.这将帮助我们知道图形和图例中的颜色是否正确.

I would externally define the palette first, I chose four colors in the palette called "Accent". Each one of the color is then associated to the eight vertices according to a factor called Group. The levels of the factor are set on purpose to G, P, O and Y because the colors are green, purple, orange and yellow. This will help us know if the colors are right in both the graph and the legend.

pal <- brewer.pal(4,"Accent")
g <- make_graph("Cubical")
Group <- gl(4, 2, labels = c("G","P","O","Y"))
vertex.col <- pal[Group]

plot(g, 
     layout=layout.fruchterman.reingold(g),
     vertex.label.color="black",
     vertex.frame.color="black",
     edge.color="black", 
     vertex.color=vertex.col, 
     main="Cubical")

legend("topleft",bty = "n",
       legend=levels(Group),
       fill=pal, border=NA)

结果……

这篇关于在剧情和图例之间同步颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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