r - Multiple time series with ggplot2 -


i need make plots work , i've been learning use ggplot2, can't quite figure out how work dataset i'm using. can't post actual data here, can give brief example of like. have 2 main dataframes; 1 contains quarterly total revenue variety of companies , other contains quarterly revenue various segments within each company. example:

quarter, compa, compb, compc... 2011.0, 1, 2, 3... 2011.25, 2, 3, 4... 2011.5, 3, 4, 5... 2011.75, 4, 5, 6... 2012.0, 5, 6, 7... 

and

quarter, compa_footwear, compa_apparel, compb_wholesale... 2011.0, 1, 2, 3... 2011.25, 2, 3, 4... 2011.5, 3, 4, 5... 2011.75, 4, 5, 6... 2012.0, 5, 6, 7... 

the script i've been building loops through each company in first table , uses select() grab of columns in second table, purposes of question, forget other companies , assume first table compa , second table of different compa segments.

what i'm trying each segment, create line plot has both total company revenue , segment revenue charted on time. this like. ideally, i'd able use facet_wrap() or able make different graphs each segment @ once, that's not absolutely necessary. clarify, each individual graph should have 2 lines: overall company , 1 specific segment.

i'm fine having restructure data in way necessary. know how can work?

i think below should work. note need move data around fair bit.

# load packages library(dplyr) library(ggplot2) library(reshape2) library(tidyr) 

make reproducible data set:

# create companies # pull column names in data companies <- paste0("comp",letters[1:4])  set.seed(12345)  sepdata <-   lapply(companies, function(thiscomp){     ndiv <- sample(3:6,1)     temp <-        sapply(1:ndiv,function(idx){         round(rnorm(24, rnorm(1,100,25), 6))       }) %>%       as.data.frame() %>%       setnames(paste(thiscomp,sample(letters,ndiv), sep = "_"))   }) %>%   bind_cols()  sepdata$quarter <-   rep(2010:2015       , each = 4) +   (0:3)/4  meltedsep <-   melt(sepdata, id.vars = "quarter"        , value.name = "revenue") %>%   separate(variable            , c("company","division")            , sep = "_") %>%   mutate(division = factor(division                            , levels = c(sort(unique(division))                                         , "total")))  fullcompany <-   meltedsep %>%   group_by(company, quarter) %>%   summarise(revenue = sum(revenue)) %>%   mutate(division = factor("total"                            , levels = levels(meltedsep$division))) 

the plot want here. note need set divison = null prevent total showing in own facet:

theme_set(theme_minimal())  catch <- lapply(companies, function(thiscompany){   tempplot <-     meltedsep %>%     filter(company == thiscompany) %>%     ggplot(aes(y = revenue                , x = quarter)) +     geom_line(aes(col = "division")) +     facet_wrap(~division) +     geom_line(aes(col = "total")               , fullcompany %>%                 filter(company == thiscompany) %>%                 mutate(division = null)               ) +     ggtitle(thiscompany) +     scale_color_manual(values = c(division = "darkblue"                                   , total = "green3"))   print(tempplot) }) 

example of output:

enter image description here

note, however, that looks sort of terrible. difference between "total" , 1 division going huge. instead, may want plot divisions on 1 plot:

alldata <-   bind_rows(meltedsep, fullcompany)  catch <- lapply(companies, function(thiscompany){   tempplot <-     alldata %>%     filter(company == thiscompany) %>%     ggplot(aes(y = revenue                , x = quarter                , col = division)) +     geom_line() +     ggtitle(thiscompany)     # add manual colors here, assigned that, e.g. "clothes" same   print(tempplot) }) 

example:

enter image description here

the difference between total , each still large, @ least can compare divisions.

if mine make though, make 2 plots. 1 each division each company (faceted) , 1 totals:

meltedsep %>%   ggplot(aes(y = revenue              , x = quarter              , col = division)) +   geom_line() +   facet_wrap(~company) 

enter image description here

fullcompany %>%   ggplot(aes(y = revenue              , x = quarter              , col = company)) +   geom_line() 

enter image description here


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -

c++ - Migration from QScriptEngine to QJSEngine -