如何使用三列制作彩条?

时间:2017-05-04 08:40:38

标签: r plot colorbar

我有一张如下表:

ID    type  group
A3EP    1    M
A3MA    2    M
A459    3    M
A3I1    5    M
A9D2    7    M
A3M9    4    M
A7XP    6    M
A4ZP    8    M

我想制作一个如下颜色条:红色代表" group"以下每种颜色代表"类型"在下面我想要" ID"名。

enter image description here

任何人都可以告诉我该怎么做?谢谢。

1 个答案:

答案 0 :(得分:2)

mypalette <- rainbow(8)
barplot(rep(0.5,8), width=1, space=0, col=mypalette, axes=F)
text(df$type-.5, .2, df$ID,  srt=90)

rect(0, .4, 8, .5, col="red")
text(4, .45, "M")

enter image description here

输入数据:

df <- structure(list(ID = structure(c(1L, 4L, 5L, 2L, 8L, 3L, 7L, 6L
), .Label = c("A3EP", "A3I1", "A3M9", "A3MA", "A459", "A4ZP", 
"A7XP", "A9D2"), class = "factor"), type = 1:8, group = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), class = "factor", .Label = "M")), .Names = 
c("ID", 
"type", "group"), row.names = c(NA, -8L), class = "data.frame")