我對在 R 中制作漂亮的圖有點陌生。現在我想制作堆疊條形圖。這是堆疊條形圖的代碼:
longer_data = structure(list(question =
c("HMI1_Speed_and_distance_control",
"HMI2_Supercruise_Speed_and_distance_control",
"HMI3_Speed_and_distance_control", "HMI4_Speed_and_distance_control",
"HMI5_Speed_and_distance_control", "HMI6_Speed_and_distance_control",
"HMI1_Speed_and_distance_control",
"HMI2_Supercruise_Speed_and_distance_control",
"HMI3_Speed_and_distance_control",
"HMI4_Speed_and_distance_control"), response = c(3L, 3L, 1L, 1L, 2L,
1L, 2L, 3L, 1L, 3L)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
longer_data %>%
ggplot(aes(x = response))
geom_bar()
facet_wrap(vars(question), ncol = 3)
labs(x = "Response (on a 1 to 5 scale)", y = "Number of respondents")
我希望每個圖形的三個條形相互重疊,而不是彼此相鄰。我怎么做?
謝謝!
uj5u.com熱心網友回復:
我創建了一個因子變數并關閉了 facet wrap
longer_data$question<- factor(longer_data$question)
longer_data %>%
ggplot(aes( x=response, fill=question))
geom_bar(stat="count")
# facet_wrap(vars(question), ncol = 3)
labs(x = "Response (on a 1 to 5 scale)", y = "Number of respondents")

uj5u.com熱心網友回復:
我猜測 1 是是,2 是否,3 是不知道,如有必要,您應該更正這些。
longer_data %>%
mutate(response = factor(response, levels = 1:3, labels = c("Yes", "No", "Don't Know"))) %>%
ggplot(aes(x = question, fill = response))
geom_bar()
scale_fill_manual(values = c("Yes" = "forestgreen", "No" = "firebrick2", "Don't Know" = "purple"))
theme(axis.text.x = element_text(angle = -90, hjust = 0))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381259.html
