【问题标题】:How to set the legend of color gradient in ggplot2 to a given value?如何将ggplot2中的颜色渐变图例设置为给定值?
【发布时间】:2017-09-17 19:47:24
【问题描述】:

我正在尝试制作一个气泡图,其中点的颜色是取决于值的渐变颜色。我设置的极限值是 0.0001 到 0.1。我遇到的问题是我希望颜色渐变的图例具有相同的长度,从 0.0001 到 0.001,从 0.001 到 0.01,从 0.01 到 0.1。我写的代码代表中间的 0.5。 我有这个:

x <- as.data.frame(cbind(c("rRNA modification","response to hydrogen peroxide","RNA processing","mRNA processing","rRNA modification","response to hydrogen peroxide","RNA processing","mRNA processing"),c("DE","DE","DE","DE","AS","AS","AS","AS"),c(3.42,2.78,1.37,0.83,0,2.87,4.05,8.63),c(4.62e-08,4.32e-03,9.00e-02,8.60e-01,0,5.30e-01,5.67e-03,6.72e-03)))
colnames(x) <- c("go","set_gene","factor","p.value")
x$factor<- as.numeric(as.character(x$factor))
x$p.value <- as.numeric(as.character(x$p.value))
library(ggplot2)
plot1 <- ggplot(x,aes(x=set_gene,y=go,size=factor,fill=ifelse(p.value>0.1,0.1,ifelse(p.value<0.0001,0.0001,p.value))))
plot1 <- plot1+ geom_point(shape=21,size=x$factor*4)
library(scales)
plot1<- plot1 + 
scale_fill_gradientn(colours=c("red","orange","yellow","grey90"),values = 
rescale(c(0.0001,0.001,0.01,0.1)),limits=c(0.0001,0.1))
plot1

当前结果:

想要的结果:

谢谢!

【问题讨论】:

    标签: r ggplot2 colors gradient legend


    【解决方案1】:

    如何使用:

      plot1<- plot1 + scale_fill_gradientn(name = "p-value", trans = "log", breaks =  c(0.0001,0.001,0.01,0.1),limits = c(0.0001,0.1),colours=c("red","orange","yellow","grey90"))
    

    输出:

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 2017-07-03
      • 2016-12-15
      • 1970-01-01
      • 2019-03-01
      相关资源
      最近更新 更多