ggplot2 - 主題



在本章中,我們將重點介紹使用自定義主題,該主題用於更改工作區的觀感。我們將使用“ggthemes”包來理解 R 工作區中的主題管理概念。

讓我們執行以下步驟,在提到的資料集中使用所需的主題。

GGTHEMES

在 R 工作區中使用所需的包安裝“ggthemes”包。

> install.packages("ggthemes")
> Library(ggthemes)
GGTHEMES

實施新主題來生成製造商圖例及其生產年份和排量。

> library(ggthemes)
> ggplot(mpg, aes(year, displ, color=factor(manufacturer)))+
+ geom_point()+ggtitle("This plot looks a lot different from the default")+
+ theme_economist()+scale_colour_economist()
Implement New Theme

可以觀察到,使用以前的主題管理後,刻度線、圖例和其他元素的預設大小都比較小。輕鬆更改所有文字元素的大小。這可以透過建立一個自定義主題來實現,在以下步驟中可以看到,所有元素的大小都與 base_size 相對 (rel())。

> theme_set(theme_gray(base_size = 30))
> ggplot(mpg, aes(x=year, y=class))+geom_point(color="red")
Implement New Theme
廣告