Hi,
I am analyzing the name "Liam" by ethnicity in New York. I exported the following data set into excel and imported into R.
https://data.cityofnewyork.us/Health/Popular-Baby-Names/25th-nujf/data
Then, I attempted to create a pie chart. The pie chart percentages do not correspond to their respective pie slices. How should I edit my code?
I have posted my code below. Thanks for your help.
NY_Popular_Baby_Names <- read_excel("NY_Popular_Baby_Names.xlsx")
View(NY_Popular_Baby_Names)
library(ggplot2)
library(dplyr)
library(scales)
library(tidyr)
library(magrittr)
sequence_of_years_NY<- seq(from = 2000, to = 2018, by = 1)
NY_Popular_Baby_Names.df <- NY_Popular_Baby_Names
colnames(NY_Popular_Baby_Names.df) <- c("year","gender","ethnicity","name","count","rank")
NY_Popular_Baby_Names.df$total <- sum(NY_Popular_Baby_Names.df$count)
NY_Popular_Baby_Names.df$perc <- NY_Popular_Baby_Names.df$count/NY_Popular_Baby_Names.df$total
Liam_NY.df <- NY_Popular_Baby_Names.df[which(NY_Popular_Baby_Names.df$name == "Liam"),]
Liam_2013.df <- Liam_NY.df[which(Liam_NY.df$year == 2013),]
Liam_2013.df <- Liam_2013.df[-c(1,6),]
Liam_2013.df$total <- sum(Liam_2013.df$count)
Liam_2013.df$perc <- Liam_2013.df$count/Liam_2013.df$total
blank_theme <- theme_minimal()+
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.border = element_blank(),
panel.grid=element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(size=14, face="bold")
)
ggplot(Liam_2013.df, aes(x="", y=perc, fill=ethnicity))+
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start=0) + scale_fill_brewer("Blues")+
blank_theme + theme(axis.text.x=element_blank())+
geom_text(aes(y = perc, label = percent(perc)), size=5) +
labs(title="William in New York by Ethnicity", x = "", y="Percentage of Total",
caption="Source: Data from the New York Department of Health")