2017-12-23 19 views
-1

プレゼンテーションスタイルに合わせて濃いグレーのテーマを使用するには、グラフが必要です。また、高さを固定する必要がありますが、幅はy軸上のラベルの長さによって異なる場合があります。エクスポートまたは保存すると、常に.jpgファイルまたは.pngファイルに白いバーが表示されます。ここで ggsave()を使用して暗いテーマのプロットを保存すると、白い境界線パネルが作成されます

私はチャートを作成するために使用するいくつかのサンプルコードです(簡略化した例に不要である、ここでいくつかの余分なテーマのコントロールがありますが、結果のグラフは、私が作成していますどのような基本的です):

library(ggplot2) 
bar.font <- 8 

title <- "Example" 
l_labs <- c("") 
x_labs <- c("A","B","C") 
ests <- c(.5,.3,.2) 
nerrs <- c(.05, .05, .05) 
perrs <- nerrs 

barchart.data <- data.frame(l_labs, x_labs, ests, nerrs, perrs) 
p <- ggplot(barchart.data, aes(x=x_labs, y=ests*100)) + 
    geom_bar(stat="identity", color="#808080", position=position_dodge(), width=0.85, fill="#808080") + 
    geom_text(aes(y=ests*100+perrs*100+1.5, label=sprintf("%1.1f%%", 100*ests)), vjust=0.5, hjust=0, size=bar.font, color="white") + 
    geom_errorbar(aes(ymin=ests*100-nerrs*100, ymax=ests*100+perrs*100), width=.2, position=position_dodge(.9), color="white", size=0.25) + 
    labs(title=title, x="", y = "") + theme_classic() + 
    scale_y_continuous(expand = c(0,0),limits = c(0,115), breaks=c(0, 20, 40, 60, 80, 100)) + 
    theme(legend.position="none", legend.text = element_text(color = "white")) + 
    theme(title = element_text(size=18, colour = "white")) + 
    theme(axis.text = element_text(size=20, color = "white"), axis.line = element_line(color = "white")) + 
    theme(axis.text.x = element_text(margin=margin(9,0,0,0)),axis.text.y = element_text(margin=margin(0,9,0,0))) + 
    theme(axis.title = element_text(size=20, color = "white")) + 
    theme(axis.title.x = element_text(margin = margin(10,0,0,0))) + 
    theme(axis.ticks = element_line(colour = 'white', size = .5)) + 
    coord_flip() + 
    theme(aspect.ratio = 1) + 
    theme(panel.background = element_rect(fill = "#1e1e1e")) + 
    theme(legend.justification=c(1,0), legend.position=c(1,0)) + 
    theme(plot.background = element_rect(fill = "#1e1e1e", color = "#1e1e1e")) + 
    theme(panel.grid.major.x = element_line(colour = "white",size=0.1, linetype = "dotted")) 

ggsave("test.jpg", height=10, units="in") 

エクスポートされた.jpgの外観は次のとおりです。正確な幅を指定することはできません。なぜなら、幅が変わるたびに各グラフの内容がわからないからです。ご指摘ありがとうございます。

Example image of exported chart using ggsave()

答えて

1

あなたが好きな値に背景色を設定することができます。これは、白バーの世話を

ggsave("test.jpg", height=10, units="in", bg = "#1e1e1e") 

enter image description here

ggsave()のマニュアルにbgオプションが記載されていないことが少し紛らわしいかもしれません。これは、グラフィックスデバイスの一部であるためです(ここではjpeg())。これはjpeg()のマニュアルに記載されています。

最初に白い棒がある理由については、this post.

関連する問題