R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

# List of packages
packages <- c("tidyverse", "fst", "modelsummary") # add any you need here

# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load the packages
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "fst"       "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "modelsummary" "fst"          "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"
germany_data <- read_fst("germany_data.fst")

step one :

#first find how many people in germany are right-winged voters and how many are left-winged voters for their Government. #from “extreme left,” to “left,” to “moderate,” to “right,” and lastly to “extreme right.”

germany_data <- germany_data %>%
  mutate(
    Ideology = case_when(
      lrscale >= 1 & lrscale <= 2 ~ "Extreme Left",
      lrscale > 2 & lrscale <= 4 ~ "Left",
      lrscale > 4 & lrscale <= 6 ~ "Moderate",
      lrscale > 6 & lrscale <= 8 ~ "Right",
      lrscale > 8 & lrscale <= 10 ~ "Extreme Right",
      TRUE ~ NA_character_  # Keeps NA values as is, ensuring character type
    )
  )
ideology_counts <- germany_data %>%
  filter(!is.na(Ideology)) %>%
  count(Ideology)

print(ideology_counts)
##        Ideology     n
## 1  Extreme Left  3442
## 2 Extreme Right   652
## 3          Left  8787
## 4      Moderate 14792
## 5         Right  3198
library(ggplot2)

ggplot(ideology_counts, aes(x = Ideology, y = n, fill = Ideology)) + 
  geom_bar(stat = "identity") +
  theme_minimal() +
  labs(
    title = "Count of Political Ideologies in Germany",
    x = "Ideology",
    y = "Count",
    fill = "Ideology"
  ) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1)  
  )

# this is to create a visualization along with the provided table, with the X and Y axises named conveniently and a title provided on the top. 

outcome

the reseults are as followed

  1. extreme left : 3,442 individuals

  2. left : 8,787 individuals

  3. moderate : 14,792 individuals

  4. right : 3,198 individuals

  5. extreme right : 652 individuals

in conclusion, most people in germany are pretty moderate when it comes to their political ideologies, however, a good number still consider themselves to be quite left-winged, with just a few less being extremely left-winged. a very small number of individuals are extremely right winged.

next, we’ll calculate the number of people that’d be ashamed if they had a homosexual family member using the “hmsfmlsh” variable from ESS.

germany_data <- germany_data %>%
  mutate(
    progressiveness = case_when(
      hmsfmlsh == 1 ~ "extremely ashamed",
      hmsfmlsh == 2 ~ "ashamed",
      hmsfmlsh == 3 ~ "indifferent",
      hmsfmlsh == 4 ~ "supportive",
      hmsfmlsh == 5 ~ "extremely supportive",
      TRUE ~ NA_character_  
    )
  )

#this coding will place individuals according to their number on the hmsfmlsh scale. 

#now to print the numbers

progressiveness_counts <- germany_data %>%
  filter(!is.na(progressiveness)) %>%
  count(progressiveness)

print(progressiveness_counts)
##        progressiveness    n
## 1              ashamed  671
## 2    extremely ashamed  280
## 3 extremely supportive 7136
## 4          indifferent 3057
## 5           supportive 2607

#and to visualize them on a graph

library(ggplot2)

progressiveness_counts <- germany_data %>%
  filter(!is.na(progressiveness)) %>%
  count(progressiveness)

ggplot(progressiveness_counts, aes(x = progressiveness, y = n, fill = progressiveness)) + 
  geom_bar(stat = "identity") +
  scale_fill_brewer(palette = "Set3") +  
  theme_minimal() +
  labs(
    title = "Attitudes Towards Homosexuality in Family Members",
    x = "Level of Progressiveness",
    y = "Count",
    fill = "Level of Progressiveness"
  ) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  #this rotates x-axis text for readability

outcome

the results are as followed

  1. extremely ashamed : 280 individuals

  2. ashamed : 671 individuals

  3. indifferent 3,057 individuals

  4. supportive : 2,607 individuals

  5. extremely supportive : 7,136 individuals.

in conclusion, most people in germany would be extremely supportive if they had a homosexual family member, and very very few wouuld be extremely ashamed by it.