2017-06-03 118 views
1

我有一个ggplot正在做我想要的,直到我添加一个覆盖。复制的数据是低于,这里的ggplot:ggplot覆盖是覆盖我的x轴时间线,如何防止?

timeline <- ggplot(dataset, aes(x = Month, y = Sessions,fill = Channel, group = Channel)) + 
    geom_area(alpha = 0.3) + 
    stat_summary(aes(group = 2), fun.y = sum, geom = 'line', size = 2, alpha = 0.5) + 
    theme(axis.text.x=element_text(angle=90, hjust=1), 
     axis.title.x = element_blank()) 

结果:enter image description here

到目前为止好。请注意x轴按顺序显示月份。然后,我添加一个覆盖(请参阅下面的示例数据生成的代码中的变量):

# make overlay representing TV 
tv_begin <- sample(mts, 4) 
tv_end <- tv_begin %m+% months(1) 
tv_overlay <- data.frame(start = format(tv_begin, "%b-%Y"), end = format(tv_end, "%b-%Y")) 

然后:

timeline + geom_rect(data = tv_overlay, inherit.aes = FALSE, 
      aes(xmin = start, xmax = end, 
       ymin = -Inf, ymax = Inf, 
       alpha = "TV On"), 
      fill = "black") 

结果:enter image description here

现在我的x轴个月未排序我不知道如何防止这种情况发生?我如何维护x轴或如何从它们继承新的叠加层,以便添加叠加层不会更改x轴?

----这里的数据复制变量 “数据集” -----

## Build dummy data 
library(dplyr) 

# dimensions 
channels <- c("Facebook", "Youtube", "SEM", "Organic", "Direct", "Email") 
last_month <- Sys.Date() %m+% months(-1) %>% floor_date("month") 
mts <- seq(from = last_month %m+% months(-23), to = last_month, by = "1 month") 
yr_month <- format(mts, "%b-%Y") 
dimvars <- expand.grid(Month = yr_month, Channel = channels) 

# metrics 
rws <- nrow(dimvars) 
set.seed(42) 

# generates variablility in the random data 
randwalk <- function(initial_val, ...){ 
    initial_val + cumsum(rnorm(...)) 
} 

Sessions <- ceiling(randwalk(3000, n = rws, mean = 8, sd = 1500)) %>% abs() 
Transactions <- ceiling(randwalk(200, n = rws, mean = 0, sd = 75)) %>% abs() 
Revenue <- ceiling(randwalk(10000, n = rws, mean = 0, sd = 3500)) %>% abs() 
Spend <- ceiling(randwalk(6000, n = rws, mean = 0, sd = 3500)) %>% abs() 

# make primary df 
dataset <- cbind(dimvars, Sessions, Transactions, Revenue, Spend) %>% 
    mutate(Spend = ifelse(Channel %in% c("Direct", "Organic"), NA, Spend)) 
+0

远离的时刻的计算机,但仍需使用stringsAsFactors = FALSE在你的调用data.frame为tv_overlay – bouncyball

+0

感谢@bouncyball的提示。我将在哪里准确地添加?我尝试在这里添加它:''geom_rect(data = tv_overlay,inherit.aes = FALSE,stringsAsFactors = FALSE'',但收到警告“Warning:Ignoring unknown parameters:stringsAsFactors”and the chart did not change –

+1

tv_overlay < - data .frame(start = format(tv_begin,“%b-%Y”),end = format(tv_end,“%b-%Y”),stringsAsFactors = FALSE) – bouncyball

回答

1

从您的代码中调用format()。它将一切变成弦乐/因素。

在这里,我已经转换x轴数据与as.Date(),并格式化x轴在图中与scale_x_date()

library(tidyverse) 
library(lubridate) 
# dimensions 
channels <- c("Facebook", "Youtube", "SEM", "Organic", "Direct", "Email") 
last_month <- Sys.Date() %m+% months(-1) %>% floor_date("month") %>% as.Date() 
mts <- seq(from = last_month %m+% months(-23), to = last_month, by = "1 month") %>% as.Date() 
#yr_month <- format(mts, "%b-%Y") 
yr_month <- mts # format(mts, "%b-%Y") 
dimvars <- expand.grid(Month = yr_month, Channel = channels, stringsAsFactors = FALSE) 

rws <- nrow(dimvars) 
set.seed(42) 

# generates variablility in the random data 
randwalk <- function(initial_val, ...){ 
     initial_val + cumsum(rnorm(...)) 
} 

Sessions <- ceiling(randwalk(3000, n = rws, mean = 8, sd = 1500)) %>% abs() 
Transactions <- ceiling(randwalk(200, n = rws, mean = 0, sd = 75)) %>% abs() 
Revenue <- ceiling(randwalk(10000, n = rws, mean = 0, sd = 3500)) %>% abs() 
Spend <- ceiling(randwalk(6000, n = rws, mean = 0, sd = 3500)) %>% abs() 

# make primary df 
dataset <- cbind(dimvars, Sessions, Transactions, Revenue, Spend) %>% 
     mutate(Spend = ifelse(Channel %in% c("Direct", "Organic"), NA, Spend)) 

glimpse(dataset) 

# make overlay representing TV 
tv_begin <- sample(mts, 4) 
tv_end <- tv_begin %m+% months(1) 
tv_overlay <- data.frame(start = tv_begin, end = tv_end) 
glimpse(tv_overlay) 
timeline <- ggplot(dataset, aes(x = Month, y = Sessions,fill = Channel, group = Channel)) + 
     geom_area(alpha = 0.3) + 
     stat_summary(aes(group = 2), fun.y = sum, geom = 'line', size = 2, alpha = 0.5) + 
     theme(axis.text.x=element_text(angle=90, hjust=1), 
       axis.title.x = element_blank()) + 
     scale_x_date(date_labels = "%b-%d", date_breaks = "1 month") 

timeline + geom_rect(data = tv_overlay, inherit.aes = FALSE, 
           aes(xmin = start, xmax = end, 
            ymin = -Inf, ymax = Inf, 
            alpha = "TV On"), 
           fill = "black") 
+0

这样做,非常感谢! –