[英]visualize a list of colors/palette in R
我有以下帶有 rgb 值的 data.frame。 因此,每一行表示一種顏色。
> ddf
r g b
1 0.374 0.183 0.528
2 0.374 0.905 0.337
3 0.051 0.662 0.028
4 0.096 0.706 0.898
5 0.876 0.461 0.628
6 0.415 0.845 0.286
7 0.596 0.070 0.523
8 0.724 0.101 0.673
9 0.847 0.434 0.937
10 0.588 0.885 0.604
11 0.481 0.366 0.337
12 0.142 0.075 0.276
13 0.819 0.737 0.658
14 0.910 0.722 0.979
15 0.969 0.012 0.451
16 0.887 0.536 0.123
17 0.432 0.967 0.446
18 0.927 0.125 0.332
19 0.381 0.646 0.656
20 0.040 0.898 0.798
>
> dput(ddf)
structure(list(r = c(0.374, 0.374, 0.051, 0.096, 0.876, 0.415,
0.596, 0.724, 0.847, 0.588, 0.481, 0.142, 0.819, 0.91, 0.969,
0.887, 0.432, 0.927, 0.381, 0.04), g = c(0.183, 0.905, 0.662,
0.706, 0.461, 0.845, 0.07, 0.101, 0.434, 0.885, 0.366, 0.075,
0.737, 0.722, 0.012, 0.536, 0.967, 0.125, 0.646, 0.898), b = c(0.528,
0.337, 0.028, 0.898, 0.628, 0.286, 0.523, 0.673, 0.937, 0.604,
0.337, 0.276, 0.658, 0.979, 0.451, 0.123, 0.446, 0.332, 0.656,
0.798)), .Names = c("r", "g", "b"), class = "data.frame", row.names = c(NA,
-20L))
我如何可視化這些顏色? 這可以是顏色條,也可以是調色板或餅圖。 我嘗試使用以下方法,但無法將其放入我的數據中:
pie(rep(1,20), col=rainbow(20))
如果您通過rgb()
轉換顏色,則image()
在這里可以很好地工作
image(1:nrow(ddf), 1, as.matrix(1:nrow(ddf)),
col=rgb(ddf$r, ddf$g, ddf$b),
xlab="", ylab = "", xaxt = "n", yaxt = "n", bty = "n")
作為使用image
的解決方案的替代方案,您還可以使用polygon
並創建一個非常相似的圖:
plot(NA, xlim=c(0, nrow(ddf)), ylim=c(0,1))
for (i in 1:nrow(ddf)) {
row <- ddf[i,]
color <- rgb(red=row$r, green=row$g, blue=row$b)
polygon(x=c(i-1, i, i, i-1), y=c(0, 0, 1, 1), col = color)
}
你也可以使用ggplot2
:
library(ggplot2)
qplot(x=1:nrow(ddf), y = 1, fill=factor(1:nrow(ddf)), geom="tile") +
scale_fill_manual(values = rgb(ddf$r, ddf$g, ddf$b)) +
theme_void()+
theme(legend.position="none")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.