ggplot2绘制背景颜色渐变 [英] ggplot2 plot background colour gradient
问题描述
这段代码产生下面的第一幅图:
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()
我有photoshopped在这个蓝色背景中:
我可以用R代码产生相同的蓝色背景吗?
ggplot2方法的相关链接在评论中给出。从那里复制:
library(grid)
g < - rasterGrob(blues9,width = unit(1, (gp)),height = unit(1,npc),
interpolate = TRUE)
#grid.draw(g)
library(ggplot2)
ggplot(mtcars,aes(factor(cyl)))+#添加渐变背景
annotation_custom(g,xmin = -Inf,xmax = Inf,ymin = -Inf,ymax = Inf)+
geom_bar )#添加数据层
我自己的方法:
像往常一样,我无法与 baptiste 对网格图形问题的解决方案,但这是我进行所有工作后的方法:
gg.background.fill< - 函数(gg.plot,cols =white,which =x){
#不适用facet
stopifnot(其中%c(x,y))
which1 < - if(which ==x)widthelseheight
require(gridExtra )
g < - ggplotGrob(gg.plot)
#g < - ggplotGrob(p)
gg < - g $ grobs
findIt< - vapply(gg,函数(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]] [[]]
w< - gg [findIt] [[1]] $ children [[n1]] $ children [[n2]] [[which1]]
attr< ; - 属性(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 (g)= c(arrange,ggplot,class(g))[[n2]] [[which1]] <-w
g $ grobs < - gg
class
$ b p1 < - gg.background.fill(p,colorRampPalette(c(red,blue))(100))
print(p1)
print(p2)
这改变了现有的背景可能被认为是一种优势,但与 annotation_custom
方法相反,它不适用于分面。需要更多的工作。
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()
I have photoshopped in this blue background:
Can I produce the same blue background with R code?
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)
p2 <- gg.background.fill(p, colorRampPalette(c("red", "blue"))(100), "y")
print(p2)
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.
这篇关于ggplot2绘制背景颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!