日志背景渐变ggplot [英] log background gradient ggplot

查看:59
本文介绍了日志背景渐变ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个ggplot2图,该图在y方向上具有特定背景.Y也是log2比例,从.25到4,中点,因此为1.Y极限(.25和4)必须为红色,而中点(1)必须为绿色.

解决方案

您可以使用 scale_fill_gradientn :

  bgplot<-ggplot(df,aes(x,y,fill = y))+geom_tile(alpha = 0.75)+scale_fill_gradientn(colors = c("red","green","red"),极限= c(0.25,4),trans ="log2")+scale_y_continuous(trans ="log2") 

I need a ggplot2 graph with a specific background in the y direction. Y is also log2 scale going from .25 to 4, midpoint thus 1. Y extremes (.25 and 4) must be red while midpoint (1) must be green.

Diagonal grading of background color of ggplot graph in R got me far and from that I have written this:

## create a diag gradient background
## create a df to supply the background to geom_tile
yseq <- seq(-2,2, length=100)

yseqlog2 <- 2^yseq

df <- expand.grid(x=0:100, y=yseqlog2) # dataframe for all combinations

## plot
bgplot <- ggplot(df, aes(x, y, fill=y)) +      # map fill to y
  geom_tile(alpha = 0.75) +      # let the grid show through a bit
  scale_fill_gradient2(low='red', high='red', mid = 'green',midpoint = 1) +  # choose your colours
  scale_y_continuous(trans = 'log2') # transform y axis to log2

bgplot

That gives me almost what I want, except the low red intensity at .25. See pic. How do I get full red at .25?? Thanks.

/Jens

解决方案

You can use scale_fill_gradientn :

bgplot <- ggplot(df, aes(x, y, fill = y)) +
  geom_tile(alpha = 0.75) + 
  scale_fill_gradientn(colors = c("red", "green", "red"), 
                       limits = c(0.25, 4), 
                       trans = "log2") +  
  scale_y_continuous(trans = "log2")

这篇关于日志背景渐变ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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