Ggplot2 简明教程

ggplot2 - Themes

在本章中,我们将专注于使用自定义主题,它用于改变工作区的观感。我们将使用 “ggthemes” 软件包来了解 R 工作区中的主题管理概念。

In this chapter, we will focus on using customized theme which is used for changing the look and feel of workspace. We will use “ggthemes” package to understand the concept of theme management in workspace of R.

让我们实施以下步骤,以在所提数据集内使用所需的主题。

Let us implement following steps to use the required theme within mentioned dataset.

GGTHEMES

使用 R 工作空间中所需的软件包安装 “ggthemes” 软件包。

Install “ggthemes” package with the required package in R workspace.

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

实施新主题以生成带有生产年份和排量的制造商图例。

Implement new theme to generate legends of manufacturers with year of production and displacement.

> 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

可以观察到,以前的主题管理中的刻度文本、图例和其他元素的默认大小有点小。可以非常容易地一次性更改所有文本元素的大小。这可以通过创建一个自定义主题来完成,我们可以在下面的步骤中观察到,所有元素的大小相对于基本大小 (rel())。

It can be observed that the default size of the tick text, legends and other elements are little small with previous theme management. It is incredibly easy to change the size of all the text elements at once. This can be done on creating a custom theme which we can observe in below step that the sizes of all the elements are relative (rel()) to the base_size.

> theme_set(theme_gray(base_size = 30))
> ggplot(mpg, aes(x=year, y=class))+geom_point(color="red")
implement new theme1