更改 geom_tile() 中的中断
changing the breaks in geom_tile()
我有矩阵数据,想使用 geom_tile() 创建热图。
geom_tile() 成功运行,但“值”的限制似乎变长了。 (例如,超过 100 的值是相同的颜色中断)。我想将结果与其他数据集进行比较,所以我认为图形之间相同的颜色中断更好。
Var1 <- c(0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162)
Var2 <- c(0,0,0,0,0,3,3,3,3,3,31,31,31,31,31,316,316,316,316,316, 3162, 3162, 3162, 3162, 3162)
value <- c(67,79,90,20, 5,10,88,82,11, 1, 2,17, 107,41, 4, 3,15,81,66,12, 1, 3,14,16,23)
longData <- data.frame(cbind(Var1, Var2, value))
ggplot(longData, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
scale_fill_steps(low="grey90", high="red", breaks=c(0, 10, 25, 50, 100, 150, 200, 300)) +
labs(x="2000", y="2007", title="Matrix")+
geom_text(aes(label = value))
-----更新-----
我想比较这两个图,但显示的颜色因值而异。
Var1 <- c(3, 31,316,3162,3,31,316,3162,3,31, 316,3162,3,31,316, 3162, 31,316,3162)
Var2 <- c(0,0,0,0,3,3,3,3,31,31,31,31,316,316,316,316,3162,3162,3162)
value <- c(36,34,9,4,129,59,12,2,37,277,50,10,3,23,106,22,5,9, 31)
longData2 <- data.frame(cbind(Var1, Var2, value))
ggplot(longData2, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
scale_fill_steps(low="grey90", high="red", breaks=c(0, 10, 25, 50, 100, 150, 200, 300)) +
labs(x="2007", y="2012", title="Matrix")+
geom_text(aes(label = value))
这似乎比我想象的要难。看来您还需要照顾重新缩放器。为了保持两者相同,您可以定义一次比例,例如
my_fill <- scale_fill_steps(low="grey90", high="red",
breaks=c(0, 10, 25, 50, 100, 150, 200, 300),
rescale=function(x, ...) scales::rescale(x, from=c(0, 300)),
limits=c(0,300))
然后将其用于两个图
ggplot(longData, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
my_fill +
labs(x="2007", y="2012", title="Matrix")+
geom_text(aes(label = value))
ggplot(longData2, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
my_fill +
labs(x="2007", y="2012", title="Matrix")+
geom_text(aes(label = value))
我有矩阵数据,想使用 geom_tile() 创建热图。 geom_tile() 成功运行,但“值”的限制似乎变长了。 (例如,超过 100 的值是相同的颜色中断)。我想将结果与其他数据集进行比较,所以我认为图形之间相同的颜色中断更好。
Var1 <- c(0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162)
Var2 <- c(0,0,0,0,0,3,3,3,3,3,31,31,31,31,31,316,316,316,316,316, 3162, 3162, 3162, 3162, 3162)
value <- c(67,79,90,20, 5,10,88,82,11, 1, 2,17, 107,41, 4, 3,15,81,66,12, 1, 3,14,16,23)
longData <- data.frame(cbind(Var1, Var2, value))
ggplot(longData, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
scale_fill_steps(low="grey90", high="red", breaks=c(0, 10, 25, 50, 100, 150, 200, 300)) +
labs(x="2000", y="2007", title="Matrix")+
geom_text(aes(label = value))
-----更新-----
我想比较这两个图,但显示的颜色因值而异。
Var1 <- c(3, 31,316,3162,3,31,316,3162,3,31, 316,3162,3,31,316, 3162, 31,316,3162)
Var2 <- c(0,0,0,0,3,3,3,3,31,31,31,31,316,316,316,316,3162,3162,3162)
value <- c(36,34,9,4,129,59,12,2,37,277,50,10,3,23,106,22,5,9, 31)
longData2 <- data.frame(cbind(Var1, Var2, value))
ggplot(longData2, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
scale_fill_steps(low="grey90", high="red", breaks=c(0, 10, 25, 50, 100, 150, 200, 300)) +
labs(x="2007", y="2012", title="Matrix")+
geom_text(aes(label = value))
这似乎比我想象的要难。看来您还需要照顾重新缩放器。为了保持两者相同,您可以定义一次比例,例如
my_fill <- scale_fill_steps(low="grey90", high="red",
breaks=c(0, 10, 25, 50, 100, 150, 200, 300),
rescale=function(x, ...) scales::rescale(x, from=c(0, 300)),
limits=c(0,300))
然后将其用于两个图
ggplot(longData, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
my_fill +
labs(x="2007", y="2012", title="Matrix")+
geom_text(aes(label = value))
ggplot(longData2, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
my_fill +
labs(x="2007", y="2012", title="Matrix")+
geom_text(aes(label = value))