library(readr)
library(ggplot2)
data <- read_csv("Desktop/RWorkingDirectory/CIRI_data.csv")
## Rows: 6262 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): CTRY
## dbl (3): YEAR, PHYSINT, polity2
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data
## # A tibble: 6,262 × 4
##    CTRY         YEAR PHYSINT polity2
##    <chr>       <dbl>   <dbl>   <dbl>
##  1 Afghanistan  1981       0      NA
##  2 Afghanistan  1982       0      NA
##  3 Afghanistan  1983       0      NA
##  4 Afghanistan  1984       0      NA
##  5 Afghanistan  1985       0      NA
##  6 Afghanistan  1986       0      NA
##  7 Afghanistan  1987       0      NA
##  8 Afghanistan  1988       0      NA
##  9 Afghanistan  1989       0      -8
## 10 Afghanistan  1990       0      -8
## # ℹ 6,252 more rows
# Define El Salvador and comparison countries
comparison_countries <- c("Brazil", "Canada", "Ukraine")
el_salvador_data <- subset(data, CTRY == "El Salvador")
comparison_data <- subset(data, CTRY %in% c("El Salvador", comparison_countries))

# Plot the time trend
ggplot() +
  geom_line(data = comparison_data, aes(x = YEAR, y = PHYSINT, color = CTRY, group = CTRY), size = 1) +
  geom_vline(xintercept = 1992, linetype = "dashed", color = "black", size = 1) +
  
  # Set plot labels and title
  labs(x = "Year", y = "Human Rights Score", title = "Time Trend in Human Rights Scores: El Salvador and Comparison Countries", subtitle = "Dashed line indicates El Salvador's civil war (Jan. 1992)") +
  
  # Customize legend
  scale_color_manual(values = c("green3", "pink", "red", "orange"), name = "Country",
                     labels = c("Brazil", "Canada", "El Salvador", "Ukraine")) +
  
  # Add grid lines
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 11 rows containing missing values (`geom_line()`).

geom_line is used to add line layers to the plot. It plots human rights scores over the years for each country, with different colors for each country. The group aesthetic is used to connect points within each country.

geom_vline adds a vertical dashed line at the x-coordinate 1992, representing the end of El Salvador’s civil war.

The labs function is used to set the x-axis label, y-axis label, main title, and subtitle for the plot.

scale_color_manual customizes the legend. It assigns specific colors to each country and sets the legend title and labels.

# Scatterplot for 1981
scatterplot_1981 <- ggplot(data[data$YEAR == 1981, ], aes(x = polity2, y = PHYSINT)) +
  geom_point(size = 3, alpha = 0.7) +
  geom_point(data = subset(data, CTRY == "El Salvador" & YEAR == 1981), size = 5, color = "red", alpha = 0.7) +
  labs(title = "El Salvador 1981 vs. 1992: Human Rights Scores vs Democracy Scores",
       x = " Democracy Score",
       y = " Human Rights Score") +
  theme_minimal()


# Scatterplot for 1992
scatterplot_1992 <- ggplot(data[data$YEAR == 1992, ], aes(x = polity2, y = PHYSINT)) +
  geom_point(size = 3, alpha = 0.7) +
  geom_point(data = subset(data, CTRY == "El Salvador" & YEAR == 1992), size = 5, color = "red", alpha = 0.7) +
  labs(title = "",
       x = "",
       y = "") +
  theme_minimal()

library(gridExtra)
grid.arrange(scatterplot_1981, scatterplot_1992, ncol = 2)
## Warning: Removed 78 rows containing missing values (`geom_point()`).
## Warning: Removed 59 rows containing missing values (`geom_point()`).

1981:

The scatterplot is created for the year 1981 using data[data$YEAR == 1981,]. Another set of points is added using geom_point for El Salvador specifically in 1981.

The default points for all countries have a size of 3 and transparency (alpha) of 0.7. The points for El Salvador in 1981 have a larger size (5) and are colored red.

The plot has a title, x-axis label, and y-axis label set using labs. The title indicates that the scatterplot compares El Salvador in 1981 and 1992 regarding human rights scores and democracy scores.

The plot theme is set to minimal using theme_minimal().

1992:

Similar to the 1981 plot, this scatterplot is for the year 1992.

Similar to the 1981 plot, the default points have a size of 3 and transparency of 0.7. The points for El Salvador in 1992 have a larger size (5) and are colored red.

The titles and labels are intentionally left blank.

The theme is also set to minimal.