kaggle:R可视化分析金拱门餐厅食物营养性(一)
感谢关注天善智能,走好数据之路↑↑↑
欢迎关注天善智能,我们是专注于商业智能BI,人工智能AI,大数据分析与挖掘领域的垂直社区,学习,问答、求职一站式搞定!
本文作者:天善智能社区专家 邬书豪
天善智能社区地址:https://www.hellobi.com/
上市公司全球大型跨国连锁快餐巨头金拱门餐厅,在全世界上大约拥有32000家分店,分布在全球121个国家和地区!我们偶尔吃的这家快餐,它滴食物营养性咋样,今天我们就来对kaggle竞争上的这份数据做一个可视化分析!
公众号后台回复“金拱门”下载数据集。
理解数据变量
Category(食物类别) 如Breakfast、Beef & Pork等等
Item(食物名称)
Serving Size(份量)
Calories(卡路里)
Calories from Fat(来自脂肪的热量)
Total Fat(总脂肪)
Saturated Fat(饱和脂肪)
Trans Fat(反式脂肪)
Cholesterol(胆固醇)
Sodium(钠)
Carbohydrates碳水化合物
Dietary Fiber(膳食纤维)
Sugars(糖)
Protein(蛋白)
Vitamin A (% Daily Value)、Vitamin C (% Daily Value)
Calcium (% Daily Value)(钙)
Iron (% Daily Value)(铁)
分析要求是:
1、麦当劳食品的平均卡路里是多少?
2、饮用多少饮料,例如苏打水或者咖啡,会加强总热量的摄入?
3、购买烤鸡肉三明治,而不是炸鸡肉三明治,是否会更加有营养?
4、怎么从菜单中选择最少数量的食品来你满足一天的营养需求?
#加载包,读入数据,查看数据形式suppressMessages(library(ggplot2))suppressMessages(library(dplyr))suppressMessages(library(plotly))suppressMessages(library(RColorBrewer))suppressMessages(library(devtools))suppressMessages(library(yarrr))suppressMessages(library(tidyr))suppressMessages(library(gridExtra))suppressMessages(library(viridis))menu<-read.csv("D:/金拱门食物数据/menu.csv")str(menu)
#查看缺失值:library(mice)md.pattern(menu)
缺失值情况
从上图可以看出,menu数据没有缺失值。
#可视化分析食物类别绘图(图1)m <- list(l = 50,r = 50,b = 100,t = 100,pad = 4)marker = list(color = brewer.pal(9, "Set1"))p <- plot_ly(x = menu$Category,marker = list(color = '#45171D'),type = "histogram")%>% layout(xaxis = list(title = ""),yaxis = list(title = ""),autosize = T)p
图1:食物类别
从上图可以看出,食物类别中数目最多的是Coffee & Tea,其次是Breakfast
#食物卡路里与糖分的关系绘图(图2)d1<-menu%>%ggplot(aes(x=Sugars,y=Calories))+stat_density_2d(geom = "polygon", aes(fill = ..level..), h=c(100,1500) ,contour = TRUE)+scale_fill_viridis(option="magma")#碳水化合物与卡路里的可视化绘图(图3)d2<-menu%>%ggplot(aes(x=Carbohydrates,y=Calories))+stat_density_2d(geom = "tile", aes(fill = ..density..), h=c(100,1500) ,contour = FALSE)+scale_fill_viridis(option="magma")#钠与卡路里的可视化绘图(图4)d3<-menu%>%ggplot(aes(x=Sodium,y=Calories))+stat_density_2d(geom = "tile", aes(fill = ..density..), h=c(3000,1500) ,contour = FALSE)+scale_fill_viridis(option="magma")#胆固醇与卡路里的可视化绘图(图5)d4<-menu%>%ggplot(aes(x=Cholesterol,y=Calories))+stat_density_2d(geom = "tile", aes(fill = ..density..), h=c(600,1500) ,contour = FALSE)+scale_fill_viridis(option="magma")#膳食纤维与卡路里的可视化绘图(图6)d5<-menu%>%ggplot(aes(x=Dietary.Fiber,y=Protein))+scale_x_continuous(limits=c(0,8),expand=c(0,0))+stat_density_2d(geom = "tile", aes(fill = ..density..), h=c(5,70) ,contour = FALSE)+scale_fill_viridis(option="magma")
图2
图3
图4
图5
图6
#卡路里的热量分布是结果变量,取决于其他营养素变量。#绘图脂肪的热量与卡路里之间的关系(图7)p1 <- plot_ly(menu,x = ~Calories,type = "histogram",histnorm = "probability",name="Calorie",alpha=0.6)%>% add_histogram(x = ~Calories.from.Fat,name="Calorie From Fat",alpha=0.6) %>% layout(barmode = "overlay")p1
图7
#食物类别与卡路里的可视化绘图(图8)new_col<-c("grey50", "blue","hotpink","Magenta","seagreen","violet","brown","maroon","navyblue")plot_ly(x = menu$Category, y=menu$Calories,color = menu$Category,colors =new_col , type = "box")%>% layout(xaxis = list(title = ""),yaxis = list(title = "Calories"),showlegend=FALSE,autosize = T)
通过箱线图,一目了然!我们可以很清晰的看到每种食物类别的卡路里(第一分位数,第三分位数,最大值,最小值,中位数)
#食物类别与蛋白质含量的可视化绘图(图9)plot_ly(x = menu$Category, y=menu$Protein,color = menu$Category,colors =new_col , type = "box")%>% layout( xaxis = list(title = ""), yaxis = list(title = ""), showlegend=FALSE, autosize = T)
通过箱线图,一目了然!我们可以很清晰的看到食物类别中蛋白质的(第一分位数,第三分位数,最大值,最小值,中位数)
#食物种类(Category)之Chicken & Fish类与Item的蛋白质(Protein)含量可视化绘图#图10library(ggrepel)menu %>% select(Category,Item,Protein,Total.Fat)%>%arrange(desc(Protein))%>%filter(Category =="Chicken & Fish")%>%ggplot(aes(x=Item,y=Protein,col=Item))+geom_point(size=3)+theme(legend.position = "none",axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank())+geom_label_repel(aes(label=substr(Item,1,20)),size=2)+labs(title="High Protein/Fat Item in Chicken & Fish Category")+geom_bar(aes(y=Total.Fat),alpha=0.5,stat="identity")
图10
#碳水化合物含量与食物种类的可视化分析(图11)plot_ly(x = menu$Category, y=menu$Carbohydrates,color = menu$Category,colors =new_col , type = "box") %>% layout( xaxis = list(title = ""), yaxis = list(title = ""), showlegend=FALSE, autosize = T)
图11
通过箱线图,可以很清晰的看到各食物类别中的碳水化合物含量(第一分位数,第三分位数,最大值,最小值,中位数)
#碳水化合物与碳水化合物之间的散点图,卡路里大于500为红色,卡路里小于500为蓝色(图12)p2<-plot_ly(x=menu$Calories, y=menu$Carbohydrates, type="scatter", mode = "markers" , marker=list( color=ifelse(menu$Calories>500,"red","blue") , opacity=0.5 , size=20) ) p2
图12
#食物类别与总脂肪的箱线图绘图,RStudio里可见箱线图数值(图13)plot_ly(x = menu$Category, y=menu$Total.Fat,color = menu$Category,colors =new_col , type = "box") %>% layout( xaxis = list(title = ""), yaxis = list(title = ""), showlegend=FALSE, autosize = T)
图13
#总脂肪、脂肪的热量,饱和脂肪三维图比较分析绘图(图14)m1<-menu%>%select(Total.Fat,Calories.from.Fat,Saturated.Fat)m2<-as.matrix(m1)colnames(m2)<-NULL#m2p3 <- plot_ly(z=~m2) %>% add_surface()%>%layout(scene = list(xaxis = list(title = 'Total.Fat'), yaxis = list(title = 'Calories from Fat'), zaxis = list(title = 'Saturated Fat')))p3
未完待续。
天善智能社区地址:https://www.hellobi.com/