Untitled

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(readr)
disease_democ <- read_csv("C:/Users/gopin/Downloads/disease_democ.csv")
Rows: 168 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): country, income_group
dbl (2): democ_score, infect_rate

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
tail(disease_democ)
# A tibble: 6 × 4
  country       income_group        democ_score infect_rate
  <chr>         <chr>                     <dbl>       <dbl>
1 Turkey        Upper middle income        54.8          39
2 Panama        Upper middle income        46            40
3 Gabon         Upper middle income        34.6          41
4 Venezuela, RB Upper middle income        41.6          42
5 Colombia      Upper middle income        44.8          43
6 Brazil        Upper middle income        37.2          45

Activity 1

unique(disease_democ$income_group)
[1] "High income: non-OECD" "High income: OECD"     "Low income"           
[4] "Lower middle income"   "Upper middle income"  
n_distinct(disease_democ$country)
[1] 168

In the first line of code they use the $ sign to only show the income group section of the disease_democ dataset. In the second line of code they do the same thing to only show the country section of the dataset.

Activity 2

disease_democ|>filter(country == "Qatar")|>pull(income_group)
[1] "High income: non-OECD"
names(postscriptFonts())
 [1] "serif"                "sans"                 "mono"                
 [4] "AvantGarde"           "Bookman"              "Courier"             
 [7] "Helvetica"            "Helvetica-Narrow"     "NewCenturySchoolbook"
[10] "Palatino"             "Times"                "URWGothic"           
[13] "URWBookman"           "NimbusMon"            "NimbusSan"           
[16] "URWHelvetica"         "NimbusSanCond"        "CenturySch"          
[19] "URWPalladio"          "NimbusRom"            "URWTimes"            
[22] "URW2Helvetica"        "URW2HelveticaItalic"  "URW2Times"           
[25] "NimbusMonoPS"         "ArialMT"              "ComputerModern"      
[28] "ComputerModernItalic" "Japan1"               "Japan1HeiMin"        
[31] "Japan1GothicBBB"      "Japan1Ryumin"         "Korea1"              
[34] "Korea1deb"            "CNS1"                 "GB1"                 
disease_democ_chart <-ggplot(disease_democ, aes(x = infect_rate, y = democ_score))+ labs(x="Infectious Disease Prevalance Score", y="Democratization Score", title="Comparison of Disease Prevalence and Democratization Score") + theme_minimal(base_family = "serif")
                                                                                                                                                                       disease_democ_chart

disease_democ_chart +
geom_point(size = 3, alpha =
0.5) + geom_smooth(method = 
lm, se=FALSE, color = "red")
`geom_smooth()` using formula = 'y ~ x'

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
p <- disease_democ_chart+
  geom_point(size = 2.5, alpha = 0.5)+
  geom_smooth(method = lm, se = FALSE, color = "red")
p <- ggplotly(p)
`geom_smooth()` using formula = 'y ~ x'
p
disease_democ_chart +
  geom_point(size = 3, alpha = 0.5, aes(color = income_group))+
  geom_smooth(method = lm, se = FALSE, color = "black", lyt = 2,
linewidth = 0.3)
Warning in geom_smooth(method = lm, se = FALSE, color = "black", lyt = 2, :
Ignoring unknown parameters: `lyt`
`geom_smooth()` using formula = 'y ~ x'

ggplot(disease_democ, aes(x = infect_rate, y = democ_score,
color = income_group)) +
 labs(x="Infectious Disease Prevalence Score",
    y = "Democratization Score",
    title="Comparison of Disease Prevalence and Democratization 
Score") +
 theme_minimal(base_size = 14, base_family = "serif") +
 geom_point(size = 3, alpha = 0.5) +
 geom_smooth(method=lm, se=FALSE, lty = 2, linewidth = 0.3)
`geom_smooth()` using formula = 'y ~ x'

library(dslabs)
data(package="dslabs")
#install.packages("gganimate")
library(gganimate)
No renderer backend detected. gganimate will default to writing frames to separate files
Consider installing:
- the `gifski` package for gif output
- the `av` package for video output
and restarting the R session
p <- ggplot(gapminder, aes(gdp, life_expectancy, size = population, color = continent)) +
 geom_point(alpha = 0.7)+
 scale_color_viridis_d()+
 scale_size(range = c(2, 12))+
 scale_x_log10()+
 labs(
  title = "Year:{frame_time}",
  x = "GDP per capita",
  y = "Life expectancy"
 )
anim <- p +transition_time(year) +
  ease_aes("linear")
anim
Warning: No renderer available. Please install the gifski, av, or magick package to
create animated output
NULL
#install.packages(c("highcharter", "RColorBrewer", "dplyr"))
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

Attaching package: 'highcharter'
The following object is masked from 'package:dslabs':

    stars
library(RColorBrewer)
library(dplyr)
nations <- read_csv("C:/Users/gopin/Downloads/nations.csv") |>
  mutate(gdp_tn = gdp_percap*population/10^12)
Rows: 5275 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): iso2c, iso3c, country, region, income
dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# prepare data
big4 <- nations |>
  filter(iso3c %in% c("CHN","DEU", "JPN", "USA")) |>
  arrange(year)
# basic symbol-and-line chart, default settings
highchart() |>
  hc_add_series(data = big4,
                type = "line", 
                hcaes(x = year,
                      y = gdp_tn, 
                      group = country))
# define color palette
cols <- brewer.pal(4, "Set1")

highchart() |>
  hc_add_series(data = big4,
                type = "line", hcaes(x = year,
                                     y = gdp_tn, 
                                     group = country)) |>
  hc_colors(cols)
highchart() |>
  hc_add_series(data = big4,
                type = "line",
                hcaes(x = year,
                      y = gdp_tn, 
                      group = country)) |>
  hc_colors(cols) |>
  hc_xAxis(title = list(text="Year")) |>
  hc_yAxis(title = list(text="GDP ($ trillion)"))
highchart() |>
  hc_add_series(data = big4,
                type = "line",
                hcaes(x = year,
                      y = gdp_tn, 
                      group = country)) |>
  hc_colors(cols) |>
  hc_xAxis(title = list(text="Year")) |>
  hc_yAxis(title = list(text="GDP ($ trillion)")) |>
  hc_plotOptions(series = list(marker = list(symbol = "circle"))) |>
  hc_legend(align = "right", 
            verticalAlign = "top")
# customize the tooltips

big4_chart <- highchart() |>
  hc_add_series(data = big4,
                type = "line",
                hcaes(x = year,
                      y = gdp_tn, 
                      group = country)) |>
  hc_colors(cols) |>
  hc_xAxis(title = list(text="Year")) |>
  hc_yAxis(title = list(text="GDP ($ trillion)")) |>
  hc_plotOptions(series = list(marker = list(symbol = "circle"))) |>
  hc_legend(align = "right", 
            verticalAlign = "top") |>
  hc_tooltip(shared = TRUE,
             borderColor = "black",
             pointFormat = "{point.country}: {point.gdp_tn:.2f}")
big4_chart
# prepare data
regions <- nations |>
  group_by(year,region) |>
  summarize(gdp_tn = sum(gdp_tn, na.rm = TRUE)) |>
  arrange(year,region)
`summarise()` has regrouped the output.
ℹ Summaries were computed grouped by year and region.
ℹ Output is grouped by year.
ℹ Use `summarise(.groups = "drop_last")` to silence this message.
ℹ Use `summarise(.by = c(year, region))` for per-operation grouping
  (`?dplyr::dplyr_by`) instead.
# basic area chart, default options
highchart () |>
  hc_add_series(data = regions,
                type = "area",
                hcaes(x = year,
                      y = gdp_tn, 
                      group = region))
# set color palette
cols <- brewer.pal(7, "Set2")

# stacked area chart
highchart () |>
  hc_add_series(data = regions,
                type = "area",
                hcaes(x = year,
                      y = gdp_tn, 
                      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_legend(align = "right", verticalAlign = "top",
            layout = "vertical") 
food_stamps<- read_csv("C:/Users/gopin/Downloads/food_stamps.csv")
Rows: 47 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (3): year, participants, costs

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cols <- c("dodgerblue","orange")  # set the color palette to dodger blue and orange

highchart() |>
  hc_yAxis_multiples(
    list(title = list(text = "Participants (millions)")),
    list(title = list(text = "Costs ($ billions)"),
         opposite = TRUE)
  ) |>
  hc_add_series(data = food_stamps$participants,
                name = "Participants (millions)",
                type = "column",
                yAxis = 0) |>
  hc_add_series(data = food_stamps$costs,
                name = "Costs ($ billions)",
                type = "line",
                yAxis = 1) |>
  hc_xAxis(categories = food_stamps$year,
           tickInterval = 5) |>
  hc_colors(cols) |>
  hc_chart(style = list(fontFamily = "AvantGarde",
                        fontWeight = "bold"))
library(ggplot2)

cols1 <- brewer.pal(4, "Set1")

ggchart1 <- ggplot(big4, aes(x = year, y = gdp_tn, group = country, color = country)) +
  geom_line() +
  geom_point(shape = "circle") +
  scale_color_manual(values = cols1) +
  labs(
    x = "Year",
    y = "GDP ($ trillion)"
  ) +
  theme_minimal() + 
  theme(
    legend.position = "top",
    legend.justification = "right"
  )

print(ggchart1)

cols2 <- brewer.pal(7, "Set2")

ggchart2 <- ggplot(regions, aes(x = year, y = gdp_tn, fill = region)) +
  
  geom_area(color = "white", linewidth = 0.5, position = "stack") +
  
  scale_color_manual(values = cols2) +
  
  labs(x = "Year", y = "GDP ($ trillion)", fill = "Region") +
 
  theme_minimal() +
  theme(
    text = element_text(face = "bold"),
    legend.position = "right",
    legend.justification = "top",
    legend.direction = "vertical",
    panel.grid.minor = element_blank() 
  )

print(ggchart2)