2016-09-22 7 views
2

ここに示すように、グラデーションカラーでggplotを生成し、プロットパネルとその背景の両方に塗りつぶしたいと思います。あなたはグラデーションの背景色を見ることができるようにR:ggplot背景グラデーションカラーリング

enter image description here

プロットパネルとその背景の両方を包含する。現時点では、必要なソリューションの唯一の「近似値」は、私にはよく知られている:

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])) 

aboveshownコードを使用して、軸の境界内に色のグラデーションとしてプロットパネルの結果は、しかし、それはそのようで、全体的な背景をまたがりませんグラデーションカラーリング。テーマ(plot.background = ...)は残りの背景を塗りつぶすことができますが、グラデーションの色付けを利用することはできません。さらに、同じグラデーションカラーをプロットバックグラウンド全体に適用する必要があることを忘れないでください。

ご意見をお寄せください。ありがとう。

答えて

0

あなたは

enter image description here

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) 
、rasterGrobの上にプロットを描く/印刷することができます