Graph Challenge 9

Author

Chien Tran

Published

April 29, 2025

The {plotly} package lets you turn your ggplot figures into interactive ones. You can learn more about it here: https://plotly.com/ggplot2/

For this graph challenge, I want you to create an interactive plot with the April 2025 DU survey data using ggplot and plotly. The type of plot is up to you. So is the variable or variables you’d like to show.

When you’re done, rather than hitting the “render” button, hit the “publish” button to publish your submission to RPubs. Then, submit the link to your publication on Canvas.

This website look into the political viewpoints of Denison University’s students

library(googlesheets4)
library(tidyr)
library(dplyr)
library(ggplot2)
library(plotly)
url <- "https://docs.google.com/spreadsheets/d/1npon5F_Gr40HQj8KVeHq_un7SOep4dzc6kjP0px7nLo/edit?usp=sharing"
gs4_deauth()
dt <- range_speedread(url)
# Convert Q2 to a factor with appropriate labels
dt$Q2 <- factor(dt$Q2, levels = 1:5,
                labels = c("Extremely interested", "Very interested",
                           "Moderately Interested", "Slightly interested",
                           "Not Interested"))

# Create the ggplot bar plot
p <- ggplot(dt, aes(x = Q2, fill = Q2)) +
  geom_bar() +
  labs(x = "Interest Level", y = "Number of Respondents",
       title = "How interested have you been in politics and the political campaigns so far this year?") +
  theme_minimal()

# Convert to plotly
fig <- ggplotly(p) %>% 
  layout(
    xaxis = list(visible = FALSE),  # Completely hide x-axis
    legend = list(title = list(text = ""))  # Ensure no legend title
  )
fig
# Convert Q3 to a factor with appropriate labels
dt$Q3 <- factor(dt$Q3, levels = 1:8,
                labels = c("Strong Democrat", "Democrat",
                           "Independent, but lean Democrat", "Independent",
                           "Independent, but lean Republican", "Republican",
                           "Strong Republican", "Other"))

# Create the ggplot bar plot
p <- ggplot(dt, aes(x = Q3, fill = Q3)) +
  geom_bar() +
  labs(x = "Party Identification", y = "Number of Respondents",
       title = "Which of these party labels best describes you?") +
  theme_minimal()

# Convert to plotly
fig <- ggplotly(p) %>% 
  layout(
    xaxis = list(visible = FALSE),  # Completely hide x-axis
    legend = list(title = list(text = ""))  # Ensure no legend title
  )
fig
# Remove NA values
dt <- dt %>%
  filter(!is.na(Q4))

# Convert Q4 to a factor with appropriate labels
dt$Q4 <- factor(dt$Q4, levels = 1:2,
                labels = c("Approve", "Disapprove"))

# Create the ggplot bar plot
p <- ggplot(dt, aes(x = Q4, fill = Q4)) +
  geom_bar() +
  labs(x = "Opinion", y = "Number of Respondents",
       title = "Do you approve or disapprove of the way that Donald Trump is handling his job as president?") +
  theme_minimal()

# Convert to plotly
fig <- ggplotly(p) %>% 
  layout(
    xaxis = list(visible = FALSE),  # Completely hide x-axis
    legend = list(title = list(text = ""))  # Ensure no legend title
  )
fig
# Convert Q34 to a factor with appropriate labels
dt$Q34 <- factor(dt$Q34, levels = 1:3,
                labels = c("Too much", "Just the right amount", "Not enough"))

# Create the ggplot bar plot
p <- ggplot(dt, aes(x = Q34, fill = Q34)) +
  geom_bar() +
  labs(x = "Opinion", y = "Number of Respondents",
       title = "Historically, how much do you think the US have spend on foreign assistance?") +
  theme_minimal()

# Convert to plotly
fig <- ggplotly(p) %>% 
  layout(
    xaxis = list(visible = FALSE),  # Completely hide x-axis
    legend = list(title = list(text = ""))  # Ensure no legend title
  )
fig
# Convert Q35 to a factor with appropriate labels
dt$Q35 <- factor(dt$Q35, levels = 1:3,
                labels = c("Too much", "Just the right amount", "Not enough"))

# Create the ggplot bar plot
p <- ggplot(dt, aes(x = Q35, fill = Q35)) +
  geom_bar() +
  labs(x = "Opinion", y = "Number of Respondents",
       title = "Historically, how much do you think the US have spend on foreign assistance? Foreign assistance typically makes up less than 1 percent of the federalb budget.") +
  theme_minimal()

# Convert to plotly
fig <- ggplotly(p) %>% 
  layout(
    xaxis = list(visible = FALSE),  # Completely hide x-axis
    legend = list(title = list(text = ""))  # Ensure no legend title
  )
fig
# First, prepare the data
dt_long <- dt %>%
  select(starts_with("Q5_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Question",
    values_to = "Response"
  ) %>%
  mutate(
    Response = factor(Response,
                     levels = 1:5,
                     labels = c("Strongly Agree", "Agree", "Neutral",
                                "Disagree", "Strongly Disagree")),
    Question = factor(Question,
                     levels = paste0("Q5_", 1:3), # Adjust based on actual number of questions
                     labels = c("Positive toward people who hold different viewpoints.", 
                                "Comfortable speaking my mind despite disagreement.", 
                                "Open to listening and changing my perspective."))
  ) %>%
  filter(!is.na(Response)) %>%  # Remove NA responses
  group_by(Question, Response) %>%
  summarise(Count = n(), .groups = "drop") %>%
  group_by(Question) %>%
  mutate(Percentage = Count / sum(Count) * 100)

# Create the stacked bar plot
p <- ggplot(dt_long, aes(x = Question, y = Count, fill = Response,
                        text = paste("Count:", Count, "<br>Percentage:", round(Percentage, 1), "%"))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) + # Red-Yellow-Green palette
  labs(title = "Participant Responses to Statements",
       x = "Statements",
       y = "Number of Participants") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "right")

# Convert to interactive plotly
fig <- ggplotly(p, tooltip = "text") %>%
  layout(
    xaxis = list(title = "Statements"),
    yaxis = list(title = "Number of Participants"),
    legend = list(title = list(text = "Response")),
    margin = list(b = 100) # Adjust bottom margin for x-axis labels
  )

fig
# First, prepare the data
dt_long <- dt %>%
  select(starts_with("Q12_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Question",
    values_to = "Response"
  ) %>%
  mutate(
    Response = factor(Response,
                     levels = 1:4,
                     labels = c("Never", "Rarely",
                                "Often", "Sometimes")),
    Question = factor(Question,
                     levels = paste0("Q12_", 1:4), # Adjust based on actual number of questions
                     labels = c("Have you complain about Denison outside class this semester?", 
                                "Have you complain about Denison in class this semester?", 
                                "Have others complain about Denison outside class this semester?", 
                                "Have others complain about Denison in class this semester?"))
  ) %>%
  filter(!is.na(Response)) %>%  # Remove NA responses
  group_by(Question, Response) %>%
  summarise(Count = n(), .groups = "drop") %>%
  group_by(Question) %>%
  mutate(Percentage = Count / sum(Count) * 100)

# Create the stacked bar plot
p <- ggplot(dt_long, aes(x = Question, y = Count, fill = Response,
                        text = paste("Count:", Count, "<br>Percentage:", round(Percentage, 1), "%"))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) + # Red-Yellow-Green palette
  labs(title = "Participant Responses to Statements",
       x = "Statements",
       y = "Number of Participants") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "right")

# Convert to interactive plotly
fig <- ggplotly(p, tooltip = "text") %>%
  layout(
    xaxis = list(title = "Statements"),
    yaxis = list(title = "Number of Participants"),
    legend = list(title = list(text = "Response")),
    margin = list(b = 100) # Adjust bottom margin for x-axis labels
  )

fig
# First, prepare the data
dt_long <- dt %>%
  select(starts_with("Q14_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Question",
    values_to = "Response"
  ) %>%
  mutate(
    Response = factor(Response,
                     levels = 1:5,
                     labels = c("Extremely important", "Very important",
                                "Moderately important", "Somewhat important", "Not at all important")),
    Question = factor(Question,
                     levels = paste0("Q14_", 1:4), # Adjust based on actual number of questions
                     labels = c("Students feel comfortable expressing political opinions on campus?", 
                                "Students feel comfortable expressing political opinions in class?", 
                                "Hearing different political opinions than yours from students?", 
                                "Hearing different political opinions than yours from faculties?"))
  ) %>%
  filter(!is.na(Response)) %>%  # Remove NA responses
  group_by(Question, Response) %>%
  summarise(Count = n(), .groups = "drop") %>%
  group_by(Question) %>%
  mutate(Percentage = Count / sum(Count) * 100)

# Create the stacked bar plot
p <- ggplot(dt_long, aes(x = Question, y = Count, fill = Response,
                        text = paste("Count:", Count, "<br>Percentage:", round(Percentage, 1), "%"))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) + # Red-Yellow-Green palette
  labs(title = "How important is it to you that...",
       x = "Statements",
       y = "Number of Participants") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "right")

# Convert to interactive plotly
fig <- ggplotly(p, tooltip = "text") %>%
  layout(
    xaxis = list(title = "Statements"),
    yaxis = list(title = "Number of Participants"),
    legend = list(title = list(text = "Response")),
    margin = list(b = 100) # Adjust bottom margin for x-axis labels
  )

fig
# First, prepare the data
dt_long <- dt %>%
  select(starts_with("Q15_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Question",
    values_to = "Response"
  ) %>%
  mutate(
    Response = factor(Response,
                     levels = 1:5,
                     labels = c("Strongly Agree", "Agree", "Neutral",
                                "Disagree", "Strongly Disagree")),
    Question = factor(Question,
                     levels = paste0("Q15_", 1:4), # Adjust based on actual number of questions
                     labels = c("Having intense political debate.", 
                                "Leading an organization.", 
                                "Having racially diverse friendships.",
                                "Taking courses outside of your comfort zone."))
  ) %>%
  filter(!is.na(Response)) %>%  # Remove NA responses
  group_by(Question, Response) %>%
  summarise(Count = n(), .groups = "drop") %>%
  group_by(Question) %>%
  mutate(Percentage = Count / sum(Count) * 100)

# Create the stacked bar plot
p <- ggplot(dt_long, aes(x = Question, y = Count, fill = Response,
                        text = paste("Count:", Count, "<br>Percentage:", round(Percentage, 1), "%"))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) + # Red-Yellow-Green palette
  labs(title = "What is essential to a liberal arts education?",
       x = "Statements",
       y = "Number of Participants") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "right")

# Convert to interactive plotly
fig <- ggplotly(p, tooltip = "text") %>%
  layout(
    xaxis = list(title = "Statements"),
    yaxis = list(title = "Number of Participants"),
    legend = list(title = list(text = "Response")),
    margin = list(b = 100) # Adjust bottom margin for x-axis labels
  )

fig
# First, prepare the data
dt_long <- dt %>%
  select(starts_with("Q16_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Question",
    values_to = "Response"
  ) %>%
  mutate(
    Response = factor(Response,
                     levels = 1:5,
                     labels = c("Strongly Agree", "Agree", "Neutral",
                                "Disagree", "Strongly Disagree")),
    Question = factor(Question,
                     levels = paste0("Q16_", 1:8), # Adjust based on actual number of questions
                     labels = c("I am different from Democrats.", 
                                "I feel distant from Democrats", 
                                "I can't see the world the way Democrats do.",
                                "My feelings toward Democrats are negative.",
                                "I hate Democrats",
                                "Democrats are immoral.",
                                "Democrats are evil.",
                                "Democrats lack integrity."))
  ) %>%
  filter(!is.na(Response)) %>%  # Remove NA responses
  group_by(Question, Response) %>%
  summarise(Count = n(), .groups = "drop") %>%
  group_by(Question) %>%
  mutate(Percentage = Count / sum(Count) * 100)

# Create the stacked bar plot
p <- ggplot(dt_long, aes(x = Question, y = Count, fill = Response,
                        text = paste("Count:", Count, "<br>Percentage:", round(Percentage, 1), "%"))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) + # Red-Yellow-Green palette
  labs(title = "Do you agree or disagree with these statements about your feelings toward the Democrats Party?",
       x = "Statements",
       y = "Number of Participants") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "right")

# Convert to interactive plotly
fig <- ggplotly(p, tooltip = "text") %>%
  layout(
    xaxis = list(title = "Statements"),
    yaxis = list(title = "Number of Participants"),
    legend = list(title = list(text = "Response")),
    margin = list(b = 100) # Adjust bottom margin for x-axis labels
  )

fig
# First, prepare the data
dt_long <- dt %>%
  select(starts_with("Q17_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Question",
    values_to = "Response"
  ) %>%
  mutate(
    Response = factor(Response,
                     levels = 1:5,
                     labels = c("Strongly Agree", "Agree", "Neutral",
                                "Disagree", "Strongly Disagree")),
    Question = factor(Question,
                     levels = paste0("Q17_", 1:8), # Adjust based on actual number of questions
                     labels = c("I am different from Republicans.", 
                                "I feel distant from Republicans.", 
                                "I can't see the world the way Republicans do.",
                                "My feelings toward Republicans are negative.",
                                "I hate Republicans.",
                                "Republicans are immoral.",
                                "Republicans are evil.",
                                "Republicans lack integrity."))
  ) %>%
  filter(!is.na(Response)) %>%  # Remove NA responses
  group_by(Question, Response) %>%
  summarise(Count = n(), .groups = "drop") %>%
  group_by(Question) %>%
  mutate(Percentage = Count / sum(Count) * 100)

# Create the stacked bar plot
p <- ggplot(dt_long, aes(x = Question, y = Count, fill = Response,
                        text = paste("Count:", Count, "<br>Percentage:", round(Percentage, 1), "%"))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) + # Red-Yellow-Green palette
  labs(title = "Do you agree or disagree with these statements about your feelings toward the Republican Party?",
       x = "Statements",
       y = "Number of Participants") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "right")

# Convert to interactive plotly
fig <- ggplotly(p, tooltip = "text") %>%
  layout(
    xaxis = list(title = "Statements"),
    yaxis = list(title = "Number of Participants"),
    legend = list(title = list(text = "Response")),
    margin = list(b = 100) # Adjust bottom margin for x-axis labels
  )

fig
# First, prepare the data
dt_long <- dt %>%
  select(starts_with("Q29_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Question",
    values_to = "Response"
  ) %>%
  mutate(
    Response = factor(Response,
                     levels = 1:5,
                     labels = c("Strongly Agree", "Agree", "Neutral",
                                "Disagree", "Strongly Disagree")),
    Question = factor(Question,
                     levels = paste0("Q29_", 1:8), # Adjust based on actual number of questions
                     labels = c("The attempted assassination on Trump was staged.", 
                                "The 2020 election was rigged.", 
                                "Taylor Swift was part of a conspiracy to help Joe Biden win the election.",
                                "John F Kennedy was assassinated by a conspiracy, rather than a single gunman.",
                                "Donald Trump colluded with Russia to illegally rig the 2016 election.",
                                "Covid was intentionally created by China in a lab.", 
                                "No matter how hard I work I can't get ahead because Denison professors play favorites.",
                                "NA"))
  ) %>%
  filter(!is.na(Response)) %>%  # Remove NA responses
  group_by(Question, Response) %>%
  summarise(Count = n(), .groups = "drop") %>%
  group_by(Question) %>%
  mutate(Percentage = Count / sum(Count) * 100)

# Create the stacked bar plot
p <- ggplot(dt_long, aes(x = Question, y = Count, fill = Response,
                        text = paste("Count:", Count, "<br>Percentage:", round(Percentage, 1), "%"))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) + # Red-Yellow-Green palette
  labs(title = "Participant Responses to Statements",
       x = "Statements",
       y = "Number of Participants") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "right")

# Convert to interactive plotly
fig <- ggplotly(p, tooltip = "text") %>%
  layout(
    xaxis = list(title = "Statements"),
    yaxis = list(title = "Number of Participants"),
    legend = list(title = list(text = "Response")),
    margin = list(b = 100) # Adjust bottom margin for x-axis labels
  )

fig
# First, prepare the data
dt_long <- dt %>%
  select(starts_with("Q33_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Question",
    values_to = "Response"
  ) %>%
  mutate(
    Response = factor(Response,
                     levels = 1:5,
                     labels = c("Very well", "Well", "Neither well nor poorly",
                                "Poorly", "Very poorly")),
    Question = factor(Question,
                     levels = paste0("Q33_", 1:4), # Adjust based on actual number of questions
                     labels = c("Foreign policy.", 
                                "Domestic economic policy.", 
                                "Student loans.",
                                "NA"))
  ) %>%
  filter(!is.na(Response)) %>%  # Remove NA responses
  group_by(Question, Response) %>%
  summarise(Count = n(), .groups = "drop") %>%
  group_by(Question) %>%
  mutate(Percentage = Count / sum(Count) * 100)

# Create the stacked bar plot
p <- ggplot(dt_long, aes(x = Question, y = Count, fill = Response,
                        text = paste("Count:", Count, "<br>Percentage:", round(Percentage, 1), "%"))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) + # Red-Yellow-Green palette
  labs(title = "How has President Trump performed in the following areas during his first few months in
office?",
       x = "Statements",
       y = "Number of Participants") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "right")

# Convert to interactive plotly
fig <- ggplotly(p, tooltip = "text") %>%
  layout(
    xaxis = list(title = "Statements"),
    yaxis = list(title = "Number of Participants"),
    legend = list(title = list(text = "Response")),
    margin = list(b = 100) # Adjust bottom margin for x-axis labels
  )

fig
# First, prepare the data
dt_long <- dt %>%
  select(starts_with("Q53_")) %>%
  pivot_longer(
    cols = everything(),
    names_to = "Question",
    values_to = "Response"
  ) %>%
  mutate(
    Response = factor(Response,
                     levels = 1:5,
                     labels = c("Strongly Agree", "Agree", "Neutral",
                                "Disagree", "Strongly Disagree")),
    Question = factor(Question,
                     levels = paste0("Q53_", 1:4), # Adjust based on actual number of questions
                     labels = c("Christians are a divisive force in the US today.", 
                                "Christians seem to want the US to be a Christian nation.", 
                                "The US would be better off without so much religion in public life.",
                                "I don't often think about religion."))
  ) %>%
  filter(!is.na(Response)) %>%  # Remove NA responses
  group_by(Question, Response) %>%
  summarise(Count = n(), .groups = "drop") %>%
  group_by(Question) %>%
  mutate(Percentage = Count / sum(Count) * 100)

# Create the stacked bar plot
p <- ggplot(dt_long, aes(x = Question, y = Count, fill = Response,
                        text = paste("Count:", Count, "<br>Percentage:", round(Percentage, 1), "%"))) +
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_brewer(palette = "RdYlGn", direction = -1) + # Red-Yellow-Green palette
  labs(title = "What is your view of Christians in public affairs in the US today?",
       x = "Statements",
       y = "Number of Participants") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "right")

# Convert to interactive plotly
fig <- ggplotly(p, tooltip = "text") %>%
  layout(
    xaxis = list(title = "Statements"),
    yaxis = list(title = "Number of Participants"),
    legend = list(title = list(text = "Response")),
    margin = list(b = 100) # Adjust bottom margin for x-axis labels
  )

fig