The line plot is excellent for showing trends over time, such as how pce (Personal Consumption Expenditures) changes.
ggplot(economics, aes(x = date, y = pce)) +
geom_line(color = "blue") +
labs(title = "Personal Consumption Expenditures Over Time", x = "Date", y = "PCE")A scatter plot can help you examine the relationship between pop (population) and pce.
ggplot(economics, aes(x = pop, y = pce)) +
geom_point(color = "darkgreen") +
labs(title = "Relationship Between Population and PCE", x = "Population", y = "PCE") +
geom_smooth(method = "lm", color = "red", se = FALSE)## `geom_smooth()` using formula = 'y ~ x'
A histogram visualizes the distribution of the personal savings rate (psavert).
ggplot(economics, aes(x = psavert)) +
geom_histogram(bins = 30, fill = "purple", alpha = 0.7) +
labs(title = "Distribution of Personal Savings Rate", x = "Personal Savings Rate", y = "Frequency")A density plot gives a smooth distribution of the median unemployment duration.
ggplot(economics, aes(x = uempmed)) +
geom_density(fill = "lightblue", alpha = 0.5) +
labs(title = "Density Plot of Median Unemployment Duration", x = "Median Unemployment Duration", y = "Density")A box plot shows the distribution and outliers of the number of unemployed.
ggplot(economics, aes(x = factor(1), y = unemploy)) +
geom_boxplot(fill = "orange") +
labs(title = "Box Plot of Unemployment", x = "", y = "Unemployment (Thousands)") +
theme(axis.text.x = element_blank())Bar plots are useful for categorical variables, such as if we were to group dates by year and examine average unemployment rates.
economics$year <- format(as.Date(economics$date), "%Y")
# Calculate the average number of unemployed per year
yearly_unemploy <- aggregate(unemploy ~ year, economics, mean)
ggplot(yearly_unemploy, aes(x = year, y = unemploy)) +
geom_bar(stat = "identity", fill = "darkblue") +
labs(title = "Average Unemployment by Year", x = "Year", y = "Average Unemployment (in thousands)") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))Choose specific years for analysis or categorize years based on events or other criteria. Calculate the proportion of unemployment for each year (or other categories you’re interested in).
Plot the data as a donut chart.
# Prepare the data by extracting year and calculating the average unemployment per year
economics$year <- format(as.Date(economics$date), "%Y")
yearly_unemploy <- economics %>%
group_by(year) %>%
summarise(average_unemploy = mean(unemploy)) %>%
filter(year %in% c("1970", "1980", "1990", "2000", "2010", "2020")) # Select specific years
# Calculate proportion of each year's average unemployment
total_unemployment <- sum(yearly_unemploy$average_unemploy)
yearly_unemploy <- yearly_unemploy %>%
mutate(proportion = average_unemploy / total_unemployment)
# Create the donut plot
ggplot(yearly_unemploy, aes(x = 2, y = proportion, fill = year)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y") +
xlim(1, 2.5) + # This controls the thickness of the donut ring
theme_void() +
labs(title = "Proportion of Average Unemployment by Year") +
theme(legend.position = "right") +
scale_fill_brewer(palette = "Set3")