LA-1 PRESENTATION

Author

Puneeth, Tarun

Write an R program to create a lollipop chart comparing the Gross Domestic Product (GDP) of various Indian states using base R and ggplot2

📘 Objective

The objective of this R program is to visualize the GDP comparison across Indian states using a lollipop chart. The chart will display fictional GDP data for 10 states in India, sorted in ascending order. This visualization is created using base R for data manipulation and ggplot2 for plotting

STEP1:Load neccessary libraries

# Step 1: Install ggplot2 if not already installed
if (!require(ggplot2)) install.packages("ggplot2", dependencies = TRUE)
Loading required package: ggplot2

This line checks if the ggplot2 package is already installed. If not, it installs the package along with any required dependencies.

Step-2: Load ggplot2

library(ggplot2)

This line loads the ggplot2 package into the R session, making its functions and tools available for use in the current script

Step-3:create sample DDP data

gdp_data <- data.frame(
  State = c("Maharashtra", "Tamil Nadu", "Uttar Pradesh", "Gujarat", "Karnataka",
            "West Bengal", "Rajasthan", "Andhra Pradesh", "Madhya Pradesh", "Bihar"),
  GDP = c(35.3, 24.8, 23.5, 22.5, 21.1, 16.7, 12.5, 11.6, 11.2, 9.8) # In lakh crore INR
)

This code creates a data frame named gdp_data with two columns: State (a list of 10 Indian states) and GDP (the corresponding GDP values in lakh crore INR for each state). The data frame organizes this information into a structured format for further analysis and plotting

Step-4: sort data manually (base-R) for plotting
gdp_data <- gdp_data[order(gdp_data$GDP), ]
gdp_data$State <- factor(gdp_data$State, levels = gdp_data$State)
  • The first line (gdp_data <- gdp_data[order(gdp_data$GDP), ]) sorts the gdp_data data frame in ascending order based on the GDP column.

  • The second line (gdp_data$State <- factor(gdp_data$State, levels = gdp_data$State)) converts the State column into a factor while preserving the sorted order of the states. This ensures that when plotting, the states will appear in the same order as their GDP values.

Step-5: create lollipop chart
ggplot(gdp_data, aes(x = State, y = GDP)) +
  geom_segment(aes(x = State, xend = State, y = 0, yend = GDP), color = "skyblue", size = 1.5) +
  geom_point(color = "blue", size = 4) +
  coord_flip() +
  labs(title = "GDP Comparison Across Indian States (Sample Data)",
       x = "State",
       y = "GDP (Lakh Crore INR)") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, size = 16, face = "bold"))
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

  1. ggplot(gdp_data, aes(x = State, y = GDP)) initializes the plot, mapping State to the x-axis and GDP to the y-axis.

  2. geom_segment() creates vertical lines from the x-axis to each state’s GDP value.

  3. geom_point() adds blue circular points at the top of each line to represent the GDP.

  4. coord_flip() flips the axes, making the chart horizontal with states on the y-axis.

  5. labs(), theme_minimal(), and theme() customize the chart’s title, axes labels, and style for a clean appearance.