title: “MiniLesson1” author: “Tin Le” date: “September 14, 2017” output: html_document —
Mini Lesson 1 Tin Le 9/15/2017
Creating a pie graph using coord_polar() function
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
glimpse(mpg)
## Observations: 234
## Variables: 11
## $ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "...
## $ model <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 qua...
## $ displ <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0,...
## $ year <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1...
## $ cyl <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6...
## $ trans <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)...
## $ drv <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4",...
## $ cty <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 1...
## $ hwy <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 2...
## $ fl <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p",...
## $ class <chr> "compact", "compact", "compact", "compact", "comp...
theme_set(theme_classic())
#Creating Frequency table for transmission
df <- as.data.frame(table(mpg$trans))
colnames(df) <- c("trans", "count") #Creating column names
#creating ggplot
pie <- ggplot(df, aes(x = "", y = count, fill = factor(trans))) +
geom_bar(width = 1, stat = "identity") +
#adding simple changes to graph for readability
theme(axis.line = element_blank(),
plot.title = element_text(hjust=0.5)) +
#adding athestics for graph
labs(fill = "trans",
x = NULL,
Y = NULL,
title = "Pie Chart of Trans",
caption = "Source: mpg")
print(pie)
The current graph is not what was wanted but has good information. Data seems to be stacked. and adding percentages.
#dplyr
df2 <- mpg %>%
group_by(trans) %>% #
summarise(count = n()) %>%
mutate(rel.percentage = paste0(round(100 * count/sum(count), 0), "%")) #source: stackoverflow
print(df2)
## # A tibble: 10 x 3
## trans count rel.percentage
## <chr> <int> <chr>
## 1 auto(av) 5 2%
## 2 auto(l3) 2 1%
## 3 auto(l4) 83 35%
## 4 auto(l5) 39 17%
## 5 auto(l6) 6 3%
## 6 auto(s4) 3 1%
## 7 auto(s5) 3 1%
## 8 auto(s6) 16 7%
## 9 manual(m5) 58 25%
## 10 manual(m6) 19 8%
coord_polar function only has a few arguments: theta: the variable to maple angles to(x or Y) start: where at the point the pie chart begins and ends. Defaults to 12 o’clock direction: clockwise(1) or counter clockwise(-1)
#creating ggplot
g1 <- ggplot(transform(transform(df2, count = count/sum(count)), labPos=cumsum(count)-count/2),aes(x = "", y = count, fill = trans)) + #Changes count numbers to percentages #stackoverflow
geom_bar(width = 1, stat = "identity") +
#adding simple changes to graph for readability
theme(axis.line = element_blank(),
plot.title = element_text(hjust=0.5)) +
#adding athestics for graph
labs(fill = "trans",
x = NULL,
Y = NULL,
title = "Pie Chart of Trans",
caption = "Source: mpg"
)
print(pie)
#creates a pie chart
pie1 = g1 + coord_polar(theta = "y")
print(pie1)