คู่มือฉบับสมบูรณ์เกี่ยวกับธีม ggplot2 ที่ดีที่สุด
บทช่วยสอนนี้จะให้คำแนะนำฉบับสมบูรณ์เกี่ยวกับธีม ggplot2 ที่ดีที่สุด ได้แก่:
- วิธีเปลี่ยนรูปลักษณ์ของแปลงโดยใช้ธีม ggplot2 ในตัว
 - วิธีเปลี่ยนรูปลักษณ์ของแปลงโดยใช้ธีมที่กำหนดไว้ล่วงหน้าจากไลบรารี ggthemes
 - วิธีแก้ไขส่วนประกอบเฉพาะของธีม รวมถึงพื้นหลังแผงพาธและเส้นตาราง
 
วิธีเปลี่ยนรูปลักษณ์ของพล็อตโดยใช้ธีม ggplot2 ในตัว
สำหรับแต่ละตัวอย่างต่อไปนี้ เราจะใช้ม่านตาจากชุดข้อมูล R ที่ฝังไว้:
 #view first six rows of iris dataset
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
ขั้นแรก เราจะโหลดไลบรารี ggplot2 และสร้าง Scatterplot ที่แสดง Sepal.Length บนแกน x และ Sepal.Width บนแกน y โดยให้ สีตามสปีชีส์:
#load ggplot2 library library(ggplot2) #create scatterplot ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point()
ต่อไป เราจะแสดงให้เห็นว่าธีม ggplot2 ในตัวแต่ละธีมส่งผลต่อรูปลักษณ์ของโครงเรื่องอย่างไร
grey_theme
ธีมเริ่มต้นที่มีพื้นหลังสีเทาและตารางสีขาว
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_gray()
bw_theme
ธีมสีดำบนพื้นขาว
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_bw()
theme_linedraw
ธีมที่มีเพียงเส้นสีดำซึ่งมีความกว้างต่างกันบนพื้นหลังสีขาว
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_linedraw()
theme_light
ธีมที่คล้ายกับ theme_linedraw แต่มีเส้นสีเทาและแกนที่ออกแบบมาเพื่อดึงดูดความสนใจไปที่ข้อมูลมากขึ้น
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_light()
dark_theme
ธีมที่คล้ายกับ theme_light แต่มีพื้นหลังสีเข้ม ธีมที่มีประโยชน์ในการดึงเส้นสีที่ละเอียดออกมา
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_dark()
มินิมอล_ธีม
ธีมที่ไม่มีคำอธิบายประกอบในพื้นหลัง
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_minimal()
classic_theme
ธีมที่ไม่มีกริด
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_classic()
theme_void
ธีมที่ว่างเปล่าโดยสิ้นเชิง
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_void()
วิธีเปลี่ยนรูปลักษณ์ของแปลงโดยใช้ธีมที่กำหนดไว้ล่วงหน้าจากไลบรารี ggthemes
นอกเหนือจากการใช้ธีม ggplot2 ในตัวแล้ว เราสามารถใช้ธีมที่กำหนดไว้ล่วงหน้าจากไลบรารี ggthemes เพื่อเปลี่ยนความสวยงามของโครงเรื่องได้
ขั้นแรก เราจะโหลดไลบรารี ggthemes:
library(ggthemes)
จากนั้นเราจะแสดงตัวอย่างการใช้ธีมที่กำหนดไว้ล่วงหน้าเพื่อปรับเปลี่ยนความสวยงามของโครงเรื่อง:
theme_wsj
ธีมจาก Wall Street Journal
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_wsj()
theme_tufte
ธีมมินิมอลที่ได้รับแรงบันดาลใจจากผลงานของนักสถิติ Edward Tufte
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_tufte()
โซลาร์ไลซ์_ธีม
ธีมที่ใช้สีตาม Solarized Palette
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_solarized()
โปรดทราบว่าเรายังสามารถใช้อาร์กิวเมนต์ light = FALSE เพื่อใช้พื้นหลังสีเข้มบนโครงเรื่องได้:
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_solarized( light = FALSE )
theme_gdocs
ธีมที่มีค่าเริ่มต้นของ Google Docs Chart
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_gdocs()
theme_fivethirtyeight
ธีมที่ได้รับแรงบันดาลใจจากโครงเรื่องของ fivethirtyeight.com
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_fivethirtyeight()
theme_economist
ธีมที่ได้รับแรงบันดาลใจจาก The Economist
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_economist()
วิธีแก้ไขส่วนประกอบเฉพาะของแปลง
เราสามารถใช้ฟังก์ชัน theme() และ element_rect() เพื่อเปลี่ยนสีพื้นหลังของพาเนลพล็อต:
theme(panel.background = element_rect(fill, color, size))
- เติม: เติมสีให้กับสี่เหลี่ยม
 - สี: สีขอบ
 - ขนาด: ขนาดเส้นขอบ
 
เรายังสามารถใช้ฟังก์ชัน element_line() เพื่อเปลี่ยนขนาดและลักษณะของตาราง:
 theme(panel.grid.major = element_line(color, size, linetype),
      panel.grid.minor = element_line(color, size, linetype))
- สี: สีขอบ
 - ขนาด: ขนาดเส้นขอบ
 - linetype: linetype (“ว่างเปล่า”, “ทึบ”, “เส้นประ”, “ประ”, “จุดประ”, “เส้นประยาว”, “สองขีดกลาง”)
 
รหัสต่อไปนี้สาธิตวิธีการลบเส้นขอบแผงลงจุดและเส้นตาราง:
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme(panel.border = element_blank(),
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank())
รหัสต่อไปนี้สาธิตวิธีการเปลี่ยนพื้นหลังแผงลงจุดและเส้นตาราง:
 ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme(
    panel.background = element_rect(fill = "powderblue",
    color = "powderblue",
    size = 0.5, linetype = "solid"),
    panel.grid.major = element_line(size = 0.5, linetype = 'solid', color = "white"),
    panel.grid.minor = element_line(size = 0.25, linetype = 'solid', color = "white")
  )