我正在尝试创建多个图表

时间:2017-12-03 22:58:43

标签: r ggplot2 google-analytics

使用此数据集我尝试使用ggplot按设备类别进行绘图,如下所示,我不断收到以下错误消息(geom_path:每个组只包含一个观察。你需要调整组审美吗?) 我尝试使用group = 1,但是没有用..任何帮助都很多?

head(gaDataExt)
      date deviceCategory users sessions pageviews
1 20171130        desktop     6        6        10
2 20171130         mobile    14       14        18
3 20171130         tablet     1        1         1
4 20171129        desktop    12       13        15
5 20171129         mobile    29       30        33
6 20171129         tablet     4        5         8

# multiple plots faceted by device categry.
ggplot(data = gaDataExt, mapping = aes(x = date, y = sessions),group = NULL ) + 
  geom_line() +
  facet_grid(deviceCategory ~ .) +
  theme_bw() +
  ylim(0,NA) 

1 个答案:

答案 0 :(得分:0)

df <- data.frame(date = c(rep(20171130, 3), rep(20171129, 3)),
                 deviceCategory = c('desktop', 'mobile', 'tablet'),
                 users = c(6, 14, 1, 12, 29, 4),
                 sessions = c(6, 14, 1, 13, 30, 5),
                 pageviews = c(10, 18, 1, 15, 33, 8))

您可以使用lubridate轻松更改为月份。这可以按月给出平均值。

library(tidyverse)
library(lubridate)

df %>% 
  mutate(date = ymd(date),
         month = month(date)) %>% 
  group_by(deviceCategory) %>% 
  summarise_all(funs(mean)) %>% 
  ggplot(aes(month, sessions)) + geom_point() + 
      facet_grid(deviceCategory~.) +
  theme_bw() 

更新,你可以简单地删除均值并使用日期:

df %>% 
  mutate(date = ymd(date)) %>% 
  ggplot(aes(date, sessions)) + geom_point() + 
      facet_grid(deviceCategory~.) +
  theme_bw()