#Infographics of McDonald’s Items using #ggplot2 #grid #extrafont

Introduction and Data

McDonald’s is a world wide fast food franchise which was founded in 1940. It started off as a barbeque stall in 1940. It now has branches in more than 100 countries.

It serves more than 68 million customers daily across 37000 outlets around the world.It is also the second largest private employer. In this post, we will be looking at the dataset submitted at Kaggle.
I came across a post on R-Bloggers on creating infographics using R. Being excited by the prospects of creating a similar infographic, I got to work.

Data Import and Characteristics


food <- read.csv("menu.csv",stringsAsFactors = F,header=T)

plotTheme <- function(base_size = 12) {
text = element_text( color = "#ffc300"),
plot.title = element_text(size = 10,colour = "#ffc300",hjust=0.5),
plot.subtitle = element_text(face="italic"),
plot.caption = element_text(hjust=0),
panel.background = element_rect(fill="#dd1021",colour="#dd1021"),
panel.grid.major = element_line("grey80", size = 0.1),
panel.grid.minor = element_blank(),
strip.text = element_text(size=12),
axis.title = element_text(size=8),
axis.text = element_text(size=8),
axis.title.x = element_text(hjust=1),
axis.title.y = element_text(hjust=1),
plot.background = element_rect(fill="#dd1021"),
legend.background = element_blank(),
legend.title = element_text(colour = "black", face = "bold"),
legend.text = element_text(colour = "black", face = "bold"),
strip.background = element_rect(fill = "#ffc300"),
panel.grid.major.y = element_line(colour = "#ffc300"),
panel.grid.major.x = element_line(colour = "#ffc300"),
axis.text.x = element_text(colour="#ffc300"),
axis.text.y = element_text(colour="#ffc300")


names(food) <- c("Category", "Item", "Serving.Size", "Calories", "Calories.from.Fat",
"Total.Fat", "Daily_Fat_Value", "Saturated.Fat", "Daily_Sat._Fat_Value",
"Trans.Fat", "Cholesterol", "Daily_Chol_Value", "Sodium",
"Daily_Sodium_Value", "Carbohydrates", "Daily_Carbohydrate_Value",
"Dietary_Fiber", "Daily_Dietary_Fiber", "Sugars", "Protein",
"Daily_Vitamin_A_value", "Daily_Vitamin_C_value", "Daily_Calcium_Value",

The data is made of 24 variables and 260 observations. To imitate the color scheme of McDonald’s we will use the #dd1021 hexadecimal red and #ffc300 hexadecimal yellow.

Mean Content Daily Value

p1 <-food %>% group_by(Category)%>%
vit_a = mean(Daily_Vitamin_A_value,na.rm=T),
vit_c = mean(Daily_Vitamin_C_value,na.rm=T),
iron = mean(Daily_Iron_Value,na.rm=T),
calcium = mean(Daily_Calcium_Value,na.rm=T),
carbs = mean(Daily_Carbohydrate_Value,na.rm=T),
dietary_fiber = mean(Daily_Dietary_Fiber,na.rm=T),
sodium= mean(Daily_Sodium_Value,na.rm=T),
cholestrol = mean(Daily_Chol_Value,na.rm=T)

) %>%
gather(type,value,2:9) %>%
facet_wrap(~type,ncol=3)+plotTheme()+labs(title="Average Daily Content")+theme(axis.text.x = element_text(size=4,colour ="#ffc300"))+coord_flip()

The above plot shows the average daily content of the different kinds of meals that you can buy at McDonald's. The breakfasts at McDOnald's contain the highest average amount of cholestrol. This is followed by the beef and pork type of McDonald's meal.The meal which has the least average amount of cholestrol is the beverage.

Customers who are aiming at keeping their sodium levels at check should avoid consuming beef and pork,breakfast and chicken and fish type of meals.

Salads contain the highest average dietary fiber content. Overall, they are the healthiest items to buy a McDonald's.

Mean Sugar,Sodium,Carbs,Cholestrol,Calories

p2<-food %>% group_by(Category)%>%
sugar = mean(Sugars,na.rm=T),
sodium = mean(Sodium,na.rm=T),
carbohydrates = mean(Carbohydrates,na.rm=T),
cholesterol = mean(Cholesterol,na.rm=T),
Calories = mean(Calories,na.rm=T),
protein = mean(Protein,na.rm=T)

) %>%
gather(type,value,2:7) %>%
facet_wrap(~type,ncol=3)+plotTheme()+labs(title="Mean Sugar,Sodium,Carbs,Cholestrol,Calories")+theme(axis.text.x = element_text(size=4))+coord_flip()

The above plot shows the mean sugar,sodium,protein,cholesterol,carbohydrate and calories content.Chicken And Fish contain the highest average amount of calories. It also contains the highest amount of average sodium content.

Anyone who is trying to keep their cholesterol under check should avoid consuming the breakfast meals.

Fats Present

p3<-food %>% group_by(Category)%>%
totalfat = mean(Total.Fat,na.rm=T),
saturatedfat = mean(Saturated.Fat,na.rm=T)

) %>%
gather(type,value,c(2,3)) %>%
facet_wrap(~type,ncol=3)+plotTheme()+labs(title="Fats Present")+theme(axis.text.x = element_text(size=6,colour="#ffc300"),aspect.ratio = 3/4)+coord_polar()

Distribution of Percentage Calories from Fat

temp <- food %>% na.omit() %>% mutate(per_fat = 100*(Calories.from.Fat/Calories))
p4 <- ggplot(data=temp,aes(x=per_fat))+
geom_histogram(position="identity",alpha=0.6,fill="#ffc300")+plotTheme()+labs(title="Percentage Calories from Fat",x="% Fat",y="Count",caption="")+facet_wrap(~Category)


A larger proportion of breakfast items have high fat content. This is followed by the beef and pork items.

Combining the Plots

Before we combine the plots, we need to download the McDonald’s font. This font is called McLetters and can be found here.
This file will be in the .ttf format. This file has to be copied to the fonts folder for the computer system.

After we have done that, we need to import this font to our session. To do this run library(extrafont) and font_import(). This imports the .ttf file that you have downloaded.

#blank <- grid.rect(gp=gpar(col="white"))

# Create a an ordered list of the plots we want to use, including 'blank'
# which we will place in the plot to create white spac
#font_import("C:\Windows\Fonts\Insaniburger Regular.ttf")
png("mcd.png", width = 10, height = 20, units = "in", res = 500)

pushViewport(viewport(layout = grid.layout(5, 5)))

grid.text("M", y = unit(1, "npc"), x = unit(0.5, "npc"), vjust = 1.5, hjust = .5, gp = gpar(fontfamily = "McLetters", col = "#ffc300", cex = 8, alpha = 0.9))
grid.rect(gp = gpar(fill = "#ffc300", col = "#ffc300"), x = unit(0.5, "npc"), y = unit(0.83, "npc"), width = unit(1, "npc"), height = unit(0.11, "npc"))

grid.text("DATA INFO", vjust = 0, hjust = 0, x = unit(0.01, "npc"), y = unit(0.86, "npc"), gp = gpar(fontfamily = "McLetters", col = "#ffc300", cex = 1.2))
"Data Source",
"Date", sep = "\n"), vjust = 0, hjust = 0, x = unit(0.01, "npc"), y = unit(0.83, "npc"), gp = gpar(fontfamily = "McLetters", col = "#dd1021", cex = 1))
"Pradeep Adhokshaja",
"17-March-2017", sep = "\n"), vjust = 0, hjust = 0, x = unit(0.15, "npc"), y = unit(0.83, "npc"), gp = gpar(fontfamily = "McLetters", col = "#dd1021", cex = 1))

print(p3, vp = vplayout(5, 1:5))
print(p4, vp = vplayout(3, 1:5))
print(p1, vp = vplayout(4, 1:5))
print(p2,vp = vplayout(2,1:5))
#grid.rect(gp = gpar(fill = "#dd1021", col = "#dd1021"), x = unit(0.5, "npc"), y = unit(0.82, "npc"), width = unit(1, "npc"), height = unit(0.11, "npc"))


Thank You for reading! If you have any feedback ,please feel free to leave a comment below. You can also reach me at padhokshaja@gmail.com or on Twitter at @PAdhokshaja.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s