Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

r - Plot background colour in gradient

This code produces the first plot below:

water.height <- seq(0, 5, 1)
y <- seq(0, 1500, length.out = 6)
df <- data.frame(water.height, y)

library(ggplot2)
ggplot(df, aes(water.height, y)) + geom_blank()+ theme_bw()

enter image description here

I have photoshopped in this blue background:

enter image description here

Can I produce the same blue background with R code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The relevant link to the ggplot2 approach was given in the comments. Copied from there:

library(grid) 
g <- rasterGrob(blues9, width=unit(1,"npc"), height = unit(1,"npc"), 
interpolate = TRUE) 
# grid.draw(g) 

library(ggplot2) 
ggplot(mtcars, aes(factor(cyl))) + # add gradient background 
   annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + 
   geom_bar() # add data layer 

My own approach:

As usual, I cannot compete with the simple elegance of baptiste's solutions for problems with grid graphics, but here is my approach since I went to all that work:

gg.background.fill <- function(gg.plot, cols = "white", which = "x") {
  #does not work with facets

  stopifnot(which %in% c("x", "y"))
  which1 <- if (which == "x") "width" else "height"

  require(gridExtra)

  g <- ggplotGrob(gg.plot)
  #g <- ggplotGrob(p)
  gg <- g$grobs      
  findIt <- vapply(gg, function(x) grepl("GRID.gTree", x$name, fixed = TRUE), TRUE)
  n1 <- getGrob(gg[findIt][[1]], "grill.gTree", grep=TRUE)$name
  n2 <- getGrob(gg[findIt][[1]], "panel.background.rect", grep=TRUE)$name
  gg[findIt][[1]]$children[[n1]]$children[[n2]]$gp$fill <- cols
  x <- gg[findIt][[1]]$children[[n1]]$children[[n2]][[which]]
  w <- gg[findIt][[1]]$children[[n1]]$children[[n2]][[which1]]
  attr <- attributes(x)
  x <- seq(0 + c(w)/length(cols)/2, 1 - c(w)/length(cols)/2, length.out = length(cols))
  attributes(x) <- attr
  gg[findIt][[1]]$children[[n1]]$children[[n2]][[which]] <- x
  w <- c(w)/length(cols) 
  attributes(w) <- attr
  gg[findIt][[1]]$children[[n1]]$children[[n2]][[which1]] <- w
  g$grobs <- gg
  class(g) = c("arrange", "ggplot", class(g)) 
  g
}
p1 <-  gg.background.fill(p, colorRampPalette(c("red", "blue"))(100))
print(p1)

resulting plot

p2 <-  gg.background.fill(p, colorRampPalette(c("red", "blue"))(100), "y")
print(p2)

enter image description here

This modifies the existing background which might be considered an advantage, but in contrast to the annotation_custom approach it doesn't work with faceting. More work would be required for that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...