I';我试图在R中绘制比例,而不是ggplot2中的比例,但我不确定如何进行

I';我试图在R中绘制比例,而不是ggplot2中的比例,但我不确定如何进行,r,ggplot2,group-by,R,Ggplot2,Group By,因此,我希望看到一定比例的留级到下一年的学生在某一年的某一个月存款。这意味着我希望保留状态值为“1”的学生总数(在计数中找到)除以给定日期的计数总数(参见下面的dput)。这是我用来在ggplot中创建代码的代码,但我不确定如何编辑它来实现我的目的 Admit <- Admit %>% group_by(year, month, week, Retention_Status) %>% summarize(count = n()) ggplot(Admit,

因此,我希望看到一定比例的留级到下一年的学生在某一年的某一个月存款。这意味着我希望保留状态值为“1”的学生总数(在计数中找到)除以给定日期的计数总数(参见下面的dput)。这是我用来在ggplot中创建代码的代码,但我不确定如何编辑它来实现我的目的

Admit <- Admit %>%
  group_by(year, month, week, Retention_Status) %>% 
  summarize(count = n())


    ggplot(Admit, aes(1, week, fill = count)) + 
      geom_tile(colour = "white") + 
      facet_grid(year~month) + 
      scale_fill_gradient(low="red", high="green")

这就是你的想法吗

library(dplyr)
library(tidyr)
Admit %>%
  spread(key = Retention_Status, value = count, fill = 0) %>%
  mutate(total = `0` + `1`, proportion = `1`/total)
# # A tibble: 130 x 7
# # Groups:   year, month, week [130]
#    year  month     week   `0`   `1` total proportion
#    <chr> <fct>    <dbl> <dbl> <dbl> <dbl>      <dbl>
#  1 2012  April       5.    0.    1.    1.      1.00 
#  2 2012  May         1.    0.    1.    1.      1.00 
#  3 2012  November    4.    0.    1.    1.      1.00 
#  4 2012  December    2.    1.    3.    4.      0.750
#  5 2013  January     1.    0.    1.    1.      1.00 
#  6 2013  January     2.    2.    3.    5.      0.600
#  7 2013  January     3.    0.    2.    2.      1.00 
#  8 2013  January     4.    0.    1.    1.      1.00 
#  9 2013  January     5.    0.    2.    2.      1.00 
# 10 2013  February    1.    1.    3.    4.      0.750
库(dplyr)
图书馆(tidyr)
接纳%>%
排列(键=保留状态,值=计数,填充=0)%>%
变异(总数=`0`+`1`,比例=`1`/总数)
##A tibble:130 x 7
##分组:年、月、周[130]
#年-月-周'0``1'总比例
#                  
#1 2012年4月5日。01.1.1
#2012年5月1日。01.1.1
#3 2012年11月4日。01.1.1
#4 2012年12月2日。1.3.4.0.750
#5 2013年1月1日。01.1.1
#6 2013年1月2日。2.3.5.0.600
#7 2013年1月3日。02.2.1
#8 2013年1月4日。01.1.1
#9 2013年1月5日。02.2.1
#10 2013年2月1日。1.3.4.0.750

类似于
承认%>%分组依据(年、月、周)%%>%总结(比例=总和(保留状态==1)/总和(计数))%%>%ggplot(aes(周、月、填充=比例))+geom_光栅()+面网格(.~年)
?这并不完全正确。问题在于ggplot之前的代码。它返回一个比例列,该列只是给定星期计数的倒数列表。相反,当给定的一周和一个月的保留状态为“0”和“1”时,我希望对每一周和一个月的计数进行求和,并让比例读取保留状态为1时的计数除以这两周/月的计数之和。例如:如果在2015年9月第2周,保留状态为0时的计数为2,2015年9月第2周,保留状态的计数为1,我希望新的比例变量为“.3333333”。这表明,在2015年9月的第二周,本周申请人数的1/3(计数)被保留。录取测试%group_by(年、月、周)%>%总结(比例=总和(保留状态==1)/总和(计数))ggplot(录取测试,aes(1,周,填充=比例))+geom_光栅()!!
library(dplyr)
library(tidyr)
Admit %>%
  spread(key = Retention_Status, value = count, fill = 0) %>%
  mutate(total = `0` + `1`, proportion = `1`/total)
# # A tibble: 130 x 7
# # Groups:   year, month, week [130]
#    year  month     week   `0`   `1` total proportion
#    <chr> <fct>    <dbl> <dbl> <dbl> <dbl>      <dbl>
#  1 2012  April       5.    0.    1.    1.      1.00 
#  2 2012  May         1.    0.    1.    1.      1.00 
#  3 2012  November    4.    0.    1.    1.      1.00 
#  4 2012  December    2.    1.    3.    4.      0.750
#  5 2013  January     1.    0.    1.    1.      1.00 
#  6 2013  January     2.    2.    3.    5.      0.600
#  7 2013  January     3.    0.    2.    2.      1.00 
#  8 2013  January     4.    0.    1.    1.      1.00 
#  9 2013  January     5.    0.    2.    2.      1.00 
# 10 2013  February    1.    1.    3.    4.      0.750