# Step 1 # install Packages - If you already have these packages installed in your system, go to Step 2 install.packages("ggplot2") install.packages("readxl") install.packages("ggplot2") install.packages("dplyr") # Step 2 # Load Package library(readxl) library(ggplot2) library(dplyr) # Step 3 # Define the file path - adjust the Path to the location of the downloaded data in your system file_path <- "C:/......./Data Visualization Example Final.xlsx" ################################################################### # Bar Chart example ########################################### # Import the sheet "Car prices - Bar" data <- read_excel(file_path, sheet = "Car prices - Bar") # Clean the Price column data <- data %>% mutate(Price = as.numeric(gsub("[\\$,]", "", `Car Price`))) # Remove '$' and ',' # Plot the bar chart ggplot(data, aes(x = Model, y = Price)) + geom_bar(stat = "identity", fill = "red") + # Set all bars to the same color labs(title = "Car Model Prices", x = "Car Model", y = "Price (USD)") + theme_minimal() + theme(legend.position = "none") + # Remove legend theme(axis.line = element_line(colour = "black")) ############################################ # Scatter plot example ############################################### # Read the data from the sheet scatterplot_data <- read_excel(file_path, sheet = "TV Adv and Sales - Scatter plot") # Clean the numeric columns by removing "$" and converting to numeric scatterplot_data <- scatterplot_data %>% mutate(across(everything(), ~ as.numeric(gsub("[\\$,]", "", .)))) # Plot the scatter plot ggplot(scatterplot_data, aes(x = `TV Ad Budget ($)`, y = `Sales ($)`)) + geom_point(color = "#1f77b4", size = 3) + # Set point color and size labs(title = "Relationship between Sales and TV Ad Budget", x = "TV Ad Budget ($)", y = "Sales ($)") + ylim(0, max(scatterplot_data$`Sales ($)`, na.rm = TRUE)) + theme_minimal() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), axis.line = element_line(colour = "black")) ##################################################################### # Pie Chart Example ################################################################ education_data <- read_excel(file_path, sheet = "Education Attainment - Pie") # Compute percentage education_data <- education_data %>% mutate(Percentage = (`Educational Attainment Distribution` / sum(`Educational Attainment Distribution`)) * 100) # Plot pie chart with percentage values ggplot(education_data, aes(x = "", y = `Educational Attainment Distribution`, fill = `Level of Education Attainment`, label = paste0(round(Percentage, 1), "%"))) + geom_bar(stat = "identity", width = 1) + coord_polar(theta = "y") + # Convert to pie chart labs(title = "Educational Attainment Distribution") + theme_minimal() + theme(axis.text = element_blank(), axis.ticks = element_blank(), panel.grid = element_blank()) + scale_fill_brewer(palette = "Pastel1") + # Set color palette geom_text(position = position_stack(vjust = 0.5), size = 4) # Add percentage labels to the pie chart ########################################################################### # Business Case Challenge ############################################################ tax_data <- read_excel(file_path, sheet = "Business Case") # Plot bar chart with one color and no legend ggplot(tax_data, aes(x = factor(Year), y = `Tax Rate`)) + geom_bar(stat = "identity", fill = "orange") + # Use one color for both bars labs(title = "Highest Marginal Income Tax Rate", x = "Year", y = "Tax Rate (%)") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + # Rotate x-axis labels theme(axis.line = element_line(colour = "black"), legend.position = "none") # Remove the legend