如何使用ggplot2在R中创建区域图

时间:2020-02-23 14:43:50  来源:igfitidea点击:

面积图是展示定量数据的图。
R提供了标准函数geom_area()来绘制面积图,并提供了geom_line()来使用ggplot2包在数据点上绘制线。

什么是面积图?

面积图是一种表示定量数据分布的线图。
在此图表类型中,我们将首先标记数据点,然后通过一条线将它们连接起来,以演示不同时间段的数据点或者值的数量。

在本教程中,我们将使用ggplot2库创建面积图。
好吧,如果您知道使用geom_area()函数,则距离在R中创建漂亮的面积图仅几步之遥。

来吧!

使用ggplot2在R中创建简单区域图

让我们使用正态分布值绘制一个简单的面积图。

这是使用ggplot2在R中的基本面积图。
这里的数据取为正态分布值(rmrm)。

执行以下代码以绘制面积图。

#imports the ggplot2 library
library(ggplot2)

#creates the dataframe having the normal distribution values (rnorm)
xdata<-1:50
ydata<-cumsum(rnorm(50))
data1<-data.frame(xdata,ydata)

#plots the area chart
ggplot(data1, aes(x=xdata, y=ydata))+geom_area(fill='#142F86',alpha=2)

使用ggplot2和hrbrthemes库自定义面积图

如上所示,一个简单的面积图看起来并不令人兴奋,对吧?好吧,让我们通过添加颜色,字体,样式和主题为我们的区域图增光添彩。

为此,您必须安装某些软件包,例如ggplot2和hrbrthemes

要安装ggplot2,请执行install.package(’ggplot2)

要安装hrbrthemes – install.packages(‘hrbrthemes)

该图包括面积图上的线和点。
点点和折线比简单的面积图有意义。
执行以下代码以绘制自定义面积图。

#install the required visualization libraries 
library(ggplot2)
library(hrbrthemes)
 
#loading the x and y data (normal distribution)
xdata<-1:50
ydata<-cumsum(rnorm(50))
#reading data into data frames
data<- data.frame(xdata,ydata)

#plots the area chart with theme, title and labels 
ggplot(data, aes(x=xdata, y=ydata))+
geom_area(fill='#142F86', alpha=1)+
geom_line(color='skyblue', size=1)+
geom_point(size=1, color='blue')+
ggtitle("Area plot using ggplot2 in R")+
labs(x='Value', y='frequency')+
theme_ipsum()

R中使用ggplot的基本堆积面积图

堆叠的面积图是面积图的一部分,它在一个图中演示了多个组的行为。

为此,您需要安装dplyr软件包。
要安装dplyr,请在r studio中运行以下代码。

install.packages(‘dplyr’)

下面的代码将说明相同的情况。

#import the libraries

library(ggplot2)
library(dplyr)
 
#creates the values and data frame
time<- as.numeric(rep(seq(1,7),each=7))
value<- runif(49,30,100)
group<- rep(LETTERS[1:7], times=7)
data1<-data.frame(time,value,group)
 
#plot the area stacked area chart
ggplot(data1, aes(x=time, y=value, fill=group))+geom_area() 

使用Viridis库增强面积图

上一节中我们增强简单区域图的方式非常棒。
同样,我们将向堆叠面积图添加一些字体,颜色和样式,但这一次使用Viridis。

Viridis是一个可视化库,可帮助向图形添加颜色和不同样式。
要安装Viridis软件包,请在r studio中运行以下代码。

install.package('viridis')

#impots the required libraries 
library(viridis)
library(hrbrthemes)

time <- as.numeric(rep(seq(1,7),each=7)) 
value <- runif(49, 10, 100)               
group <- rep(LETTERS[1:7],times=7)      
data <- data.frame(time, value, group)

#adds title, colors and styles to the plot
ggplot(data1, aes(x=time, y=value, fill=group))+
   geom_area(size=0.5, alpha=0.8, color='yellow')+
   scale_fill_viridis(discrete = TRUE)+
   theme_ipsum()+
   ggtitle("Customized area plot using viridis library")

使用plotly库绘制面积图

plotly是一个开放源代码库,用于创建具有各种主题和悬停功能的极具吸引力的视觉图形。

在本节中,我们将针对美国婴儿名字在过去几年中的受欢迎程度绘制叠加区域图。

如您所见,下图非常有趣,并带有图例。
对Plotly表示感谢。

#imports the required libraries
library(ggplot2)
library(hrbrthemes)
library(viridis)
library(babynames)
library(tidyverse)
library(plotly)

#creates the data frame with baby names
data<-babynames %>%
  filter(name %in% c('Margaret','Anna','Emma','Bertha','Sarah'))%>%
  filter(sex=='F')
 
#plots the stacked area chart with american babynames 
 p<-data%>%
   ggplot(aes(x=year, y=n, fill=name, text=name))+
   geom_area()+
   scale_fill_viridis(discrete = T)+
   theme(legend.position = 'none')+
   theme_ipsum()+
   ggtitle('Yearwise american baby names popularity')
 ggplotly(p, tootltip='text')

使用facet_wrap()绘制多个区域图

多个方面是面积图的主要部分,因为它们将演示每个数据组的行为。
在这种情况下,使用facet_wrap()函数说明了每个婴儿名字的流行程度。

使用简单的函数facet_wrap(),您可以在R中创建多个绘图面板。
此函数可方便地显示各个组的行为,如下所示。

执行以下代码,使用facet_wrap()函数创建多面板图。

#loads the babynames data with a filter of name and sex as 'F'
data<-babynames %>%
   filter(name %in% c('Margaret','Anna','Emma','Bertha','Sarah'))%>%
   filter(sex=='F')

#plots the multiple area plots using the function facet_wrap()
data%>%
 ggplot(aes(x=year, y=n, group=name, fill=name))+
 geom_area()+
 scale_fill_viridis(discrete = TRUE)+
 theme(legend.position = 'none')+
 ggtitle("Indivisual american names popularity - yearwise")+
 theme_ipsum()+
 theme(legend.position = "none", panel.spacing = unit(0.1, "lines"), 
 strip.text.x = element_text(size = 6))+
facet_wrap(~name, scale='free_y')

使用R中的堆积面积图求出1900-2002年间美国人口的年龄分布

在本节中,我们将绘制一个堆积面积图,该图显示1900年至2002年之间的人口年龄分布。

为此,您必须安装软件包gcookbook,其中包含USpopage数据。
您可以通过运行以下代码进行安装– install.packages('gcookbook')。

执行以下代码以绘制堆积面积图,以显示人口的年龄分布。

#installs the required package
install.packages('gcookbook’)

#imports the libraries
library(gcookbook)
library(ggplot2)

#reads the data
Str(uspopage)
ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup))+geom_area()

ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup))+geom_area(color='black', size=0.3, alpha=1)+scale_fill_brewer(palette = 'blues',breaks=rev(uspopage$AgeGroup))

#creates the stacked area chart with uspopage data
ggplot(uspopage, mapping = aes(x=Year, y=Thousands, fill=AgeGroup))+
   geom_area(color='black', size=0.5, alpha=1, position = position_stack(reverse = T))+
   scale_fill_brewer(palette = 'blues')+
guides(fill=guide_legend(reverse = T))

Ggplot多区域图

使用dplyr()库在R中按比例堆积的面积图

在比例堆积面积图中,组的值由百分比而不是其他参数表示。

此方法对于清楚地了解组的百分比非常有用,并请注意,百分比更有意义,并且还可以识别隐藏的数据模式。

好吧,对于这种方法,首先我们必须创建一个另外的百分比列。
为此,我们需要一个名为" dplyr"的库。

Dplyr是R中的特殊软件包,其中包括用于数据处理的特定工具。

执行以下代码以用百分比表示的组来绘制堆积面积图。

#installs the required package
install.packages(‘dplyr’)
#imports the library
library(dplyr)

#groups the data and adds the column with the percentiles
us_dplyr<-uspopage%>%
 group_by(Year)%>%
 mutate(percentage=Thousands/sum(Thousands)*100)
View(us_dplyr)

#plots the chart
ggplot(us_dplyr, aes(x=Year, y=percentage, fill=AgeGroup))+geom_area(color='black', size=0.3, alpha=1)+scale_fill_brewer(palette = 'blues',breaks=rev(uspopage$AgeGroup))

这是显示添加的"百分比"列的数据框。