R语言的ggplot2,热图,标尺(scale)

我想把0.05到1这个范围的标尺改成灰色

img

library(reshape2)
library(ggplot2)
data_m <-  melt (data2, id.vars= c ( "ID" ))  
p <- ggplot(data_m, aes(x=variable,y=ID)) + 
  xlab( "" ) +
  ylab( "" ) + 
  theme_bw() + 
  theme(panel.grid.major = element_blank()) + 
  theme(legend.key=element_blank()) + 
  coord_fixed(ratio=0.3)+
  theme(axis.text.x=element_text(angle=45,hjust=1, vjust=1)) + 
  theme(legend.position= "right" )+ 
  geom_tile(aes(fill=value),color="black") + 
  scale_fill_fermenter(name = "FDR",
                       breaks = c(0,0.005,0.010,0.015,0.020,0.030,0.040,0.050,1), 
                       limit = c(0, 1),
                       palette = 7 )+
  guides (fill = guide_coloursteps(show.limits = T))



我修改了一下,0.05到1是没问题了,但0.05一下变成这样子了,是不是与我数据的分布有关系,因为我没有0.05到1之间的数据,我的数据都在0.05一下或者是1

img

library(reshape2)
library(ggplot2)
data_m <-  melt (data2, id.vars= c ( "ID" ))  
p <- ggplot(data_m, aes(x=variable,y=ID)) + 
  xlab( "" ) +
  ylab( "" ) + 
  theme_bw() + 
  theme(panel.grid.major = element_blank()) + 
  theme(legend.key=element_blank()) + 
  coord_fixed(ratio=0.3)+
  theme(axis.text.x=element_text(angle=45,hjust=1, vjust=1)) + 
  theme(legend.position= "right" )+ 
  geom_tile(aes(fill=value),color="black") + 
  scale_fill_steps2(name = "FDR",
                       breaks = c(0,0.005,0.010,0.015,0.020,0.030,0.040,0.050,1), 
                       limit = c(0, 1),
                       low = "Red",
                       mid = "Yellow",midpoint = 0.05,
                       high = "Gray",
                       space = "Lab")+
  guides (fill = guide_coloursteps(show.limits = T))

这是一个散点图的Demo:

散点图的渐变色

图表使用qsec连续变量着色:

# Color by qsec values
sp2<-ggplot(mtcars, aes(x=wt, y=mpg, color=qsec)) + geom_point()
sp2
# Change the low and high colors
# Sequential color scheme
sp2+scale_color_gradient(low="blue", high="red")
# Diverging color scheme
mid<-mean(mtcars$qsec)
sp2+scale_color_gradient2(midpoint=mid, low="blue", mid="white",
                     high="red", space ="Lab" )

n种颜色之间的渐变

# Scatter plot
# Color points by the mpg variable
sp3<-ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) + geom_point()
sp3
# Gradient between n colors
sp3+scale_color_gradientn(colours = rainbow(5))

链接中有很多类似的 颜色的设置和风格的不同代码Demo,请查看下对你是否有帮助
参考链接:


如有问题及时沟通

加一行


p2 <- p + scale_colour_brewer(palette = "Yellows")