R에서 투명한 배경의 PNG 파일로 ggplot2 그래픽을 출력해야합니다. 기본 R 그래픽에서는 모든 것이 정상이지만 ggplot2에서는 투명성이 없습니다.
d <- rnorm(100) #generating random data
#this returns transparent png
png('tr_tst1.png',width=300,height=300,units="px",bg = "transparent")
boxplot(d)
dev.off()
df <- data.frame(y=d,x=1)
p <- ggplot(df) + stat_boxplot(aes(x = x,y=y))
p <- p + opts(
panel.background = theme_rect(fill = "transparent",colour = NA), # or theme_blank()
panel.grid.minor = theme_blank(),
panel.grid.major = theme_blank()
)
#returns white background
png('tr_tst2.png',width=300,height=300,units="px",bg = "transparent")
p
dev.off()
ggplot2로 투명한 배경을 얻을 수있는 방법이 있습니까?
답변
theme()
함수 ggsave()
및 범례 배경 코드로 업데이트되었습니다 .
df <- data.frame(y = d, x = 1, group = rep(c("gr1", "gr2"), 50))
p <- ggplot(df) +
stat_boxplot(aes(x = x, y = y, color = group),
fill = "transparent" # for the inside of the boxplot
)
가장 빠른 방법은 rect
모든 사각형 요소가 rect에서 상속하므로 using을 사용하는 것입니다 .
p <- p +
theme(
rect = element_rect(fill = "transparent") # all rectangles
)
p
보다 통제 된 방법은 다음 옵션을 사용하는 것입니다 theme
.
p <- p +
theme(
panel.background = element_rect(fill = "transparent"), # bg of the panel
plot.background = element_rect(fill = "transparent", color = NA), # bg of the plot
panel.grid.major = element_blank(), # get rid of major grid
panel.grid.minor = element_blank(), # get rid of minor grid
legend.background = element_rect(fill = "transparent"), # get rid of legend bg
legend.box.background = element_rect(fill = "transparent") # get rid of legend panel bg
)
p
저장하려면 (이 마지막 단계가 중요합니다) :
ggsave(p, filename = "tr_tst2.png", bg = "transparent")
답변
다음과 같은 plot.background
옵션 도 있습니다 panel.background
.
df <- data.frame(y=d,x=1)
p <- ggplot(df) + stat_boxplot(aes(x = x,y=y))
p <- p + opts(
panel.background = theme_rect(fill = "transparent",colour = NA), # or theme_blank()
panel.grid.minor = theme_blank(),
panel.grid.major = theme_blank(),
plot.background = theme_rect(fill = "transparent",colour = NA)
)
#returns white background
png('tr_tst2.png',width=300,height=300,units="px",bg = "transparent")
print(p)
dev.off()
어떤 이유로 업로드 한 이미지가 내 컴퓨터와 다르게 표시되어 생략했습니다. 하지만 저에게는 여전히 흰색 인 상자 그림의 상자 부분을 제외하고 완전히 회색 배경의 그림이 표시됩니다. boxplot 지오메트리의 채우기 미학을 사용하여 변경할 수도 있습니다.
편집하다
ggplot2 는 이후 업데이트되었으며 opts()
함수는 더 이상 사용되지 않습니다. 현재, theme()
대신 opts()
및 element_rect()
대신 theme_rect()
등을 사용 합니다 .
답변
YCR의 답변을 개선하기 위해 :
1) x와 y 축에 검은 선을 추가했습니다. 그렇지 않으면 투명하게됩니다.
2) 범례 키에 투명한 테마를 추가했습니다. 그렇지 않으면 거기에 채워져 심미적이지 않을 것입니다.
마지막으로, 모든 파일은 pdf 및 png 형식에서만 작동합니다. jpeg는 투명한 그래프를 생성하지 못합니다.
MyTheme_transparent <- theme(
panel.background = element_rect(fill = "transparent"), # bg of the panel
plot.background = element_rect(fill = "transparent", color = NA), # bg of the plot
panel.grid.major = element_blank(), # get rid of major grid
panel.grid.minor = element_blank(), # get rid of minor grid
legend.background = element_rect(fill = "transparent"), # get rid of legend bg
legend.box.background = element_rect(fill = "transparent"), # get rid of legend panel bg
legend.key = element_rect(fill = "transparent", colour = NA), # get rid of key legend fill, and of the surrounding
axis.line = element_line(colour = "black") # adding a black line for x and y axis
)