Posts

Showing posts from March, 2024

Module 11

Image
Module 11  ```R # Load necessary library library(ggplot2) # Define data x <- 1967:1977 y <- c(0.5, 1.8, 4.6, 5.3, 5.3, 5.7, 5.4, 5, 5.5, 6, 5) # Create data frame data <- data.frame(x = x, y = y) # Create dot-dash plot using ggplot2 ggplot(data, aes(x = x, y = y)) +   geom_point() +  # Add points   geom_line(linetype = "dashed") +  # Add dashed lines   labs(x = "", y = "Per capita budget expenditures in constant dollars") +  # Labels   theme_minimal() +  # Minimal theme   theme(axis.text.x = element_text(family = "serif"),  # Set x-axis font         axis.text.y = element_text(family = "serif"))  # Set y-axis font ``` This code will generate a dot-dash plot using ggplot2, with dots representing each data point and dashed lines connecting them. The x-axis represents the years from 1967 to 1977, and the y-axis represents the per capita budget expenditures in constant dollars. Adjustments to the font family are made to ensure the t

Module 10

  Exploring Time Series Data Visualization with ggplot2 In this blog post, I will explore the visualization of time series data using ggplot2, a powerful data visualization package in R. Time series data represents observations collected or recorded at different points in time, making it essential for analyzing trends, patterns, and relationships over time. 1. Hot Dog Eating Contest Results Visualization The first example focuses on visualizing the results of Nathan's Hot Dog Eating Contest from 1980 to 2010. We start by loading the data and examining the first few rows: ```R hotdogs <- read_csv("http://datasets.flowingdata.com/hot-dog-contest-winners.csv") head(hotdogs) ``` Next, we create a bar plot using R-base graphics to display the number of hot dogs and buns eaten each year, with different colors indicating whether a new record was set: ```R colors <- ifelse(hotdogs$New.record == 1, "darkred", "grey") barplot(hotdogs$Dogs.eaten, names.arg

Module 8 Assignment

Image
Correlation Analysis and ggplot2  Download the mtcars data set from the R datasets package using the following code: ```R data(mtcars) ```  Once you have the data set loaded, you can generate correlation analysis using the `cor()` function to calculate the correlation matrix: ```R correlation_matrix <- cor(mtcars) ``` Then, you can visualize the correlation matrix using ggplot2 and the `geom_tile()` function to create a heatmap: ```R library(ggplot2) # Convert correlation matrix to long format correlation_long <- reshape2::melt(correlation_matrix) # Plot correlation heatmap ggplot(correlation_long, aes(x = Var1, y = Var2, fill = value)) +   geom_tile() +   scale_fill_gradient2(low = "blue", high = "red", mid = "white",                         midpoint = 0, limit = c(-1,1),                        name = "Correlation") +   theme_minimal() +   labs(title = "Correlation Heatmap of mtcars Variables") ``` After generating the visualizat