Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R分组条形图_R_Ggplot2 - Fatal编程技术网

R分组条形图

R分组条形图,r,ggplot2,R,Ggplot2,我希望有人能把这个noob指向正确的方向,尝试创建一些分组的条形图 我的数据(调查回答1=是)如下所示 fac_blister8 fac_lackaccess8 fac_remote8 fac_timely8 fac_none8 <dbl> <dbl> <dbl> <dbl> <dbl> 1 0 0

我希望有人能把这个noob指向正确的方向,尝试创建一些分组的条形图

我的数据(调查回答1=是)如下所示

fac_blister8 fac_lackaccess8 fac_remote8 fac_timely8 fac_none8
          <dbl>           <dbl>       <dbl>       <dbl>     <dbl>
 1            0               0           0           0         1
 2            0               0           0           0         1
 3            0               0           0           0         1
 4            0               0           0           1         0
 5            0               1           1           1         0
 6            0               0           0           0         1
 7            0               1           0           1         0
 8            0               0           0           0         1
q8 <-
  q8vars                        %>% 
  group_by(fac_lackaccess8)           %>% 
  summarise(name = n())      %>%
  mutate(fac_lackaccess8 = ifelse(fac_lackaccess8 == 0,"No","Yes")) 

q8_2 <-ggplot(q8, aes(x=fac_lackaccess8, y=name)) +
  geom_bar(stat="identity", width = 0.5, color="green", fill="green") + 
  scale_y_continuous(limits = c(0,22)) 

q8_2 +
  ylab("Count") + xlab("Working remotely and lack of resources")
  
fac_bull 8 fac_lackaccess 8 fac_remote 8 fac_timely 8 fac_none 8
1            0               0           0           0         1
2            0               0           0           0         1
3            0               0           0           0         1
4            0               0           0           1         0
5            0               1           1           1         0
6            0               0           0           0         1
7            0               1           0           1         0
8            0               0           0           0         1
我设法为每一列创建了一个这样的绘图

fac_blister8 fac_lackaccess8 fac_remote8 fac_timely8 fac_none8
          <dbl>           <dbl>       <dbl>       <dbl>     <dbl>
 1            0               0           0           0         1
 2            0               0           0           0         1
 3            0               0           0           0         1
 4            0               0           0           1         0
 5            0               1           1           1         0
 6            0               0           0           0         1
 7            0               1           0           1         0
 8            0               0           0           0         1
q8 <-
  q8vars                        %>% 
  group_by(fac_lackaccess8)           %>% 
  summarise(name = n())      %>%
  mutate(fac_lackaccess8 = ifelse(fac_lackaccess8 == 0,"No","Yes")) 

q8_2 <-ggplot(q8, aes(x=fac_lackaccess8, y=name)) +
  geom_bar(stat="identity", width = 0.5, color="green", fill="green") + 
  scale_y_continuous(limits = c(0,22)) 

q8_2 +
  ylab("Count") + xlab("Working remotely and lack of resources")
  
q8%
分组依据(fac_lackaccess8)%>%
摘要(name=n())%>%
突变(fac_lackaccess8=ifelse(fac_lackaccess8==0,“否”,“是”))

问题8_2将数据重新格式化为长格式,计算每列中出现的是/否,并用
facet_wrap
绘制数据:

library(tidyverse)

q8vars %>%
  pivot_longer(cols = everything()) %>%
  count(name, value) %>%
  mutate(value = ifelse(value == 0, 'no', 'yes')) %>%
  ggplot(aes(x=value, y=n)) +
  geom_bar(stat="identity", width = 0.5, color="green", fill="green") + 
  facet_wrap(~name) + 
  ylab("Count") + xlab("Working remotely and lack of resources")

哦,非常感谢!我一直在为此奋斗。你是个明星!