0引言
R語言中的ggplot2包裏面的風格固定,在需要特殊的圖形時,需要更改甚至自定義設置主題。本文展示ggplot2包裏的八種默認的主題,並介紹擴展包ggthemes去自定義主題內容。
八種主題函數彙總表
主題函數 | 效果 |
---|---|
theme_bw() | 網格白色主題 |
theme_classic() | 經典主題 |
theme_dark() | 暗色主題,可用於對比 |
theme_gray() | 默認主題 |
theme_light() | 淺色座標帶網格 |
theme_linedraw() | 黑色網格線 |
theme_minimal() | 極簡主題 |
theme_void() | 空白主題 |
主題彙總圖
代碼:
p1 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_bw() +
labs(title = "網格白色主題")+ theme(legend.position = "none")
p2 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_classic() +
labs(title = "經典主題")+ theme(legend.position = "none")
p3 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_dark() +
labs(title = "暗色主題,可用於對比")+ theme(legend.position = "none")
p4 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_gray() +
labs(title = "默認主題")+ theme(legend.position = "none")
p5 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_light() +
labs(title = "淺色座標帶網格")+ theme(legend.position = "none")
p6 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_linedraw() +
labs(title = "黑色網格線")+ theme(legend.position = "none")
p7 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_minimal() +
labs(title = "極簡主題")+ theme(legend.position = "none")
p8 <- ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, col = class, shape = class)) +
theme_void() +
labs(title = "空白主題")+ theme(legend.position = "none")
grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8, nrow=2)
擴展包
如果感覺上面的主題話沒有你滿意的可以去加載ggthemes
包,裏面會有更加個性化的參數,創作出你想要的主題。