鉴于离散值的数据帧的离散值,情节不同颜色的
d=data.frame(id=1:6, a=c(1,1,1,0,0,0), b=c(0,0,0,1,1,1), c=c(10,20,30,30,10,20))
我要让喜欢
不过,我想使不同颜色的每一层的阴谋,对于“a”说红色和绿色,对“b”说黄色/蓝色。
鉴于离散值的数据帧的离散值,情节不同颜色的
d=data.frame(id=1:6, a=c(1,1,1,0,0,0), b=c(0,0,0,1,1,1), c=c(10,20,30,30,10,20))
我要让喜欢
不过,我想使不同颜色的每一层的阴谋,对于“a”说红色和绿色,对“b”说黄色/蓝色。
首先,我们使用reshape2
从广从数据到长。然后为了得到离散值,我们使用as.factor(value)
,最后我们用scale_fill_manual
来分配我们需要的5种不同的颜色。在geom_tile
中,我们指定了瓦片边界的颜色。
library(reshape2)
library(ggplot2)
df <- data.frame(id=1:6, a=c(1,1,1,0,0,0), b=c(0,0,0,1,1,1), c=c(10,20,30,30,10,20))
df <- melt(df, id.vars=c("id"))
ggplot(df, aes(id, variable, fill = as.factor(value))) + geom_tile(colour = "white") +
scale_fill_manual(values = c("lightblue", "steelblue2", "steelblue3", "steelblue4", "darkblue"), name = "Values")+
scale_x_discrete(limits = 1:6)
require("dplyr")
require("tidyr")
require("ggplot2")
d=data.frame(id=1:6, a=c(1,1,1,0,0,0), b=c(0,0,0,1,1,1), c=c(10,20,30,30,10,20))
ggplot(d %>% gather(type, value, a, b, c) %>% mutate(value = paste0(type, value)),
aes(x = id, y = type)) +
geom_tile(aes(fill = value), color = "white") +
scale_fill_manual(values = c("forestgreen", "indianred", "lightgoldenrod1",
"royalblue", "plum1", "plum2", "plum3"))
你如何确定颜色渐变?从我在图片上看到的'a'线上的6个矩形应该有'浅蓝 - 浅蓝 - 浅蓝 - 深蓝 - 深蓝 - 深蓝'(数值为'1 1 1 0 0 0') –
这只是我想要实现的一个随机示例。重点是根据datafram填充不同颜色的框。 –
你尝试过'geom_tile'吗? – zx8754