library(rvest)
library(plotly)
## Loading required package: ggplot2
## 
## 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(ggplot2)
url <- "https://data.giss.nasa.gov/gistemp/graphs/graph_data/Global_Mean_Estimates_based_on_Land_and_Ocean_Data/graph.txt"
con <- url(url)
data <- readLines(con)
close(con)

start_index <- grep("^-?[0-9]", data)

data <- read.table(text = data[start_index:length(data)], header = TRUE)
## Warning in start_index:length(data): numerical expression has 144 elements:
## only the first used
print(head(data))
##   X1880 X.0.16 X.0.09
## 1  1881  -0.08  -0.13
## 2  1882  -0.11  -0.16
## 3  1883  -0.17  -0.20
## 4  1884  -0.28  -0.24
## 5  1885  -0.33  -0.26
## 6  1886  -0.31  -0.27
colnames(data)[1] <- "Year"
colnames(data)[2:3] <- c("Temperature (C)", "Temperature Smoothing")


print(data)
##     Year Temperature (C) Temperature Smoothing
## 1   1881           -0.08                 -0.13
## 2   1882           -0.11                 -0.16
## 3   1883           -0.17                 -0.20
## 4   1884           -0.28                 -0.24
## 5   1885           -0.33                 -0.26
## 6   1886           -0.31                 -0.27
## 7   1887           -0.36                 -0.27
## 8   1888           -0.17                 -0.26
## 9   1889           -0.10                 -0.25
## 10  1890           -0.35                 -0.25
## 11  1891           -0.22                 -0.25
## 12  1892           -0.27                 -0.26
## 13  1893           -0.31                 -0.26
## 14  1894           -0.30                 -0.24
## 15  1895           -0.22                 -0.22
## 16  1896           -0.11                 -0.20
## 17  1897           -0.11                 -0.18
## 18  1898           -0.27                 -0.16
## 19  1899           -0.17                 -0.17
## 20  1900           -0.08                 -0.20
## 21  1901           -0.15                 -0.23
## 22  1902           -0.28                 -0.25
## 23  1903           -0.36                 -0.28
## 24  1904           -0.46                 -0.30
## 25  1905           -0.25                 -0.33
## 26  1906           -0.22                 -0.35
## 27  1907           -0.38                 -0.37
## 28  1908           -0.42                 -0.39
## 29  1909           -0.48                 -0.40
## 30  1910           -0.43                 -0.41
## 31  1911           -0.44                 -0.38
## 32  1912           -0.36                 -0.35
## 33  1913           -0.33                 -0.32
## 34  1914           -0.14                 -0.30
## 35  1915           -0.13                 -0.29
## 36  1916           -0.35                 -0.29
## 37  1917           -0.45                 -0.29
## 38  1918           -0.29                 -0.29
## 39  1919           -0.27                 -0.29
## 40  1920           -0.27                 -0.27
## 41  1921           -0.18                 -0.26
## 42  1922           -0.28                 -0.25
## 43  1923           -0.26                 -0.24
## 44  1924           -0.27                 -0.23
## 45  1925           -0.22                 -0.22
## 46  1926           -0.11                 -0.22
## 47  1927           -0.22                 -0.21
## 48  1928           -0.20                 -0.20
## 49  1929           -0.36                 -0.19
## 50  1930           -0.15                 -0.19
## 51  1931           -0.09                 -0.18
## 52  1932           -0.16                 -0.18
## 53  1933           -0.28                 -0.17
## 54  1934           -0.12                 -0.15
## 55  1935           -0.20                 -0.14
## 56  1936           -0.14                 -0.10
## 57  1937           -0.03                 -0.06
## 58  1938            0.00                 -0.01
## 59  1939           -0.02                  0.03
## 60  1940            0.13                  0.07
## 61  1941            0.19                  0.09
## 62  1942            0.07                  0.11
## 63  1943            0.09                  0.10
## 64  1944            0.21                  0.08
## 65  1945            0.10                  0.05
## 66  1946           -0.07                  0.01
## 67  1947           -0.02                 -0.03
## 68  1948           -0.10                 -0.07
## 69  1949           -0.11                 -0.08
## 70  1950           -0.17                 -0.07
## 71  1951           -0.07                 -0.07
## 72  1952            0.01                 -0.07
## 73  1953            0.08                 -0.07
## 74  1954           -0.13                 -0.06
## 75  1955           -0.14                 -0.06
## 76  1956           -0.19                 -0.05
## 77  1957            0.05                 -0.04
## 78  1958            0.06                 -0.01
## 79  1959            0.03                  0.01
## 80  1960           -0.03                  0.03
## 81  1961            0.06                  0.01
## 82  1962            0.03                 -0.01
## 83  1963            0.05                 -0.03
## 84  1964           -0.20                 -0.04
## 85  1965           -0.11                 -0.05
## 86  1966           -0.06                 -0.06
## 87  1967           -0.02                 -0.05
## 88  1968           -0.08                 -0.03
## 89  1969            0.05                 -0.02
## 90  1970            0.03                  0.00
## 91  1971           -0.08                  0.00
## 92  1972            0.01                  0.00
## 93  1973            0.16                  0.00
## 94  1974           -0.07                  0.01
## 95  1975           -0.01                  0.02
## 96  1976           -0.10                  0.04
## 97  1977            0.18                  0.07
## 98  1978            0.07                  0.12
## 99  1979            0.16                  0.16
## 100 1980            0.26                  0.20
## 101 1981            0.32                  0.21
## 102 1982            0.14                  0.22
## 103 1983            0.31                  0.21
## 104 1984            0.16                  0.21
## 105 1985            0.12                  0.22
## 106 1986            0.18                  0.24
## 107 1987            0.32                  0.27
## 108 1988            0.39                  0.31
## 109 1989            0.27                  0.33
## 110 1990            0.45                  0.33
## 111 1991            0.40                  0.33
## 112 1992            0.22                  0.33
## 113 1993            0.23                  0.33
## 114 1994            0.31                  0.34
## 115 1995            0.45                  0.37
## 116 1996            0.33                  0.40
## 117 1997            0.46                  0.42
## 118 1998            0.61                  0.44
## 119 1999            0.38                  0.47
## 120 2000            0.39                  0.50
## 121 2001            0.53                  0.52
## 122 2002            0.63                  0.55
## 123 2003            0.62                  0.58
## 124 2004            0.53                  0.61
## 125 2005            0.68                  0.62
## 126 2006            0.64                  0.62
## 127 2007            0.66                  0.63
## 128 2008            0.54                  0.64
## 129 2009            0.66                  0.64
## 130 2010            0.72                  0.65
## 131 2011            0.61                  0.66
## 132 2012            0.65                  0.70
## 133 2013            0.68                  0.74
## 134 2014            0.74                  0.79
## 135 2015            0.90                  0.83
## 136 2016            1.02                  0.87
## 137 2017            0.92                  0.91
## 138 2018            0.85                  0.93
## 139 2019            0.98                  0.93
## 140 2020            1.01                  0.95
## 141 2021            0.85                  0.97
## 142 2022            0.89                  0.99
## 143 2023            1.17                  1.01
data$'Temperature (C)' <- (9/5) * data$'Temperature (C)' + 32
colnames(data)[2] <- "Temperature F"

data$`Temperature Smoothing` <- (9/5) * data$`Temperature Smoothing` + 32
colnames(data)[3] <- "Temperature Smoothing F"


data <- data[data$Year >= 1990 & data$Year <= 2020, ]




print(data)
##     Year Temperature F Temperature Smoothing F
## 110 1990        32.810                  32.594
## 111 1991        32.720                  32.594
## 112 1992        32.396                  32.594
## 113 1993        32.414                  32.594
## 114 1994        32.558                  32.612
## 115 1995        32.810                  32.666
## 116 1996        32.594                  32.720
## 117 1997        32.828                  32.756
## 118 1998        33.098                  32.792
## 119 1999        32.684                  32.846
## 120 2000        32.702                  32.900
## 121 2001        32.954                  32.936
## 122 2002        33.134                  32.990
## 123 2003        33.116                  33.044
## 124 2004        32.954                  33.098
## 125 2005        33.224                  33.116
## 126 2006        33.152                  33.116
## 127 2007        33.188                  33.134
## 128 2008        32.972                  33.152
## 129 2009        33.188                  33.152
## 130 2010        33.296                  33.170
## 131 2011        33.098                  33.188
## 132 2012        33.170                  33.260
## 133 2013        33.224                  33.332
## 134 2014        33.332                  33.422
## 135 2015        33.620                  33.494
## 136 2016        33.836                  33.566
## 137 2017        33.656                  33.638
## 138 2018        33.530                  33.674
## 139 2019        33.764                  33.674
## 140 2020        33.818                  33.710
data <- data %>%
  mutate(Year = as.character(Year),
         `Temperature F` = as.character(`Temperature F`),
         `Temperature Smoothing F` = as.character(`Temperature Smoothing F`))
head(data)
##     Year Temperature F Temperature Smoothing F
## 110 1990         32.81                  32.594
## 111 1991         32.72                  32.594
## 112 1992        32.396                  32.594
## 113 1993        32.414                  32.594
## 114 1994        32.558                  32.612
## 115 1995         32.81                  32.666
library(plotly)

hover_text <- paste("Year: ", data$Year, "<br>Temperature F: ", data$`Temperature F`)

# Create the plot
fig1 <- plot_ly(data = data, x = ~Year, y = ~`Temperature F`, type = 'bar',
               text = hover_text, hoverinfo = 'text',
               marker = list(color = 'rgb(255, 0, 0)',  
                             line = list(color = 'rgb(0, 0, 0)', width = 1)))

# Layout adjustments
fig1 <- fig1 %>% layout(
  xaxis = list(title = "Year", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  yaxis = list(title = "Temperature", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  bargap = 0.05,
  height = 500,
  width = 800,
  font = list(color = "rgba(0, 0, 0, 0.8)"))
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
# Add annotations
fig1 <- fig1 %>% add_annotations(
  text = "<b>TEMPERATURE PER YEAR</b>",
  x = 0, xref = "paper",
  y = 1.05, yref = "paper",
  showarrow = FALSE,
  font = list(size = 18, color = "rgba(0, 0, 0, 0.8)"),
  align = "left"
)

fig1
hover_text <- paste("Year: ", data$Year, "<br>Temperature F: ", data$`Temperature F`)

# Create the plot
fig2 <- plot_ly(data = data, x = ~Year, y = ~`Temperature F`, type = 'scatter', mode = 'lines+markers',
               text = hover_text, hoverinfo = 'text',color = 'rgb(255, 123, 255)',
               marker = list(color = 'rgb(255, 0, 0)',
                             size = 8,
                             line = list(color = 'rgb(255, 0, 0)', width = 1)))


fig2 <- fig2 %>% layout(
  xaxis = list(title = "Year", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  yaxis = list(title = "Temperature", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  height = 500,
  width = 800,
  font = list(color = "rgba(0, 0, 0, 0.8)"))
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
# Add annotations
fig2 <- fig2 %>% add_annotations(
  text = "<b>TEMPERATURE PER YEAR</b>",
  x = 0, xref = "paper",
  y = 1.05, yref = "paper",
  showarrow = FALSE,
  font = list(size = 12, color = "rgba(0, 0, 0, 0.8)"),
  align = "left"
)

fig2 <- fig2 %>% add_annotations(
  text = "1990 to 2020",
  x = 1, xref = "paper",
  y = 0, yref = "paper",
  xanchor = "right",
  yanchor = "bottom",
  showarrow = FALSE,
  font = list(size = 14, color = "rgba(0, 0, 0, 0.8)"),
  align = "center "
)

fig2
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

Hurricane

url <- "https://tropical.atmos.colostate.edu/Realtime/index.php?arch&loc=global"
hur <- read_html(url)

hur_df <- hur %>% 
  html_nodes("td:nth-child(8), td:nth-child(6), th:nth-child(8), th:nth-child(6), td:nth-child(4), th:nth-child(4), #info a, th:nth-child(1)") %>% 
  html_text() 

column_names <- c("Year", "Hurricanes", "Cat. 3+ Hurricanes", "Accumulated Cyclone Energy")

num_columns <- length(column_names)
num_rows <- length(hur_df) / num_columns

hur_matrix <- matrix(hur_df, ncol = num_columns, byrow = TRUE)

hur_df_final <- as.data.frame(hur_matrix)

colnames(hur_df_final) <- column_names

print(hur_df_final)
##    Year Hurricanes Cat. 3+ Hurricanes Accumulated Cyclone Energy
## 1  Year Hurricanes Cat. 3+ Hurricanes Accumulated Cyclone Energy
## 2  1980         43                 19                        638
## 3  1981         45                 15                      554.7
## 4  1982         46                 21                      709.2
## 5  1983         42                 21                        680
## 6  1984         47                 20                      726.2
## 7  1985         51                 24                      717.8
## 8  1986         48                 16                        695
## 9  1987         39                 18                      649.1
## 10 1988         39                 19                      625.4
## 11 1989         55                 25                      853.9
## 12 1990         58                 21                      930.8
## 13 1991         47                 25                      860.6
## 14 1992         59                 32                     1163.1
## 15 1993         49                 24                      710.4
## 16 1994         51                 31                       1019
## 17 1995         49                 24                      779.3
## 18 1996         57                 27                        960
## 19 1997         58                 28                     1099.2
## 20 1998         50                 21                      773.1
## 21 1999         39                 21                      606.4
## 22 2000         45                 20                      677.3
## 23 2001         51                 24                      672.4
## 24 2002         41                 27                        812
## 25 2003         50                 25                        833
## 26 2004         51                 32                     1024.4
## 27 2005         51                 27                      899.6
## 28 2006         42                 27                        761
## 29 2007         44                 22                      568.1
## 30 2008         40                 24                      613.9
## 31 2009         38                 21                      609.6
## 32 2010         39                 19                      526.8
## 33 2011         39                 21                      573.8
## 34 2012         47                 24                      740.5
## 35 2013         46                 22                      618.5
## 36 2014         46                 26                        724
## 37 2015         54                 39                       1047
## 38 2016         47                 26                      806.5
## 39 2017         43                 20                      621.1
## 40 2018         59                 33                     1108.4
## 41 2019         55                 35                      854.8
## 42 2020         46                 24                      599.1
## 43 2021         37                 16                      621.1
## 44 2022         40                 17                      559.6
## 45 2023         45                 30                      857.4
library(plotly)

# Filter data for the years 1990 to 2020
hur_df_filtered <- hur_df_final %>%
  filter(Year >= 1990 & Year <= 2020)

# Create hover text
hover_text <- paste("Year: ", hur_df_filtered$Year, "<br>Hurricanes: ", hur_df_filtered$Hurricanes)

# Create the plot
fig3 <- plot_ly(data = hur_df_filtered, x = ~Year, y = ~Hurricanes, type = 'bar',
               text = hover_text, hoverinfo = 'text',
               marker = list(color = 'rgb(255, 0, 0)',  # Red color
                             line = list(color = 'rgb(0, 0, 0)', width = 1)))

# Layout adjustments
fig3 <- fig3 %>% layout(
  xaxis = list(title = "Year", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  yaxis = list(title = "Number of Hurricanes", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  bargap = 0.05,
  height = 500,
  width = 800,
  font = list(color = "rgba(0, 0, 0, 0.8)"))
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
# Add annotations
fig3 <- fig3 %>% add_annotations(
  text = "<b>NUMBER OF HURRICANES PER YEAR</b>",
  x = 0, xref = "paper",
  y = 1.05, yref = "paper",
  showarrow = FALSE,
  font = list(size = 18, color = "rgba(0, 0, 0, 0.8)"),
  align = "left"
)

fig3
hover_text <- paste("Year: ", hur_df_filtered$Year, "<br>Hurricanes: ", hur_df_filtered$Hurricanes)

# Create the plot
fig4 <- plot_ly(data = hur_df_filtered, x = ~Year, y = ~Hurricanes, type = 'scatter', mode = 'lines+markers',
               text = hover_text, hoverinfo = 'text',color = 'rgb(255, 123, 255)',
               marker = list(color = 'rgb(255, 123, 255)',
                             size = 8,
                             line = list(color = 'rgb(255, 0, 0)', width = 1)))

# Layout adjustments
fig4 <- fig4 %>% layout(
  xaxis = list(title = "Year", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  yaxis = list(title = "Number of Hurricanes", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  height = 500,
  width = 800,
  font = list(color = "rgba(0, 0, 0, 0.8)"))
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
# Add annotations
fig4 <- fig4 %>% add_annotations(
  text = "<b>NUMBER OF HURRICANES PER YEAR</b>",
  x = 0, xref = "paper",
  y = 1.05, yref = "paper",
  showarrow = FALSE,
  font = list(size = 12, color = "rgba(0, 0, 0, 0.8)"),
  align = "left"
)

fig4 <- fig4 %>% add_annotations(
  text = "1990 to 2020",
  x = 1, xref = "paper",
  y = 0, yref = "paper",
  xanchor = "right",
  yanchor = "bottom",
  showarrow = FALSE,
  font = list(size = 14, color = "rgba(0, 0, 0, 0.8)"),
  align = "center "
)

fig4
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
hur_df_final <- hur_df_final %>%
  mutate(Year = as.integer(Year),
         Hurricanes = as.integer(Hurricanes),
         `Cat. 3+ Hurricanes` = as.integer(`Cat. 3+ Hurricanes`),
         `Accumulated Cyclone Energy` = as.numeric(`Accumulated Cyclone Energy`))
## Warning: There were 4 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `Year = as.integer(Year)`.
## Caused by warning:
## ! NAs introduced by coercion
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 3 remaining warnings.
print(hur_df_final)
##    Year Hurricanes Cat. 3+ Hurricanes Accumulated Cyclone Energy
## 1    NA         NA                 NA                         NA
## 2  1980         43                 19                      638.0
## 3  1981         45                 15                      554.7
## 4  1982         46                 21                      709.2
## 5  1983         42                 21                      680.0
## 6  1984         47                 20                      726.2
## 7  1985         51                 24                      717.8
## 8  1986         48                 16                      695.0
## 9  1987         39                 18                      649.1
## 10 1988         39                 19                      625.4
## 11 1989         55                 25                      853.9
## 12 1990         58                 21                      930.8
## 13 1991         47                 25                      860.6
## 14 1992         59                 32                     1163.1
## 15 1993         49                 24                      710.4
## 16 1994         51                 31                     1019.0
## 17 1995         49                 24                      779.3
## 18 1996         57                 27                      960.0
## 19 1997         58                 28                     1099.2
## 20 1998         50                 21                      773.1
## 21 1999         39                 21                      606.4
## 22 2000         45                 20                      677.3
## 23 2001         51                 24                      672.4
## 24 2002         41                 27                      812.0
## 25 2003         50                 25                      833.0
## 26 2004         51                 32                     1024.4
## 27 2005         51                 27                      899.6
## 28 2006         42                 27                      761.0
## 29 2007         44                 22                      568.1
## 30 2008         40                 24                      613.9
## 31 2009         38                 21                      609.6
## 32 2010         39                 19                      526.8
## 33 2011         39                 21                      573.8
## 34 2012         47                 24                      740.5
## 35 2013         46                 22                      618.5
## 36 2014         46                 26                      724.0
## 37 2015         54                 39                     1047.0
## 38 2016         47                 26                      806.5
## 39 2017         43                 20                      621.1
## 40 2018         59                 33                     1108.4
## 41 2019         55                 35                      854.8
## 42 2020         46                 24                      599.1
## 43 2021         37                 16                      621.1
## 44 2022         40                 17                      559.6
## 45 2023         45                 30                      857.4
hur_df_final <- hur_df_final[-1,]


print(hur_df_final)
##    Year Hurricanes Cat. 3+ Hurricanes Accumulated Cyclone Energy
## 2  1980         43                 19                      638.0
## 3  1981         45                 15                      554.7
## 4  1982         46                 21                      709.2
## 5  1983         42                 21                      680.0
## 6  1984         47                 20                      726.2
## 7  1985         51                 24                      717.8
## 8  1986         48                 16                      695.0
## 9  1987         39                 18                      649.1
## 10 1988         39                 19                      625.4
## 11 1989         55                 25                      853.9
## 12 1990         58                 21                      930.8
## 13 1991         47                 25                      860.6
## 14 1992         59                 32                     1163.1
## 15 1993         49                 24                      710.4
## 16 1994         51                 31                     1019.0
## 17 1995         49                 24                      779.3
## 18 1996         57                 27                      960.0
## 19 1997         58                 28                     1099.2
## 20 1998         50                 21                      773.1
## 21 1999         39                 21                      606.4
## 22 2000         45                 20                      677.3
## 23 2001         51                 24                      672.4
## 24 2002         41                 27                      812.0
## 25 2003         50                 25                      833.0
## 26 2004         51                 32                     1024.4
## 27 2005         51                 27                      899.6
## 28 2006         42                 27                      761.0
## 29 2007         44                 22                      568.1
## 30 2008         40                 24                      613.9
## 31 2009         38                 21                      609.6
## 32 2010         39                 19                      526.8
## 33 2011         39                 21                      573.8
## 34 2012         47                 24                      740.5
## 35 2013         46                 22                      618.5
## 36 2014         46                 26                      724.0
## 37 2015         54                 39                     1047.0
## 38 2016         47                 26                      806.5
## 39 2017         43                 20                      621.1
## 40 2018         59                 33                     1108.4
## 41 2019         55                 35                      854.8
## 42 2020         46                 24                      599.1
## 43 2021         37                 16                      621.1
## 44 2022         40                 17                      559.6
## 45 2023         45                 30                      857.4
print(df2)
##    October.September.U.S..Tornadoes...denotes.preliminary.data.         X
## 1                                                          Date Tornadoes
## 2                                                        195109       246
## 3                                                        195209       255
## 4                                                        195309       392
## 5                                                        195409       556
## 6                                                        195509       578
## 7                                                        195609       505
## 8                                                        195709       787
## 9                                                        195809       624
## 10                                                       195909       621
## 11                                                       196009       608
## 12                                                       196109       676
## 13                                                       196209       704
## 14                                                       196309       452
## 15                                                       196409       677
## 16                                                       196509       900
## 17                                                       196609       582
## 18                                                       196709       882
## 19                                                       196809       676
## 20                                                       196909       644
## 21                                                       197009       633
## 22                                                       197109       854
## 23                                                       197209       789
## 24                                                       197309      1008
## 25                                                       197409      1040
## 26                                                       197509       912
## 27                                                       197609       897
## 28                                                       197709       794
## 29                                                       197809       815
## 30                                                       197909       835
## 31                                                       198009       889
## 32                                                       198109       789
## 33                                                       198209       964
## 34                                                       198309       935
## 35                                                       198409       949
## 36                                                       198509       730
## 37                                                       198609       757
## 38                                                       198709       634
## 39                                                       198809       612
## 40                                                       198909       928
## 41                                                       199009      1143
## 42                                                       199109      1177
## 43                                                       199209      1143
## 44                                                       199309      1297
## 45                                                       199409      1068
## 46                                                       199509      1162
## 47                                                       199609      1205
## 48                                                       199709      1149
## 49                                                       199809      1448
## 50                                                       199909      1421
## 51                                                       200009       972
## 52                                                       200109      1101
## 53                                                       200209       934
## 54                                                       200309      1555
## 55                                                       200409      1640
## 56                                                       200509      1322
## 57                                                       200609      1140
## 58                                                       200709      1148
## 59                                                       200809      1722
## 60                                                       200909      1122
## 61                                                       201009      1195
## 62                                                       201109      1802
## 63                                                       201209       923
## 64                                                       201309       844
## 65                                                       201409       929
## 66                                                       201509      1071
## 67                                                       201609      1109
## 68                                                       201709      1389
## 69                                                       201809       983
## 70                                                       201909      1651
## 71                                                       202009      1150
## 72                                                       202109       984
## 73                                                       202209      1387
## 74                                                       202309     1393*
##           X.1
## 1  Fatalities
## 2          35
## 3         233
## 4         470
## 5          81
## 6         131
## 7          85
## 8         147
## 9         109
## 10         62
## 11         45
## 12         52
## 13         31
## 14         31
## 15         49
## 16        319
## 17         98
## 18        106
## 19        141
## 20         69
## 21         68
## 22        163
## 23         34
## 24         76
## 25        376
## 26         64
## 27         45
## 28         40
## 29         51
## 30         82
## 31         34
## 32         25
## 33         57
## 34         36
## 35        122
## 36         96
## 37         18
## 38         42
## 39         34
## 40         30
## 41         84
## 42         41
## 43         11
## 44        135
## 45         45
## 46         37
## 47         26
## 48         72
## 49        128
## 50         94
## 51         31
## 52         32
## 53         28
## 54         97
## 55         29
## 56         17
## 57         83
## 58         87
## 59        130
## 60         22
## 61         37
## 62        557
## 63         75
## 64         45
## 65         51
## 66         16
## 67         38
## 68         41
## 69          6
## 70         42
## 71         80
## 72         13
## 73        106
## 74
colnames(df2) <- unlist(df2[1, ])

# Remove the first row
df2 <- df2[-1, ]

# Print the dataframe
print(df2)
##      Date Tornadoes Fatalities
## 2  195109       246         35
## 3  195209       255        233
## 4  195309       392        470
## 5  195409       556         81
## 6  195509       578        131
## 7  195609       505         85
## 8  195709       787        147
## 9  195809       624        109
## 10 195909       621         62
## 11 196009       608         45
## 12 196109       676         52
## 13 196209       704         31
## 14 196309       452         31
## 15 196409       677         49
## 16 196509       900        319
## 17 196609       582         98
## 18 196709       882        106
## 19 196809       676        141
## 20 196909       644         69
## 21 197009       633         68
## 22 197109       854        163
## 23 197209       789         34
## 24 197309      1008         76
## 25 197409      1040        376
## 26 197509       912         64
## 27 197609       897         45
## 28 197709       794         40
## 29 197809       815         51
## 30 197909       835         82
## 31 198009       889         34
## 32 198109       789         25
## 33 198209       964         57
## 34 198309       935         36
## 35 198409       949        122
## 36 198509       730         96
## 37 198609       757         18
## 38 198709       634         42
## 39 198809       612         34
## 40 198909       928         30
## 41 199009      1143         84
## 42 199109      1177         41
## 43 199209      1143         11
## 44 199309      1297        135
## 45 199409      1068         45
## 46 199509      1162         37
## 47 199609      1205         26
## 48 199709      1149         72
## 49 199809      1448        128
## 50 199909      1421         94
## 51 200009       972         31
## 52 200109      1101         32
## 53 200209       934         28
## 54 200309      1555         97
## 55 200409      1640         29
## 56 200509      1322         17
## 57 200609      1140         83
## 58 200709      1148         87
## 59 200809      1722        130
## 60 200909      1122         22
## 61 201009      1195         37
## 62 201109      1802        557
## 63 201209       923         75
## 64 201309       844         45
## 65 201409       929         51
## 66 201509      1071         16
## 67 201609      1109         38
## 68 201709      1389         41
## 69 201809       983          6
## 70 201909      1651         42
## 71 202009      1150         80
## 72 202109       984         13
## 73 202209      1387        106
## 74 202309     1393*
df2$Date <- substr(df2$Date, 1, 4)

colnames(df2)[1] <- "Year"

df2 <- df2[df2$Year >= 1990 & df2$Year <= 2020, ]
df2$Tornadoes <- as.numeric(df2$Tornadoes)
print(df2)
##    Year Tornadoes Fatalities
## 41 1990      1143         84
## 42 1991      1177         41
## 43 1992      1143         11
## 44 1993      1297        135
## 45 1994      1068         45
## 46 1995      1162         37
## 47 1996      1205         26
## 48 1997      1149         72
## 49 1998      1448        128
## 50 1999      1421         94
## 51 2000       972         31
## 52 2001      1101         32
## 53 2002       934         28
## 54 2003      1555         97
## 55 2004      1640         29
## 56 2005      1322         17
## 57 2006      1140         83
## 58 2007      1148         87
## 59 2008      1722        130
## 60 2009      1122         22
## 61 2010      1195         37
## 62 2011      1802        557
## 63 2012       923         75
## 64 2013       844         45
## 65 2014       929         51
## 66 2015      1071         16
## 67 2016      1109         38
## 68 2017      1389         41
## 69 2018       983          6
## 70 2019      1651         42
## 71 2020      1150         80
library(plotly)

# Convert Year column to character type
df2$Year <- as.character(df2$Year)

# Create hover text without fatalities
hover_text <- paste("Year: ", df2$Year, "<br>Tornadoes: ", df2$Tornadoes)

# Create the plot
fig5 <- plot_ly(data = df2, x = ~Year, y = ~Tornadoes, type = 'bar',
               text = hover_text, hoverinfo = 'text',
               marker = list(color = 'rgb(255, 102, 0)',  # Orange color
                             line = list(color = 'rgb(0, 0, 0)', width = 1)))

# Layout adjustments
fig5 <- fig5 %>% layout(
  xaxis = list(title = "Year", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  yaxis = list(title = "Number of Tornadoes", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  bargap = 0.05,
  height = 500,
  width = 800,
  font = list(color = "rgba(0, 0, 0, 0.8)"))
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
# Add annotations
fig5 <- fig5 %>% add_annotations(
  text = "<b>NUMBER OF TORNADOES PER YEAR</b>",
  x = 0, xref = "paper",
  y = 1.05, yref = "paper",
  showarrow = FALSE,
  font = list(size = 18, color = "rgba(0, 0, 0, 0.8)"),
  align = "left"
)

fig5
library(plotly)

# Create hover text for tornadoes data
hover_text_tornadoes <- paste("Year: ", df2$Year, "<br>Tornadoes: ", df2$Tornadoes)

# Create the plot for tornadoes data
fig6 <- plot_ly(data = df2, x = ~Year, y = ~Tornadoes, type = 'scatter', mode = 'lines+markers',
               text = hover_text_tornadoes, hoverinfo = 'text', color = 'rgb(255, 123, 255)',
               marker = list(color = 'rgb(255, 102, 0)',  
                             line = list(color = 'rgb(0, 0, 0)', width = 1)))

# Layout adjustments
fig6 <- fig6 %>% layout(
  xaxis = list(title = "Year", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  yaxis = list(title = "Number of Tornadoes", tickfont = list(color = "rgba(0, 0, 0, 0.8)")),
  height = 500,
  width = 800,
  font = list(color = "rgba(0, 0, 0, 0.8)"))
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()
# Add annotations
fig6 <- fig6 %>% add_annotations(
  text = "<b>NUMBER OF TORNADOES PER YEAR</b>",
  x = 0, xref = "paper",
  y = 1.05, yref = "paper",
  showarrow = FALSE,
  font = list(size = 12, color = "rgba(0, 0, 0, 0.8)"),
  align = "left"
)

# Add annotation for the time period
fig6 <- fig6 %>% add_annotations(
  text = "1990 to 2020",
  x = 1, xref = "paper",
  y = 0, yref = "paper",
  xanchor = "right",
  yanchor = "bottom",
  showarrow = FALSE,
  font = list(size = 14, color = "rgba(0, 0, 0, 0.8)"),
  align = "center "
)

fig6
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
subplot_fig <- subplot(fig2, fig4, fig6, nrows = 3, titleX = FALSE, shareX = TRUE)
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
# Add title and subtitle
subplot_fig <- subplot_fig %>% layout(
  title = "Meteorological Statistics By Year",
  titlefont = list(size = 20),
  titlepad = 20,
  subtitle = "Yearly counts of meteorological events from 1997 to 2022",
  font = list(size = 14)
)

subplot_fig
## Warning: The titlefont attribute is deprecated. Use title = list(font = ...)
## instead.
## Warning: 'layout' objects don't have these attributes: 'titlepad', 'subtitle'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'