library(ggplot2)
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(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.1 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(gapminder)
library(gganimate)
library(dplyr)
Temp_and_rain <- read.csv("Temp_and_rain.CSV")
##Temp_and_rain -Scatter plot
ggplot(Temp_and_rain, aes(x = tem, y = rain)) +
geom_point(color = 'blue') +
labs(title = 'Scatter Plot of Temperature and Rainfall', x = 'Temperature (°C)', y = 'Rainfall (mm)') +
theme_minimal()
###***Interpretation
The plot visually represents the relationship between temperature and rainfall using blue points on a scatter plot. The x-axis represents temperature in degrees Celsius; the y-axis represents rainfall in millimetres. Each point on the plot corresponds to a specific combination of temperature and rain from the dataset “Temp_and_rain.” The minimal theme enhances the simplicity in the plot’s appearance.
##Add a linear Regression line
ggplot(Temp_and_rain, aes(x = tem, y = rain)) +
geom_point(color = 'blue') +
geom_smooth(method = 'lm', se = FALSE, color = 'red') +
labs(title = 'Scatter Plot of Temperature and Rainfall', x = 'Temperature (°C)', y = 'Rainfall (mm)') +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
###***Interpretation
The plot illustrates the correlation between temperature and rainfall using blue points on a scatter plot. Additionally, a red linear regression line has been added through the data points, providing a visual representation of the overall trend.The linear regression line helps to assess the general direction and strength of the relationship between temperature and rainfall in the dataset.
##Base plot with points and regression line
plot(Temp_and_rain$tem, Temp_and_rain$rain, col = "skyblue", pch = 16,
xlab = 'Temperature (°C)', ylab = 'Rainfall (mm)',
main = 'Scatter Plot of Temperature and Rainfall Across Years')
lm_model <- lm(rain ~ tem, data = Temp_and_rain)
abline(lm_model, col = 'blue')
###***Interpretation
The provided R code generates a scatter plot depicting the relationship between temperature and rainfall across multiple years. The points on the plot, shown in sky blue, represent individual temperature and rainfall data pairs. Additionally, a linear regression line in blue is superimposed on the plot using a model created with the lm function. This line serves as a visual representation of the overall trend in the data. ##Add an Interactive Scatter Plot
plot_ly(data = Temp_and_rain, x = ~tem, y = ~rain, type = 'scatter', mode = 'markers',
marker = list(color = ~Year, colorscale = 'Viridis'),
text = ~paste('Year: ', Year)) %>%
add_trace(x = ~tem, y = ~fitted(lm(rain ~ tem)), type = 'scatter', mode = 'lines',
line = list(color = 'green')) %>%
layout(title = 'Interactive Scatter Plot of Temperature and Rainfall Across Years',
xaxis = list(title = 'Temperature (°C)'),
yaxis = list(title = 'Rainfall (mm)'),
showlegend = FALSE)
## A marker object has been specified, but markers is not in the mode
## Adding markers to the mode...
###***Interpretation
The plot displays temperature on the x-axis and rainfall on the y-axis from the dataset Temp_and_rain. Each point in the scatter plot is colored based on the corresponding year, using the Viridis color scale. Text labels provide information about the year for each data point. Additionally, a green line represents the fitted linear regression line for the relationship between temperature and rainfall. #Create an animated bubble chart
animated_bubble_chart <- ggplot(Temp_and_rain, aes(x = tem, y = rain, size = tem, color = as.factor(Year))) +
geom_point(alpha = 0.7) +
labs(
title = "Animated Bubble Chart of Temperature and Rainfall",
x = "Temperature",
y = "Rainfall",
size = "Temperature",
color = "Year"
) +
transition_states(Year, transition_length = 2, state_length = 1) +
ease_aes('linear') +
shadow_mark(past = TRUE, future = FALSE)
animate(animated_bubble_chart, nframes = length(unique(Temp_and_rain$Year)) * 3, fps = 5)
anim_save("Temp_and_rain.gif", animated_bubble_chart)
###***Interpretation
Each bubble on the chart represents a specific year, with its size corresponding to temperature and color indicating the year. The chart transitions smoothly between different years, providing a dynamic visual representation of how temperature and rainfall patterns evolve over time. The animation is designed to enhance insights into the temporal trends within the dataset, with a linear easing effect and added shadows to highlight past states.