我有以下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
我想创建图块图/热图图,所以我编写了脚本:
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)
如您所见,填充值之间存在很大差异,因此低值看起来像白色,就像零值一样。较低的值对我来说很重要。
如何创建此图形,以便较低的值也可见?
答案 0 :(得分:2)
您可以将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,依此类推。