我有一个数字列表:
vector = c(0.1, 0.25, 0.1, 0.05, 0.05, 0.15, 0.15, 0.15)
我希望将此列表可视化如下。我想绘制一个带有8个内部小方框的矩形框,每个框填充一个颜色,其RGB值与列表中的相关数值相关联。例如,R = 0,G = 0,B = value_from_list。
答案 0 :(得分:2)
library(ggplot2)
myVector <- c(0.1, 0.25, 0.1, 0.05, 0.05, 0.15, 0.15, 0.15)
ggplot(data.frame(x = 1:8, y = 0), aes(x, y)) +
geom_tile(fill = rgb(0, 0, myVector)) +
coord_fixed() +
theme_void()
答案 1 :(得分:-2)
#DATA
mylist = c(0.1, 0.25, 0.1, 0.05, 0.05, 0.15, 0.15, 0.15)
L = length(mylist)
#create empty plot
plot(c(1, L+1, L+1, 1), y = c(0, 0, 1, 1), type = "l", ann = FALSE, axes = FALSE, asp = 1)
#draw polygons
sapply(1:L, function(i)
polygon(x = c(i, i+1, i+1, i),
y = c(0, 0, 1, 1),
col = rgb(red = 0, green = 0, blue = mylist[i])))