日志背景渐变ggplot

时间:2018-05-22 09:17:41

标签: r ggplot2 gradient

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

Diagonal grading of background color of ggplot graph in R让我走得很远,我写下了这个:

## 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

这给了我几乎我想要的东西,除了0.25的低红色强度。见图。如何在.25获得全红?感谢。

/延

output showing low red intensity at .25

1 个答案:

答案 0 :(得分: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"), 
                       limits = c(0.25, 4), 
                       trans = "log2") +  
  scale_y_continuous(trans = "log2")

enter image description here