简体   繁体   中英

How to get rid of bright border around plot in `theme_bw` transparent mode?

Is there a way to get rid of these white lines in theme_bw() when printed transparent onto darker background? In theme_minimal() they don't appear, but I want theme_bw() . I tried several theme() options to modify, without success though.

Example:

data(iris)
names(iris) <- tolower(names(iris))

library(ggplot2)
plot1 <- ggplot(iris, aes(x=sepal.length, y=sepal.width)) +
  geom_smooth() +
  theme_bw() + 
  theme(rect=element_rect(fill="transparent"),
        panel.grid = element_blank(),
        panel.background= element_blank())
pdf("plot1.pdf", width=4, height=4)
plot1
dev.off()

plot2 <- ggplot(iris, aes(x=sepal.length, y=sepal.width)) +
  geom_smooth() +
  theme_minimal() + 
  theme(rect=element_rect(fill="transparent"),
        panel.grid = element_blank(),
        panel.background= element_blank())
pdf("plot2.pdf", width=4, height=4)
plot2
dev.off()

在此输入图像描述

Note: I'm doing this in InDesign CS4.

You're looking for the plot.background theme element, not the panel.background one. The plot is the entire area, including outside the axes, while the panel is the area inside or between the axes.

Ugly plots to illustrate the difference:

ggplot(iris, aes(x=sepal.length, y=sepal.width)) +
  geom_smooth() +
  theme_bw() + 
  theme(rect=element_rect(fill="transparent"),
        panel.grid = element_blank(),
        panel.background= element_rect(color = "red", fill = "yellow", size = 6))

ggplot(iris, aes(x=sepal.length, y=sepal.width)) +
  geom_smooth() +
  theme_bw() + 
  theme(rect=element_rect(fill="transparent"),
        panel.grid = element_blank(),
        plot.background = element_rect(color = "red", fill = "yellow", size = 6))

theme_bw defaults to a plot background with a white border.

theme_bw()$plot.background
List of 5
 $ fill         : NULL
 $ colour       : chr "white"
 $ size         : NULL
 $ linetype     : NULL
 $ inherit.blank: logi TRUE
 - attr(*, "class")= chr [1:2] "element_rect" "element"

So you can instead set plot.background to an element_blank , or an element_rect with a color of NA or transparent, or any other means of making it invisible. Since you don't need any attributes of the plot background, the easiest is just plot.background = element_blank() .

ggplot(iris, aes(x=sepal.length, y=sepal.width)) +
  geom_smooth() +
  theme_bw() + 
  theme(rect=element_rect(fill="transparent"),
        panel.grid = element_blank(),
        plot.background = element_blank())

Panel Border should does the trick

plot1 <- ggplot(iris, aes(x=sepal.length, y=sepal.width)) +
  geom_smooth() +
  theme_bw() + 
  theme(rect=element_rect(fill="transparent"),panel.border = element_blank(),
        panel.grid = element_blank(),
        panel.background= element_blank())

Why not use the theme_minimal and adapt the theme?

col = "#383838"
plot2 <- ggplot(iris, aes(x=sepal.length, y=sepal.width)) +
  geom_smooth() +
  theme_minimal() + 
  theme(rect=element_rect(fill="transparent"),
        panel.grid = element_blank(),
        axis.ticks = element_line(colour = col),
        axis.line = element_line(colour = col, color = col, size = 0.1),
        panel.border = element_rect(colour = col),
        panel.background= element_blank())
pdf("plot2.pdf", width=4, height=4)
plot2
dev.off()

Or do you have to use theme_bw for some reason?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM