Nations - Chart 2

Load Libraries

library(rvest)
## Loading required package: xml2
library(tidyverse)
## -- Attaching packages -------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.1     v purrr   0.3.4
## v tibble  3.0.1     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ----------------------------------- tidyverse_conflicts() --
## x dplyr::filter()         masks stats::filter()
## x readr::guess_encoding() masks rvest::guess_encoding()
## x dplyr::lag()            masks stats::lag()
## x purrr::pluck()          masks rvest::pluck()
library(ggsci)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(DT)
library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
library(RColorBrewer)
library(ggplot2)

Load Dataset

setwd("C:/Users/Valued Customer/Desktop/Lovebug/Montgomery College/DATA 110/Week 4")
nations <- read_csv("nations.csv")
## Parsed with column specification:
## cols(
##   iso2c = col_character(),
##   iso3c = col_character(),
##   country = col_character(),
##   year = col_double(),
##   gdp_percap = col_double(),
##   population = col_double(),
##   birth_rate = col_double(),
##   neonat_mortal_rate = col_double(),
##   region = col_character(),
##   income = col_character()
## )

Highcharter

# prepare data
regions <- nations %>%
  group_by(year,region) %>%
  summarize(gdp_percap = sum(gdp_percap, na.rm = TRUE)) %>%
  arrange(year,region)
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
highchart () %>%
  hc_add_series(data = regions,
                   type = "area",
                   hcaes(x = year,
                   y = gdp_percap, 
                   group = region)) %>% 
  hc_title(text="GDP Per Capita by Region") 
## Warning: `parse_quosure()` is deprecated as of rlang 0.2.0.
## Please use `parse_quo()` instead.
## This warning is displayed once per session.
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: `select_()` is deprecated as of dplyr 0.7.0.
## Please use `select()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: `as_data_frame()` is deprecated as of tibble 2.0.0.
## Please use `as_tibble()` instead.
## The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: `rename_()` is deprecated as of dplyr 0.7.0.
## Please use `rename()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Changing Color Palette to RColorBrewer “Set 2”

# set color palette
cols <- brewer.pal(7, "Set2")
# stacked area chart
highchart () %>%
  hc_add_series(data = regions,
                   type = "area",
                   hcaes(x = year,
                   y = gdp_percap, 
                   group = region)) %>%
  hc_colors(cols) %>% 
  hc_chart(style = list(fontFamily = "Georgia",
                        fontWeight = "bold")) %>%
hc_plotOptions(series = list(stacking = "normal",
                               marker = list(enabled = FALSE,
                               states = list(hover = list(enabled = FALSE))),
                               lineWidth = 0.5,
                               lineColor = "white")) %>%
  hc_xAxis(title = list(text="Year")) %>%
  hc_yAxis(title = list(text="GDP ($ trillion)")) %>%
  hc_title(text="GDP Per Capita by Region") %>% 
  hc_legend(align = "right", verticalAlign = "middle",
            layout = "vertical") %>%
  hc_tooltip(enabled = FALSE)

ggplot

nations2 <- nations %>%
  group_by(region) %>%
  group_by(year)
nations3 <- nations2 %>% 
  summarise(sum = sum(gdp_percap, na.rm = TRUE))
## `summarise()` ungrouping output (override with `.groups` argument)

Trying to figure out fill

p1 <- nations2 %>% 
  ggplot(aes(year, gdp_percap)) +
  geom_area()+
  scale_fill_brewer(palette = "Set2")
p1 
## Warning: Removed 766 rows containing missing values (position_stack).

p2 <- nations2 %>% 
  ggplot(aes(year, gdp_percap, fill=region)) +
  geom_area()+
  scale_fill_brewer(palette = "Set2")
p2 
## Warning: Removed 766 rows containing missing values (position_stack).

p3 <- nations2 %>% 
  ggplot(aes(year, gdp_percap)) +
  geom_area()+
  scale_fill_brewer(palette = "Set2")
p3 
## Warning: Removed 766 rows containing missing values (position_stack).

p2 <- nations2 %>% 
  ggplot(aes(year, gdp_percap, color = region)) +
  geom_area()+
  scale_fill_brewer(palette = "Set2")
p2 
## Warning: Removed 766 rows containing missing values (position_stack).

p3 <- nations2 %>% 
  ggplot(aes(year, gdp_percap, color = region, fill=region)) +
  geom_area()+
  scale_fill_brewer(palette = "Set2")
p3 
## Warning: Removed 766 rows containing missing values (position_stack).

p4 <- nations2 %>% 
  ggplot(aes(year, gdp_percap, color = region, fill=region)) +
  geom_area()+
  geom_point(aes(year, gdp_percap))+
  scale_fill_brewer(palette = "Set2")
p4 
## Warning: Removed 766 rows containing missing values (position_stack).
## Warning: Removed 766 rows containing missing values (geom_point).

p5 <- nations2 %>% 
  ggplot(aes(year, gdp_percap, color = region, fill=region)) +
  geom_area()+
  geom_point(aes(year, gdp_percap))+
  geom_smooth()
  scale_fill_brewer(palette = "Set2")
## <ggproto object: Class ScaleDiscrete, Scale, gg>
##     aesthetics: fill
##     axis_order: function
##     break_info: function
##     break_positions: function
##     breaks: waiver
##     call: call
##     clone: function
##     dimension: function
##     drop: TRUE
##     expand: waiver
##     get_breaks: function
##     get_breaks_minor: function
##     get_labels: function
##     get_limits: function
##     guide: legend
##     is_discrete: function
##     is_empty: function
##     labels: waiver
##     limits: NULL
##     make_sec_title: function
##     make_title: function
##     map: function
##     map_df: function
##     n.breaks.cache: NULL
##     na.translate: TRUE
##     na.value: NA
##     name: waiver
##     palette: function
##     palette.cache: NULL
##     position: left
##     range: <ggproto object: Class RangeDiscrete, Range, gg>
##         range: NULL
##         reset: function
##         train: function
##         super:  <ggproto object: Class RangeDiscrete, Range, gg>
##     rescale: function
##     reset: function
##     scale_name: brewer
##     train: function
##     train_df: function
##     transform: function
##     transform_df: function
##     super:  <ggproto object: Class ScaleDiscrete, Scale, gg>
p5 
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
## Warning: Removed 766 rows containing non-finite values (stat_smooth).
## Warning: Removed 766 rows containing missing values (position_stack).
## Warning: Removed 766 rows containing missing values (geom_point).

set2 <- scale_fill_brewer(palette = "Set2")
#p6 <- nations2 %>% 
  #ggplot(aes(year, gdp_percap, color = region, fill=region)) +
  #geom_area(aes(fill = nations2$region ))+
  #geom_point(aes(year, gdp_percap))
  
#p6