ฉันจะเปลี่ยนสีพื้นหลังของพล็อตที่สร้างด้วย ggplot2 ได้อย่างไร


101

ตามค่าเริ่มต้น ggplot2 จะสร้างพล็อตที่มีพื้นหลังสีเทา ฉันจะเปลี่ยนสีพื้นหลังของพล็อตได้อย่างไร?

ตัวอย่างเช่นพล็อตที่สร้างโดยรหัสต่อไปนี้:

library(ggplot2)
myplot<-ggplot(data=data.frame(a=c(1,2,3), b=c(2,3,4)), aes(x=a, y=b)) + geom_line()
myplot

คำตอบ:


130

ในการเปลี่ยนสีพื้นหลังของแผงให้ใช้รหัสต่อไปนี้:

myplot + theme(panel.background = element_rect(fill = 'green', colour = 'red'))

หากต้องการเปลี่ยนสีของพล็อต (แต่ไม่ใช่สีของแผงควบคุม) คุณสามารถทำได้:

myplot + theme(plot.background = element_rect(fill = 'green', colour = 'red'))

ดูที่นี่สำหรับรายละเอียดเพิ่มเติมธีมแผ่นอ้างอิงด่วนสำหรับตำนานแกนและรูปแบบ


42
นอกจากนี้ยังมีtheme_bwพื้นหลังสีขาวและเส้นตารางสีเทา ฉันใช้มันตลอดเวลาเพราะในการพิมพ์มันดูดีกว่าพื้นหลังสีเทาเริ่มต้นมาก:myplot + theme_bw()
ROLO

@ROLO: ดี! มีวิธีใช้สิ่งนี้กับทุกแปลงโดยค่าเริ่มต้นหรือไม่?
krlmlr

12
ใส่นี้ที่จุดเริ่มต้นของสคริปต์ของคุณสำหรับการเริ่มต้น B & W ggplots นี้: ggplot <- function(...) { ggplot2::ggplot(...) + theme_bw() }
ROLO

1
@ROLO สมควรได้รับคำตอบของตัวเองโดยเฉพาะอย่างยิ่งเพราะคำตอบของ Jack ไม่เปลี่ยนสีของเส้นตาราง
naught101

7
โปรดทราบว่าoptsและtheme_rectเลิกใช้งานแล้วใน ggplot2 เวอร์ชันที่ใหม่กว่า (0.9.3) ดังนั้นคำสั่งที่สองเวอร์ชันที่ใหม่กว่าจะกลายเป็น:myplot + theme(plot.background = element_rect(fill='green', colour='red'))
รามนราสิมฮาน

50

เพื่อหลีกเลี่ยงการเลิกใช้งานoptsและtheme_rectใช้:

myplot + theme(panel.background = element_rect(fill='green', colour='red'))

ในการกำหนดธีมที่คุณกำหนดเองขึ้นอยู่กับ theme_gray แต่มีการเปลี่ยนแปลงบางอย่างและสิ่งพิเศษเพิ่มเติมบางส่วนรวมถึงการควบคุมสี / ขนาดเส้นตาราง (ตัวเลือกเพิ่มเติมที่สามารถเล่นได้ที่ ggplot2.org ):

theme_jack <- function (base_size = 12, base_family = "") {
    theme_gray(base_size = base_size, base_family = base_family) %+replace% 
        theme(
            axis.text = element_text(colour = "white"),
            axis.title.x = element_text(colour = "pink", size=rel(3)),
            axis.title.y = element_text(colour = "blue", angle=45),
            panel.background = element_rect(fill="green"),
            panel.grid.minor.y = element_line(size=3),
            panel.grid.major = element_line(colour = "orange"),
            plot.background = element_rect(fill="red")
    )   
}

ในการทำให้ธีมที่กำหนดเองของคุณเป็นค่าเริ่มต้นเมื่อมีการเรียก ggplot ในอนาคตโดยไม่ต้องกำบัง:

theme_set(theme_jack())

หากคุณต้องการเปลี่ยนองค์ประกอบของธีมที่ตั้งไว้ในปัจจุบัน:

theme_update(plot.background = element_rect(fill="pink"), axis.title.x = element_text(colour = "red"))

ในการจัดเก็บธีมเริ่มต้นปัจจุบันเป็นวัตถุ:

theme_pink <- theme_get()

โปรดทราบว่าtheme_pinkเป็นรายการในขณะที่theme_jackเป็นฟังก์ชัน เพื่อที่จะกลับมาในรูปแบบของการใช้ theme_jack ขณะที่จะกลับไปใช้theme_set(theme_jack()) theme_pinktheme_set(theme_pink)

คุณสามารถแทนที่theme_grayด้วยtheme_bwคำจำกัดความของtheme_jackif you want เพื่อให้ธีมที่กำหนดเองของคุณมีลักษณะคล้ายกันtheme_bwแต่ปิดใช้งานเส้นตารางทั้งหมด (x, y, major และ minor):

theme_nogrid <- function (base_size = 12, base_family = "") {
    theme_bw(base_size = base_size, base_family = base_family) %+replace% 
        theme(
            panel.grid = element_blank()
    )   
}

ในที่สุดธีมที่รุนแรงกว่าก็มีประโยชน์เมื่อวางพล็อตchoroplethsหรือแผนที่อื่น ๆ ใน ggplot ตามการสนทนาที่นี่แต่อัปเดตเพื่อหลีกเลี่ยงการเลิกใช้งาน จุดมุ่งหมายคือการลบพื้นหลังสีเทาและคุณสมบัติอื่น ๆ ที่อาจเบี่ยงเบนความสนใจจากแผนที่

theme_map <- function (base_size = 12, base_family = "") {
    theme_gray(base_size = base_size, base_family = base_family) %+replace% 
        theme(
            axis.line=element_blank(),
            axis.text.x=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks=element_blank(),
            axis.ticks.length=unit(0.3, "lines"),
            axis.ticks.margin=unit(0.5, "lines"),
            axis.title.x=element_blank(),
            axis.title.y=element_blank(),
            legend.background=element_rect(fill="white", colour=NA),
            legend.key=element_rect(colour="white"),
            legend.key.size=unit(1.2, "lines"),
            legend.position="right",
            legend.text=element_text(size=rel(0.8)),
            legend.title=element_text(size=rel(0.8), face="bold", hjust=0),
            panel.background=element_blank(),
            panel.border=element_blank(),
            panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),
            panel.margin=unit(0, "lines"),
            plot.background=element_blank(),
            plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
            plot.title=element_text(size=rel(1.2)),
            strip.background=element_rect(fill="grey90", colour="grey50"),
            strip.text.x=element_text(size=rel(0.8)),
            strip.text.y=element_text(size=rel(0.8), angle=-90) 
        )   
}

1
สิ่งนี้มีประโยชน์มากขอบคุณ FYI ฉันได้พบอาร์กิวเมนต์ต้องถูกส่งผ่านไปplot.background themeอาร์กิวเมนต์อื่นเป็นทางเลือก
แข่งลูกอ๊อด

1

นี่คือธีมที่กำหนดเองเพื่อทำให้พื้นหลัง ggplot2 เป็นสีขาวและการเปลี่ยนแปลงอื่น ๆ อีกมากมายที่เหมาะสำหรับสิ่งพิมพ์และโปสเตอร์ เพียงแค่ใช้ + mytheme หากคุณต้องการเพิ่มหรือเปลี่ยนตัวเลือกตาม + ธีมหลัง + mytheme มันจะแทนที่ตัวเลือกเหล่านั้นจาก + mytheme

library(ggplot2)
library(cowplot)
theme_set(theme_cowplot())

mytheme = list(
    theme_classic()+
        theme(panel.background = element_blank(),strip.background = element_rect(colour=NA, fill=NA),panel.border = element_rect(fill = NA, color = "black"),
              legend.title = element_blank(),legend.position="bottom", strip.text = element_text(face="bold", size=9),
              axis.text=element_text(face="bold"),axis.title = element_text(face="bold"),plot.title = element_text(face = "bold", hjust = 0.5,size=13))
)

ggplot(data=data.frame(a=c(1,2,3), b=c(2,3,4)), aes(x=a, y=b)) + mytheme + geom_line()

ธีม ggplot ที่กำหนดเอง

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.