2017-12-23 24 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.