PROJECT
STATIC PLOT
data(mtcars)
library(ggplot2)
ggplot(data=mtcars,aes(wt, mpg)) +
geom_point(size = 2, color = "magenta", alpha = 1) +
geom_line() +
labs(
title = "Relationship between Weight and Miles per Gallon",
x = "Weight (1000 lb)",
y = "Miles per gallon",
caption = "Data from the 1974 Motor Trend US magazine."
) +
theme_dark()+
theme(plot.title = element_text(color = "red"),
axis.title.y = element_text(color = "blue"),
axis.title.x = element_text(color="blue")
)
EXPLANATION:
The graph above demonstrates the relation between weight and MPG(miles per gallon).We can see there is an inverse relation between weight and MPG.In simpler terms, heavier cars tend to have lower MPG, meaning they travel fewer miles per gallon of fuel.Heavier car needs more energy/fuel to travel the same distance as lighter cars.However.the line is not smooth,meaning that weight is not the only factor that effects MPG.
DYNAMIC PLOT
library(ggplot2)
library(plotly)
p<-ggplot(airquality, aes(x = Solar.R, y = Ozone, color = Temp)) +
geom_point(alpha = .8) +
labs(
title = "Ozone vs. Solar Radiation (Colored by Temperature)",
x = "Ozone (μg/m^3)",
y = "Solar Radiation (μg/m^3)",
caption = "Source: Air Quality Data") +
theme(
plot.title = element_text(face = "bold", color = "orange"),
axis.title.y = element_text(face = "bold", color = "green"),
axis.title.x = element_text(face = "bold", color = "blue"),
legend.title = element_text(face="bold",color="black")
) +
scale_color_viridis_c(name="Temperature")+
coord_cartesian(ylim=c(0,100),xlim = c(0,300))+
theme(plot.title = element_text(hjust = .5))+
geom_smooth(method = "lm", se = FALSE, color = "gray")
ggplotly(p)
EXPLANATION:
The graph shows the relationship between Ozone,Sun radiation and Temperature.The data suggests weak positive relationships between all three variables. Higher solar radiation is associated with slightly higher ozone and temperature, and higher ozone is associated with slightly higher temperature.This is shown by the changes in color spectrum and the increasing trend in points. However, the relationships between them are weak which is shown by the way points are scattered across the line.
ANIMATION
library(ggplot2)
library(gganimate)
ggplot(data=economics,aes(pce, psavert, size=pop, colour = unemploy))+
geom_point(alpha = 0.5, show.legend = TRUE)+
scale_color_viridis_c(name = "Unemployment Rate")+
scale_size(range = c(2,12), name = "Population (Millions)")+
labs(title = 'Year: {frame_time}', x = 'Personal Consumption Expenditure', y = 'Personal Savings Rate',
caption = "US economics data")+
theme_dark()+
theme(plot.title = element_text(face = "bold",hjust = .5),
axis.title.x = element_text(face="bold"),
axis.title.y = element_text(face="bold"))+
transition_time(date) +
shadow_wake(.2)+
ease_aes('linear')
EXPLANATON:
The graph shows relation of unemployment rate and Population with regards to personal savings rate and personal capital expenditure.We can see that years go on,more people tend to spend more than save,which is shown by declining trend in animation.While people spend more,unemployment rate also increases as time goes on,which is shown by color changes in the spectrum.