图块 plot/heatmap 中的值差异很大 plot ggplot
Big difference of values in tile plot/heatmap plot ggplot
我有以下 df:
frequency name sample_type exist
1: 0.0115 sam 1 1
2: 0.3000 sam 2 0
3: 0.0000 sam 3 0
4: 0.0141 bill 1 1
5: 0.0124 bill 3 1
6: 0.0000 bill 2 0
7: 0.0136 mike 3 1
8: 0.0000 mike 1 0
9: 0.0000 mike 2 0
10: 0.0112 leo 1 1
11: 0.0000 leo 2 0
12: 1.0000 leo 3 0
13: 0.0108 brad 3 1
14: 0.0000 brad 2 0
15: 0.9900 brad 1 0
16: 0.0286 randy 3 1
17: 0.0000 randy 2 0
18: 0.7000 randy 1 0
我想创建图块 plot/heatmap 情节,所以我写了脚本:
library(data.table)
ra_df <- fread("G:\My Drive\Net2\test_1.csv",
select=c(1,2,3,4))
library(ggplot2)
ra_figure <- ggplot(ra_df, aes(name, factor(sample_type), fill = frequency)) +
geom_tile(colour="white",size=0.25) +
scale_fill_gradientn(colours = c("white", "lightgreen"), values = c(0,1)) +
theme(axis.text.x = element_text(angle = 90, hjust=1),
legend.position="none", axis.title.x = element_blank(),axis.title.y = element_blank(),
plot.title = element_text(hjust = 0.5,family = "Helvetica", face = "bold.italic", size = (15),
colour="steelblue4"))+
scale_x_discrete(position = "top") +
scale_y_discrete(labels=c("3"="c","2"="b", "1"="a"),expand=c(0,0))+
coord_fixed(ratio = 3)
然后我得到了下图:
如您所见,填充值之间存在很大差异,因此低值看起来是白色的,就像零值一样。较低的值对我来说很重要。
我怎样才能创建这个图形,以便较低的值也可见?
您可以将 scale_fill_gradientn
调整为:
scale_fill_gradientn(colours = c("lightgreen", "green"), values = c(0,1))
第一种颜色(浅绿色)对应您的最低值 (0),第二种颜色(绿色)对应您的最高值 (1)。
跟进您的评论:
scale_fill_gradientn(colours = c("white", "lightgreen", "green"), values = c(0, 0.0001, 1))
这应该没问题,除非您的值是 ]0, 0,0001[
。如果是这样,就把它设为 0.000001 等等。
我有以下 df:
frequency name sample_type exist
1: 0.0115 sam 1 1
2: 0.3000 sam 2 0
3: 0.0000 sam 3 0
4: 0.0141 bill 1 1
5: 0.0124 bill 3 1
6: 0.0000 bill 2 0
7: 0.0136 mike 3 1
8: 0.0000 mike 1 0
9: 0.0000 mike 2 0
10: 0.0112 leo 1 1
11: 0.0000 leo 2 0
12: 1.0000 leo 3 0
13: 0.0108 brad 3 1
14: 0.0000 brad 2 0
15: 0.9900 brad 1 0
16: 0.0286 randy 3 1
17: 0.0000 randy 2 0
18: 0.7000 randy 1 0
我想创建图块 plot/heatmap 情节,所以我写了脚本:
library(data.table)
ra_df <- fread("G:\My Drive\Net2\test_1.csv",
select=c(1,2,3,4))
library(ggplot2)
ra_figure <- ggplot(ra_df, aes(name, factor(sample_type), fill = frequency)) +
geom_tile(colour="white",size=0.25) +
scale_fill_gradientn(colours = c("white", "lightgreen"), values = c(0,1)) +
theme(axis.text.x = element_text(angle = 90, hjust=1),
legend.position="none", axis.title.x = element_blank(),axis.title.y = element_blank(),
plot.title = element_text(hjust = 0.5,family = "Helvetica", face = "bold.italic", size = (15),
colour="steelblue4"))+
scale_x_discrete(position = "top") +
scale_y_discrete(labels=c("3"="c","2"="b", "1"="a"),expand=c(0,0))+
coord_fixed(ratio = 3)
然后我得到了下图:
如您所见,填充值之间存在很大差异,因此低值看起来是白色的,就像零值一样。较低的值对我来说很重要。
我怎样才能创建这个图形,以便较低的值也可见?
您可以将 scale_fill_gradientn
调整为:
scale_fill_gradientn(colours = c("lightgreen", "green"), values = c(0,1))
第一种颜色(浅绿色)对应您的最低值 (0),第二种颜色(绿色)对应您的最高值 (1)。
跟进您的评论:
scale_fill_gradientn(colours = c("white", "lightgreen", "green"), values = c(0, 0.0001, 1))
这应该没问题,除非您的值是 ]0, 0,0001[
。如果是这样,就把它设为 0.000001 等等。