knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Dataset

This dataset contains information about average fruit consumption per person as well as fruit consumption by fruit types in each country in the world. (From OurWroldInData) \ \ Fruit consumption by fruit type: Average fruit consumption per person, differentiated by fruit types, measured in kilograms per year. Click Here\ Fruit consumption per capita: Average fruit consumption per person, measured in kilograms per year. Click Here \

library(dplyr)
library(FunctionPkg)
Pivot_Data_Country <- FunctionPkg::Pivot_Data_Country
Data <- FunctionPkg::FruitData
Top_10_Countries <- Data %>% 
  dplyr::arrange(desc(Fruit))%>%
  utils::head(15)

Pivot_Data <- Data%>%
  dplyr::select("Country", 'Code', "Bananas" , "Oranges", "Apples" , "Grapes", "Plantains")%>%
  tidyr::pivot_longer(
    cols = c("Bananas" , "Oranges", "Apples" , "Grapes", "Plantains"),
    names_to = 'FruitType',
    values_to = 'Consumption'
  ) 

Countries <- Data %>% dplyr::arrange(desc(Fruit)) %>% utils::head(10)%>% dplyr::select(Country)


Pivot_Data_Country <-Pivot_Data %>% 
  dplyr::left_join(Data %>% dplyr::select(Country, Fruit), on = Country) %>% 
  dplyr::filter(Country %in% Countries$Country)%>% dplyr::arrange(desc(Fruit))

\ \

Top_10_Countries <- Data %>% 
  dplyr::arrange(desc(Fruit))%>%
  utils::head(15)

\ \ \

Data$hover <- with(Data, paste( '<b>' , Country , '</b>' , '<br>', 
                                           "Bananas", '<b>' , Bananas,'</b>', "kg/year", "<br>", 
                                           "Dates", '<b>' , Dates,'</b>', "kg/year", "<br>",
                                            "Citrus", '<b>' , Citrus,'</b>', "kg/year", "<br>",
                                            "Oranges", '<b>' , Oranges,'</b>', "kg/year", "<br>",
                                           "Apples", '<b>' , Apples,'</b>', "kg/year", "<br>",
                                        "Lemons", '<b>' , Lemons,'</b>', "kg/year", "<br>",
                                        "Grapes", '<b>' , Grapes,'</b>', "kg/year", "<br>",
                                        "Grapefruit", '<b>' , Grapefruit,'</b>', "kg/year", "<br>",
                                "Pineapples", '<b>' , Pineapples,'</b>', "kg/year", "<br>",
                                "Plantains", '<b>' , Plantains,'</b>', "kg/year", "<br>",
                                "Other", '<b>' , Other,'</b>', "kg/year", "<br>"))
D1 <- Data%>%
  dplyr::select(-c("Code", "hover"))%>%
  tidyr::pivot_longer(
    cols = (-Country),
    names_to = 'FruitType',
    values_to = 'value'
  )

D2 <- D1 %>% 
  dplyr::left_join(Data %>% dplyr::select(Country, Code, Fruit), on = Country)


countryKey <- plotly::highlight_key(Data, ~Country)

base <- plotly::plot_ly(countryKey, color = I("black")) %>% 
  dplyr::group_by(Country)

plot11 <- D2 %>%
      plotly::highlight_key(~Country) %>%
     plotly::plot_ly(type='choropleth', 
                      locations=D2$Code, 
                      z=D2$Fruit,  
                      #text=Data$hover, 
                      colorscale="Viridis") %>% 
  plotly::layout(title = 'Fruit Consumption')  %>% 
    plotly::highlight("plotly_click", persistent = FALSE, dynamic = TRUE)

plot22 <- D2 %>% 
    plotly::plot_ly(x = ~Fruit, y = ~value)  %>%
    plotly::add_bars() %>%
  #layout(barmode = "overlay") %>%
  plotly::highlight("plotly_click")

Top 10 Countries with Most Fruit Consumption (Interactive Graphs)

countryKey <- plotly::highlight_key(Pivot_Data_Country, ~Country)


line <- countryKey  %>% 
  plotly::plot_ly( x = ~ reorder(Country, -Fruit), y = ~Fruit, 
name = 'Fruit total', type = 'scatter', mode = 'lines+markers',
  text = paste("<br>Country: ", Pivot_Data_Country$Country,"<br>",
                 Pivot_Data_Country$Fruit, 'kg/year'),
  hoverinfo = 'text') %>% 
  plotly::layout(title= "Which Country Eats the Most Fruits Per Capita?")%>% 
  plotly::highlight("plotly_click")


bar <- countryKey %>% 
  plotly::plot_ly()  %>%
  plotly::add_bars(x = ~FruitType, y = ~Consumption, color = 'green') %>% 
  plotly::layout(barmode = "overlay")

crosstalk::bscols(line,bar) 
# library(crosstalk)
# countryKey <- highlight_key(Pivot_Data, ~Country)
# 
# map <- countryKey %>%
#   plot_ly()%>%
#   group_by(Country, Code) %>%
#   summarise(Consumption = sum(Consumption, na.rm = TRUE)) %>%
#   add_choropleth(
#     locations=~Code, 
#     z=~Consumption,  
#     #text=Data$hover, 
#     colorscale="Viridis") %>% 
#   layout(title = 'Fruit Consumption')  %>% 
#   highlight("plotly_click", persistent = FALSE, dynamic = TRUE)
# 
# barplot <- countryKey %>% 
#   plot_ly(x = ~FruitType, y = ~Consumption)  %>%
#   add_bars() 
#   #layout(barmode = "overlay")
# 
# dotplot <- countryKey %>%
#   plot_ly()%>%
#   group_by(Country, Code) %>%
#   summarise(Consumption = sum(Consumption, na.rm = TRUE)) %>%
#   add_markers(
#     x = ~Country, y = ~Consumption
#   )%>%
#  highlight("plotly_click")
# 
# crosstalk::bscols(dotplot,barplot) 

\ \

Fruit Consumption by Country

fig3 <-  plotly::plot_ly(Data, type='choropleth', 
                      locations=Data$Code, 
                      z=Data$Fruit,  
                      text=Data$hover, 
                      colorscale="Viridis") %>% 
  plotly::layout(title = 'Fruit Consumption by fruit type/country(2013)')

fig3


haoping-he/FunctionPkg documentation built on April 14, 2022, 10:46 a.m.