banner



How To Draw Pie Chart In R

ggplot2 pie nautical chart : Quick start guide - R software and data visualization

  • Simple pie charts
  • Alter the pie chart fill colors
  • Create a pie chart from a factor variable
  • Customized pie charts
  • Infos

This R tutorial describes how to create a pie chart for information visualization using R software and ggplot2 package.

The office coord_polar() is used to produce a pie chart, which is but a stacked bar chart in polar coordinates.

Simple pie charts

Create some data :

                df <- data.frame(   group = c("Male", "Female", "Child"),   value = c(25, 25, 50)   ) caput(df)              
                ##    group value ## 1   Male person    25 ## ii Female    25 ## 3  Child    50              

Employ a barplot to visualize the data :

                library(ggplot2) # Barplot bp<- ggplot(df, aes(x="", y=value, make full=group))+ geom_bar(width = 1, stat = "identity") bp              

ggplot2 pie chart for data visualization in R software

Create a pie chart :

                pie <- bp + coord_polar("y", first=0) pie              

ggplot2 pie chart for data visualization in R software

Change the pie nautical chart fill colors

It is possible to change manually the pie chart make full colors using the functions :

  • scale_fill_manual() : to apply custom colors
  • scale_fill_brewer() : to use color palettes from RColorBrewer bundle
  • scale_fill_grey() : to use grey color palettes
                # Use custom color palettes pie + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))              

ggplot2 pie chart for data visualization in R software

                # use brewer color palettes pie + scale_fill_brewer(palette="Dark2")              

ggplot2 pie chart for data visualization in R software

                pie + scale_fill_brewer(palette="Blues")+   theme_minimal()              

ggplot2 pie chart for data visualization in R software

                # Use gray calibration pie + scale_fill_grey() + theme_minimal()              

ggplot2 pie chart for data visualization in R software

Read more on ggplot2 colors here : ggplot2 colors

Create a pie chart from a cistron variable

PlantGrowth data is used :

                caput(PlantGrowth)              
                ##   weight group ## one   four.17  ctrl ## 2   5.58  ctrl ## iii   5.xviii  ctrl ## 4   six.11  ctrl ## 5   four.l  ctrl ## half-dozen   iv.61  ctrl              

Create the pie chart of the count of observations in each group :

                ggplot(PlantGrowth, aes(x=cistron(1), fill=group))+   geom_bar(width = 1)+   coord_polar("y")              

ggplot2 pie chart for data visualization in R software

Customized pie charts

Create a bare theme :

                blank_theme <- theme_minimal()+   theme(   axis.championship.x = element_blank(),   axis.title.y = element_blank(),   console.border = element_blank(),   panel.filigree=element_blank(),   axis.ticks = element_blank(),   plot.title=element_text(size=14, confront="bold")   )              
  1. Apply the blank theme
  2. Remove axis tick marking labels
  3. Add text annotations : The parcel scales is used to format the labels in percent
                # Use bare theme library(scales) pie + scale_fill_grey() +  blank_theme +   theme(centrality.text.x=element_blank()) +   geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]),              label = per centum(value/100)), size=v)              

ggplot2 pie chart for data visualization in R software

                # Use brewer palette pie + scale_fill_brewer("Dejection") + blank_theme +   theme(axis.text.x=element_blank())+   geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]),                  characterization = percent(value/100)), size=v)              

ggplot2 pie chart for data visualization in R software

Infos

This analysis has been performed using R software (ver. three.1.2) and ggplot2 (ver. 1.0.0)


Enjoyed this article? I'd be very grateful if yous'd help information technology spread by emailing information technology to a friend, or sharing information technology on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank y'all and please don't forget to share and comment below!!

Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa improvidence en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi united nations peu d'flirtation avec les like ci-dessous ... Merci et due north'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!



Source: http://www.sthda.com/english/wiki/ggplot2-pie-chart-quick-start-guide-r-software-and-data-visualization

Posted by: uptongramemptere.blogspot.com

0 Response to "How To Draw Pie Chart In R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel