Load the ggplot2 package for plotting and the dplyr package for data manipulation
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Creating a data frame with yearly data on favorable views
Constructing the plot
plot <- ggplot(data, aes(x = year, y = percentage)) +
geom_point(size = 3, color = 'grey') + # Points with customized size and color
geom_smooth(method = "lm", color = 'black', fill = 'grey90', se = TRUE) + # Linear model with confidence band
scale_x_continuous(breaks = seq(2012, 2024, 2)) + # Ensure all years are included on the x-axis
labs(title = "Favorable Views of the United States in Tunisia",
subtitle = "Source: Pew Research Center | Visualization: M.D.Hammami",
x = "Year", y = "% Favorable Views") +
theme_minimal() +
theme(
plot.title = element_text(size = 15, face = "bold"),
axis.title = element_text(size = 16),
axis.text = element_text(size = 12),
legend.position = "none"
)
print(plot)
## `geom_smooth()` using formula = 'y ~ x'