DATA SCIENCE ZING
  • Data Science
  • Machine Learning
  • Deep Learning
  • Artificial Intelligence
  • Big Data
  • Computer Vision
  • Blog
  • Advertise with us

How to create pie-charts in R using simple,3D, ggplot2 and googleVis methods

6/9/2017

0 Comments

 

Create a dataset- Sales of different region

region<-c("US","Europe","Japan","China","Others")
sales<-c(25000,12000 ,10000,5000,2000)
region_sales<-data.frame(region,sales)

Simple Normal Piechart

pie(sales,labels = region,main="Sales per region")

plot of chunk unnamed-chunk-2

Simple Pie chart with Percentage lables

pct<-round(sales/sum(sales)*100)
label<-paste(region,pct)
label<-paste(label,"%",sep="")
pie(sales,labels = label,col=rainbow(length(label)),main="Sales per region")

plot of chunk unnamed-chunk-3

3D Pie Chart

library(plotrix)
pie3D(sales,labels = region,main="3D Pie chart of Sales per region")

plot of chunk unnamed-chunk-4

Using ggplot2

library(ggplot2)
bar<-ggplot(region_sales,aes(x="",y=sales,fill=region))+geom_bar(width=1,stat="identity")
bar

plot of chunk unnamed-chunk-5

pie<-bar+coord_polar("y",start=0)+scale_fill_brewer(palette = "Dark2")+theme_minimal()
pie

plot of chunk unnamed-chunk-5

Using ggplot2 -Percentage Annotation

ggplot(transform(transform(region_sales, sales=sales/sum(sales)), labPos=cumsum(sales)-sales/2), 
       aes(x="", y = sales, fill = region)) +
  geom_bar(width = 1, stat = "identity") +
  scale_fill_manual(values = c("red", "yellow","blue", "green", "cyan")) +
  coord_polar(theta = "y") +
  labs(title = "Percentage Sales per Region") + 
  geom_text(aes(y=labPos, label=scales::percent(sales)))

plot of chunk unnamed-chunk-6

Using googleVis

library(googleVis)
op <- options(gvis.plot.tag = "chart")
pie <- gvisPieChart(region_sales, options = list(title = "Sales per region", 
    width = 1000, height = 500))
plot(pie)

0 Comments



Leave a Reply.

    Picture

      Subscribe now

    Subscribe to Newsletter
    Picture

    RSS Feed

    Categories

    All
    Basics
    Classification
    Courses
    Foreacsting
    Mapping
    R
    Shiny
    Visualization

    Picture
    Picture
    Picture
    200x200 Machine Learning Expert
    Picture
Powered by Create your own unique website with customizable templates.
  • Data Science
  • Machine Learning
  • Deep Learning
  • Artificial Intelligence
  • Big Data
  • Computer Vision
  • Blog
  • Advertise with us