R中的饼图–在Plain R,GGPlot2和Plotrix中实现

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

R中的饼图是本质上为圆形的统计图。
饼图使用"切片"来表示或者说明数据的数字分布。

在饼图中,切片的大小显示值的权重。
在本文中,我们将绘制简单的饼图,添加标签,文本,并使用ggplot2和plotrix库。

饼图的优点和缺点

尽管饼图是展示数据分布的最佳图解之一,但它也有一些挫折。
饼形图的一些优缺点如下:

饼图–优点

  • 表示多组数据的比例。

  • 可以通过圆圈大小来表示数量。

  • 简单的可视化图。

  • 高度使用的图形代表大多数介质中的数据。

饼图–缺点

  • 不精确

  • 动态数据需要大量图表来展示。

  • 无法根据此可视化数据做出关键决策。

  • 由于错误的印象,无法使用3D绘图。

在R中创建饼图的语法

饼图语法。

pie(x, labels,radius,main,col)

其中:

  • x =向量或者数据中具有各种值。

  • 标签=每个切片的注释。

  • 半径=确定饼状圆的半径。

  • Main =代表饼图的标题。

  • col =此参数给出切片的颜色。

在R中绘制饼图

好吧,希望现在您对饼图的概念,定义和基本理解有所了解。
让我们在R中绘制一个简单的饼图,这将非常有趣。

1. R中的基本饼图

#vector with numerical values
Marks<- c(89,90,67,54,77,48)

#vector with strings as student names
Students_name<-c("Rahul","Ajay","Ram","Vishnu","Rupali","Shyma")

#plots a basic pie chart in R
pie(Marks,Students_name)

R中第一个基本饼形图上的荣誉。
在此图中,数值(标记)显示为切片大小,而学生姓名则显示为各个标记的标签。

您为更多而兴奋吗?不用再拖延了,让我们绘制另一个具有更多功能的饼图。

2.在饼图中添加标题和颜色

标题本身揭示了主题。
在本节中,我们将为饼图添加标题和颜色。
让我们看看它是如何工作的。

#vector having GDP values
GDP<-c(21.44,14.14,5.15,4.44,2.94)

#Vector having the strings as countries 
Countries<-c("America","China","Japan","Germany","San Franceco")

#plots the pie chart with title and colour
pie(GDP,labels = Countries,main = "Top 5 countries by GDP",border = 'White',col = rainbow(length(GDP)))

R中的饼形图,带有标题和颜色

现在,您可以将此饼图与以前的图表进行比较。
您可以看到此图中的巨大变化,对吗?这种情节更有意义,并且看起来也很优雅。
您可以看到,我们在图表中添加了标题"按GDP排名前5位的国家",并在饼图中添加了颜色(彩虹色)。

在某些情况下,您可能会觉得标签看起来不太好,或者您想同时显示数据(即值和标签)。
在这种情况下,您该怎么办?如果您的猜测是要添加"传奇",那就太棒了。

是的,在饼图中添加图例可以使其更有条理,也很有意义。

3.带有图例的饼图

图例是对图形上存在的事物的简单视觉解释。
它使用带有简短说明的颜色或者符号或者标签来指示图形上的内容,从而使图形看起来也很专业且组织良好。

在本节中,我们将向饼图添加图例。

#creates 2 vector of values and labels
x<-c(88,85,75,80,90)
laptop_brands<-c('Dell','HP','Lenovo','Asus','Apple')

#calculates the percentage of the values
percentage<-round(x/sum(x)*100)

#concatenates the strings with percentages
labels_new<-paste(laptop_brands,percentage)
labels_new

Output = "Dell 21" "HP 20" "Lenovo 18" "Asus 19" "Apple 22"

#concatenates the above output with the '%' symbol 
final_labels<-paste(labels_new,'%',sep = "")
final_labels

Output = "Dell 21%"   "HP 20%"    "Lenovo 18%"   "Asus 19%"   "Apple 22%" 

#plots the pie chart with various parameters
pie(x,labels = final_labels,col = rainbow(length(final_labels)),main = "Popularity of laptop brands",radius = 1)

#adds the legend to the pie chart
legend('topright',c("Dell","HP","Lenovo","Asus","Apple"),cex = 0.7,fill = rainbow(length(x)))

看起来不错吧?现在,我们的饼图中有一个图例,该图例在饼图中指示了笔记本电脑品牌及其颜色。
这全部是关于在饼图中添加图例。

在下一节中,我们将使用ggplot2绘制饼图。

4.使用ggplot2在R中绘制饼图

在本节中,我们将使用最佳库之一在R – ggplot2中进行绘图。

ggplot2是R中的数据可视化程序包。
ggplot2为图形添加了许多功能,以使其在存在性和平滑性方面也更好。

来吧!

#creates a data frame 
df<-data.frame(Phones=c("Samsung","One plus","iphone","Red mi","Realme"),sales=c(58,68,78,38,31))

Phones   sales
1  Samsung    58
2 One plus    68
3   iphone    78
4   Red mi    38
5   Realme    31

#plots the ggplot2 pie chart 

ggplot(df,aes(x='',y=sales,fill=Phones))+
ggtitle("Pie chart using ggplot2")+
geom_bar(stat = "Identity",width = 2,color="Yellow")+
coord_polar("y",start = 0)+
theme_void()

5.使用plotrix库的饼图

好吧,3D图看起来可能比普通图更好,但是3D饼图在表示负数数据时不是一个好选择,而且饼图无法有效地可视化大量变量,因为在大范围内它变得无效数据,即使您添加标签也是如此。
剧情变得人满为患。

您可能想知道为什么我只提到有关此3D饼图的底片。
好吧,3D饼图仅在少量数据时看起来不错。
非常适合立即做出决定,并且只代表您的分数。

让我们继续以上讨论,并使用库plotrix绘制3D饼图。

Plotrix:Plotlix是几种绘图功能(例如标签,标题,颜色等)的组合。
这用于创建3D图。

我使用的是ggplot2饼图中使用的相同数据。
让我们尝试为上面的ggplot2饼图绘制一个3-D图形。

#creates 2 vector of values and labels
x<-c(88,85,75,80,90)
laptop_brands<-c('Dell','HP','Lenovo','Asus','Apple')

#calculates the percentage of the values
percentage<-round(x/sum(x)*100)

#concatenates the strings with percentages
labels_new<-paste(laptop_brands,percentage)
labels_new

#concatenates the above output with the '%' symbol 
final_labels<-paste(labels_new,'%',sep = "")
final_labels

#install required packages 
install.packages('plotrix')
library(plotrix)

#plots the 3-D pie chart
pie3D(x,labels = final_labels,explode = 0.3,main='The 3-D pie chart using Plotrix',labelcex = 0.8,shade = 0.5,radius = 2.5)