【问题标题】:R: ggplot background gradient coloringR:ggplot 背景渐变着色
【发布时间】:2016-09-22 07:09:06
【问题描述】:

我想生成带有渐变颜色的 ggplot,填充绘图面板及其背景,如图所示。

正如您所见,渐变背景颜色包含绘图面板及其背景。目前,我只知道所需解决方案的“近似值”:

library(ggplot2)
library(grid)
library(gridExtra)
reds <- c("#7B0664", "#E32219")
g <- rasterGrob(reds, width = unit(1, "npc"), height = unit(1, "npc"),   
                interpolate = TRUE) 
ggplot(data = economics, aes(x = date, y = unemploy)) + 
     annotation_custom(g, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) + 
     geom_line( alpha=1, color = "white", size = 0.5 ) +
     xlab("Years") + ylab("Unemployed [thousands]") +
     theme(plot.background = element_rect(fill=reds[2]))

使用上面显示的代码,绘图面板会在轴边界内显示为渐变色,但它不会跨越具有这种渐变色的整个背景。 theme(plot.background =...) 能够填充剩余的背景,但它似乎无法利用渐变色。进一步说明,应将相同的渐变着色应用于整个绘图背景。

任何建议将不胜感激。谢谢。

【问题讨论】:

    标签: r ggplot2 gridextra r-grid grob


    【解决方案1】:

    您可以在 rasterGrob 上打印/绘制绘图,

    library(ggplot2)
    library(grid)
    library(ggthemes)
    
    reds <- c("#7B0664", "#E32219")
    g <- rasterGrob(reds, width = unit(1, "npc"), height = unit(1, "npc"), interpolate = TRUE)
    p <- ggplot(data = economics, aes(x = date, y = unemploy)) +
      geom_line( alpha=1, color = "white", size = 0.5 ) +
      xlab("Years") + ylab("Unemployed [thousands]") +
      theme_base() + 
      theme(panel.background=element_blank(),
            panel.border = element_blank(),
            plot.background=element_blank(),
            text = element_text(colour="white"),
            line = element_line(colour="white")) +
      theme()
    
    grid.newpage()
    grid.draw(g)
    print(p, newpage = FALSE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 2022-12-21
      • 2012-06-04
      • 1970-01-01
      • 2012-03-15
      • 2018-11-25
      • 1970-01-01
      相关资源
      最近更新 更多