如何将ggplot2中的颜色渐变图例设置为给定值? [英] How to set the legend of color gradient in ggplot2 to a given value?

查看:496
本文介绍了如何将ggplot2中的颜色渐变图例设置为给定值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to do a bubble chart where the colors of the points are a gradient color depending on a value. The limit values I've set are 0.0001 to 0.1. The problem i have is that i want that the legend of color gradient have the same length from 0.0001 to 0.001, from 0.001 to 0.01, and from 0.01 to 0.1. The code I've wrote represents 0.5 in the middle of scale. I have this:

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

当前结果:

所需结果:

谢谢!

推荐答案

如何使用:

  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"))

输出:

这篇关于如何将ggplot2中的颜色渐变图例设置为给定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆