R 代码 - 绘制调查响应图

问题描述 投票:0回答:1

我正在尝试用我的数据制作一些条形图,但我总是遇到一些困难。如果这里有人可以提供帮助那就太好了

我想创建并排比较前后响应的条形图,我需要 x 轴作为响应类型(同意/不同意等),y 轴作为响应的百分比

这感觉非常基础,但却很困难。任何有关 R 代码使用的帮助都会很棒。我还提供了我使用的数据框。

data<- structure(list(Sample = c("1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", 
"41", "42", "43"), ID = c("Resident", "Resident", "City_personal", 
"Manager_staff", "Resident", "Manager_staff", "Manager_staff", 
"Manager_staff", "Resident", "Manager_staff", "Manager_staff", 
"Manager_staff", "Manager_staff", "Manager_staff", "Manager_staff", 
"Manager_staff", "Manager_staff", "City_personal", "Resident", 
"Manager_staff", "Resident", "Manager_staff", "Resident", "Resident", 
"City_personal", "Manager_staff", "Resident", "Resident", "Resident", 
"Manager_staff", "City_personal", "City_personal", "City_personal", 
"City_personal", "City_personal", "City_personal", "City_personal", 
"City_personal", "City_personal", "City_personal", "City_personal", 
"City_personal", "City_personal"), envir_conscious = c("Strongly_agree", 
"Strongly_agree", "Agree", "Agree", "Agree", "Strongly_agree", 
"Strongly_agree", "Strongly_disagree", "Neutral", "Agree", "Agree", 
"Agree", "Agree", "Strongly_disagree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Strongly_agree", "Agree", 
"Strongly_agree", "Strongly_agree", "Agree", "Strongly_disagree", 
"Agree", "Agree", "Strongly_agree", "Strongly_disagree", "Agree", 
"Strongly_agree", "Strongly_agree", "Agree", "Agree", "Agree", 
"Neutral", "Strongly_agree", "Strongly_agree", "Strongly_agree", 
"Agree", "Agree", "Agree", "Strongly_agree", "Agree"), PRE_understand = c("Agree", 
"Strongly_agree", "Agree", "Agree", "Agree", "Agree", "Strongly_agree", 
"Strongly_disagree", "Neutral", "Agree", "Neutral", "Agree", 
"Disagree", "Strongly_disagree", "Agree", "Strongly_agree", "Agree", 
"Strongly_agree", "Strongly_agree", "Neutral", "Strongly_agree", 
"Strongly_agree", "Agree", "Neutral", "Neutral", "Neutral", "Agree", 
"Neutral", "Neutral", "Strongly_agree", "Agree", "Agree", "Agree", 
"Agree", "Neutral", "Agree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Agree", "Agree", "Neutral"
), PRE_serious_prob = c("Strongly_agree", "Strongly_agree", "Agree", 
"Agree", "Agree", "Agree", "Strongly_agree", "Strongly_disagree", 
"Neutral", "Neutral", "Strongly_agree", "Agree", "Neutral", "Strongly_disagree", 
"Agree", "Strongly_agree", "Unsure", "Strongly_agree", "Strongly_agree", 
"Agree", "Strongly_agree", "Strongly_agree", "Agree", "Neutral", 
"Strongly_agree", "Neutral", "Agree", "Neutral", "Neutral", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Strongly_agree", "Agree", 
"Agree", "Agree", "Strongly_agree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Agree", "Neutral"), PRE_confident = c("Agree", 
"Strongly_agree", "Agree", "Agree", "Agree", "Agree", "Strongly_agree", 
"Disagree", "Neutral", "Agree", "Strongly_agree", "Agree", "Neutral", 
"Strongly_disagree", "Agree", "Strongly_agree", "Agree", "Strongly_agree", 
"Strongly_agree", "Disagree", "Strongly_agree", "Strongly_agree", 
"Agree", "Neutral", "Neutral", "Unsure", "Agree", "Neutral", 
"Neutral", "Strongly_agree", "Strongly_agree", "Neutral", "Neutral", 
"Neutral", "Agree", "Agree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Agree", "Agree", "Neutral"
), PRE_willing = c("Strongly_agree", "Strongly_agree", "Agree", 
"Neutral", "Agree", "Agree", "Strongly_agree", "Strongly_disagree", 
"Neutral", "Agree", "Strongly_agree", "Agree", "Agree", "Strongly_disagree", 
"Agree", "Strongly_agree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Agree", "Strongly_agree", "Strongly_agree", 
"Agree", "Strongly_disagree", "Agree", "Unsure", "Agree", "Strongly_disagree", 
"Agree", "Strongly_agree", "Strongly_agree", "Strongly_agree", 
"Agree", "Agree", "Agree", "Agree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Agree", "Agree", "Agree", "Agree"), PRE_attain = c("Neutral", 
"Strongly_agree", "Agree", "Agree", "Agree", "Agree", "Strongly_agree", 
"Disagree", "Neutral", "Agree", "Strongly_agree", "Agree", "Neutral", 
"Strongly_disagree", "Strongly_agree", "Agree", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Agree", "Strongly_agree", 
"Strongly_agree", "Agree", "Neutral", "Strongly_agree", "Agree", 
"Agree", "Neutral", "Unsure", "Strongly_agree", "Agree", "Strongly_agree", 
"Agree", "Agree", "Agree", "Strongly_agree", "Agree", "Disagree", 
"Unsure", "Strongly_agree", "Agree", "Agree", "Agree"), res_time = c("less_than_5_yrs", 
"less_than_5_yrs", "less_than_5_yrs", "10_yrs", "less_than_5_yrs", 
"5_to_10_yrs", "less_than_5_yrs", "10_yrs", "10_yrs", "less_than_5_yrs", 
"less_than_5_yrs", "less_than_5_yrs", "less_than_5_yrs", "10_yrs", 
"less_than_5_yrs", "less_than_5_yrs", "less_than_5_yrs", "less_than_5_yrs", 
"5_to_10_yrs", "less_than_5_yrs", "10_yrs", "5_to_10_yrs", "10_yrs", 
"10_yrs", "less_than_5_yrs", "10_yrs", "10_yrs", "10_yrs", "10_yrs", 
"less_than_5_yrs", "10_yrs", "less_than_5_yrs", "less_than_5_yrs", 
"less_than_5_yrs", "less_than_5_yrs", "5_to_10_yrs", "less_than_5_yrs", 
"less_than_5_yrs", "5_to_10_yrs", "less_than_5_yrs", "less_than_5_yrs", 
"10_yrs", "less_than_5_yrs"), nature_feeling = c("Yes", "Yes", 
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes_IDK", "Yes", "Yes", 
"Yes_IDK", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "Yes", 
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "No", 
"Yes", "Yes_IDK", "Yes", "Yes", "Yes", "No", "Yes", "No", "Yes", 
"Yes", "Yes", "Yes", "Yes_IDK", "Yes"), seen_turtle = c("Yes", 
"No", "Yes", "Yes", "Yes", "Yes", "Yes", "No", "No", "Yes", "Yes", 
"Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "Yes", "Yes", 
"Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Yes", 
"Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", 
"No", "Yes", "Yes", "Yes"), active_envir = c("Yes_less_than_3", 
"First", "First", "Yes_less_than_3", "Yes_over_1", "Yes_over_1", 
"Yes_less_than_3", "First", "Unsure", "First", "Yes_over_1", 
"First", "Unsure", "First", "First", "Yes_more_than_3", "Yes_over_1", 
"Yes_over_1", "Yes_more_than_3", "Unsure", "Yes_more_than_3", 
"First", "Yes_more_than_3", "First", "Yes_less_than_3", "Yes_less_than_3", 
"Yes_over_1", "First", "Yes_over_1", "Yes_over_1", "Yes_over_1", 
"Yes_over_1", "Yes_over_1", "First", "First", "Yes_over_1", "First", 
"Yes_over_1", "Yes_over_1", "Yes_over_1", "First", "Yes_over_1", 
"Yes_more_than_3"), Too_Expensive = c("Unsure", "Strongly_disagree", 
"Agree", "Neutral", "Neutral", "Unsure", "Strongly_disagree", 
"Disagree", "Disagree", "Disagree", "Disagree", "Disagree", "Neutral", 
"Disagree", "Disagree", "Disagree", "Disagree", "Unsure", "Agree", 
"Neutral", "Disagree", "Strongly_disagree", "Disagree", "Strongly_disagree", 
"Strongly_disagree", "Neutral", "Disagree", "Strongly_disagree", 
"Strongly_disagree", "Disagree", "Neutral", "Neutral", "Neutral", 
"Disagree", "Neutral", "Neutral", "Neutral", "Strongly_disagree", 
"Disagree", "Strongly_disagree", "Strongly_disagree", "Neutral", 
"Neutral"), Not_aesthetic = c("Strongly_disagree", "Strongly_disagree", 
"Agree", "Disagree", "Neutral", "Unsure", "Strongly_disagree", 
"Disagree", "Strongly_disagree", "Disagree", "Strongly_disagree", 
"Disagree", "Strongly_disagree", "Disagree", "Strongly_disagree", 
"Disagree", "Disagree", "Neutral", "Disagree", "Neutral", "Disagree", 
"Strongly_disagree", "Disagree", "Strongly_disagree", "Strongly_disagree", 
"Unsure", "Strongly_disagree", "Strongly_disagree", "Strongly_disagree", 
"Disagree", "Unsure", "Strongly_disagree", "Disagree", "Disagree", 
"Neutral", "Unsure", "Neutral", "Strongly_disagree", "Disagree", 
"Disagree", "Strongly_disagree", "Neutral", "Neutral"), Safety_concern = c("Strongly_disagree", 
"Strongly_disagree", "Agree", "Disagree", "Disagree", "Unsure", 
"Strongly_disagree", "Strongly_disagree", "Strongly_disagree", 
"Disagree", "Strongly_disagree", "Strongly_agree", "Disagree", 
"Disagree", "Neutral", "Agree", "Disagree", "Agree", "Disagree", 
"Neutral", "Disagree", "Disagree", "Strongly_disagree", "Strongly_disagree", 
"Disagree", "Agree", "Strongly_disagree", "Strongly_disagree", 
"Neutral", "Disagree", "Strongly_agree", "Strongly_disagree", 
"Neutral", "Agree", "Neutral", "Strongly_agree", "Neutral", "Strongly_disagree", 
"Agree", "Strongly_disagree", "Strongly_disagree", "Strongly_disagree", 
"Neutral"), POST_understand = c("Strongly_agree", "Strongly_agree", 
"Agree", "Agree", "Agree", "Agree", "Strongly_agree", "Strongly_disagree", 
"Strongly_agree", "Agree", "Strongly_agree", "Strongly_agree", 
"Unsure", "Strongly_disagree", "Neutral", "Strongly_agree", "Disagree", 
"Strongly_agree", "Strongly_agree", "Neutral", "Strongly_agree", 
"Strongly_agree", "Agree", "Neutral", "Agree", "Neutral", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Strongly_agree", "Neutral", 
"Strongly_agree", "Neutral", "Agree", "Neutral", "Strongly_agree", 
"Neutral", "Strongly_agree", "Agree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Neutral"), POST_serious_prob = c("Strongly_agree", 
"Strongly_agree", "Agree", "Agree", "Agree", "Agree", "Strongly_agree", 
"Strongly_disagree", "Unsure", "Agree", "Strongly_agree", "Strongly_agree", 
"Unsure", "Strongly_disagree", "Neutral", "Strongly_agree", "Disagree", 
"Strongly_agree", "Strongly_agree", "Neutral", "Strongly_agree", 
"Strongly_agree", "Agree", "Neutral", "Strongly_agree", "Neutral", 
"Strongly_agree", "Strongly_agree", "Strongly_agree", "Strongly_agree", 
"Neutral", "Strongly_agree", "Agree", "Strongly_agree", "Neutral", 
"Agree", "Neutral", "Strongly_agree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Neutral"), POST_confident = c("Strongly_agree", 
"Strongly_agree", "Agree", "Agree", "Agree", "Agree", "Strongly_agree", 
"Strongly_disagree", "Strongly_agree", "Agree", "Strongly_agree", 
"Strongly_agree", "Unsure", "Strongly_disagree", "Neutral", "Strongly_agree", 
"Disagree", "Strongly_agree", "Strongly_agree", "Disagree", "Strongly_agree", 
"Strongly_agree", "Agree", "Neutral", "Agree", "Neutral", "Strongly_agree", 
"Strongly_agree", "Agree", "Agree", "Strongly_agree", "Agree", 
"Agree", "Neutral", "Neutral", "Agree", "Neutral", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Strongly_agree", "Strongly_agree", 
"Neutral"), POST_willing = c("Strongly_agree", "Strongly_agree", 
"Agree", "Agree", "Neutral", "Agree", "Agree", "Strongly_disagree", 
"Strongly_agree", "Agree", "Strongly_agree", "Strongly_agree", 
"Unsure", "Strongly_disagree", "Disagree", "Strongly_agree", 
"Disagree", "Strongly_agree", "Strongly_agree", "Agree", "Strongly_agree", 
"Strongly_agree", "Agree", "Strongly_disagree", "Agree", "Neutral", 
"Strongly_agree", "Strongly_agree", "Agree", "Strongly_agree", 
"Strongly_agree", "Agree", "Agree", "Agree", "Neutral", "Agree", 
"Neutral", "Strongly_agree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Neutral"), POST_attain = c("Agree", 
"Strongly_agree", "Agree", "Agree", "Neutral", "Unsure", "Strongly_agree", 
"Strongly_disagree", "Strongly_agree", "Agree", "Strongly_agree", 
"Strongly_agree", "Agree", "Strongly_disagree", "Unsure", "Strongly_agree", 
"Disagree", "Strongly_agree", "Strongly_agree", "Agree", "Strongly_agree", 
"Strongly_agree", "Agree", "Neutral", "Strongly_agree", "Unsure", 
"Strongly_agree", "Strongly_agree", "Strongly_agree", "Strongly_agree", 
"Strongly_agree", "Strongly_agree", "Strongly_agree", "Unsure", 
"Neutral", "Agree", "Neutral", "Strongly_agree", "Neutral", "Strongly_agree", 
"Agree", "Agree", "Neutral")), row.names = 2:44, class = "data.frame")

我尝试了 barplot、ggplot,但没有任何结果。我已经把它们变成了 as.factors 但仍然无法得到带有误差线的数字。

r percentage survey factors stacked-bar-chart
1个回答
0
投票

鉴于上面的数据,这里是绘制第一列的基本代码:

library(ggplot2)
#responses
levels <- c("Strongly_disagree", "Disagree" , "Neutral", "Agree", "Strongly_agree")

#convert to factors in the proper order
data$envir_conscious <- factor(data$envir_conscious, levels=levels)

#plot
ggplot(data, aes(x=envir_conscious)) +
   geom_bar()
© www.soinside.com 2019 - 2024. All rights reserved.