绘制三组虚拟随时间的比例

时间:2018-03-10 15:51:08

标签: r plot ggplot2

我有一个日期框架,其中包含感兴趣的二元结果,var sql = "INSERT INTO resources (`resource_container_id`, `name`, `title`, `extension`, `mime_type`, `size`) VALUES ?"; y系列和date变量,如下例所示。

group

由此,我想在折线图 [/ EDIT] 中将{em> [EDIT] 绘制成{{1}的比例(在y轴上)随时间(在x轴上)按组。 (实际数据框包含每组超过千个观测值,因此该行将有意义,与此示例不同。;)

最好,我想使用R的内置绘图功能来执行此操作,但是如果需要也可以使用date <- c("2000-05-01", "2000-05-01", "2000-05-01", "2000-05-02", "2000-05-02", "2000-05-02", "2000-05-02", "2000-05-03", "2000-05-03", "2000-05-03", "2000-05-04", "2000-05-04") y <- c("1", "0", "0", "0","1","1","0", "1","1","0", "1","0") group <- c("1", "2", "3", "2", "1", "1", "2", "3", "2", "1", "1", "3") df <- as.data.frame(cbind(date, y, group))

其他类似的问题e.g. here已经通过对我来说不可行的解决方案(错误的情节)得到了回答,所以我有点迷失并希望得到帮助!

1 个答案:

答案 0 :(得分:2)

一种方法是预先计算比例并使用geom_line绘制它:

library(tidyverse)
df %>%
  mutate(date = as.POSIXct(date)) %>% #convert date to date
  group_by(group, date) %>% #group
  summarise(prop = sum(y=="1")/n()) %>% #calculate proportion 
  ggplot()+
  geom_line(aes(x = date, y = prop, color = group))+
  geom_point(aes(x = date, y = prop, color = group))

enter image description here

在评论中回答更新的问题:

df %>%
  mutate(date = as.POSIXct(date)) %>% #convert date to date
  group_by(group, date) %>% #group
  summarise(prop = sum(y=="1")/n()) %>%
  ggplot()+
  geom_line(aes(x = date, y = prop, color = group))+
  geom_point(aes(x = date, y = prop, color = group))+
  geom_vline(xintercept = as.POSIXct("2000-05-03 CEST"))

enter image description here