Constructing a binary portion of a spark line in R with barplot ggplot2

I have data for the year that look like this:

datetime, key, value 1/1/15, 7k Steps, 1 1/1/15, Ate Poorly, 1 1/1/15, Audiobook, 1 1/1/15, Befriend, 1 1/1/15, Called Mom, 1 1/1/15, Code, 1 1/1/15, Create, 1 1/1/15, Critical, 1 1/1/15, Emailed Friend, 1 1/2/15, 10k Steps, 1 1/2/15, Ate Poorly, 1 1/2/15, Audiobook, 1 1/2/15, Befriend, 1 1/2/15, Called Mom, 1 1/2/15, Create, 1 1/2/15, Emailed Friend, 1 1/2/15, Exercise, 1 1/2/15, Friend Contact, 1 1/2/15, Great Day, 1 1/2/15, Write, 1 1/3/15, 7k Steps, 1 1/3/15, Ate Poorly, 1 1/3/15, Befriend, 1 1/3/15, Create, 1 1/3/15, Emailed Friend, 1 1/3/15, Friend Contact, 1 1/3/15, Great Day, 1 1/3/15, Happiness, 1 1/3/15, Health, 1 1/3/15, Videogame, 1 1/3/15, Walked With Michelle, 1 1/3/15, Write, 1 1/4/15, 7k Steps, 1 1/4/15, Ate Poorly, 1 1/4/15, Audiobook, 1 1/4/15, Great Day, 1 1/4/15, Happiness, 1 1/4/15, Health, 1 1/4/15, Impatient, 1 1/4/15, Love, 1 1/4/15, Movie With Michelle, 1 

I want to create a chart that displays one row for each key with bars for each day, which has 1 for this key. Here is an example of the desired result:

enter image description here

What I painfully did with Python and Matplotlib.

I am looking for the best and easiest way to make such a graph in R with maybe ggplot2. I planned to use a bar graph in ggplot2 with a loop for each key. Here is an example of my code:

 library(ggplot2) library(reshape) #library(ggtheme) # 2015 Lifedata Processing d <- read.csv("lifedata_2015.csv") d$datetime <- as.Date(d$datetime, "%m/%d/%Y") # Create a new dataframe with a subset of keys r <- d[d$key %in% c("Read", "Audiobook"), ] # Put 1s in all values. r$value <- 1 # Generate a data frame for each day with a value of 1 and a key of "alldates" mydates <- data.frame("datetime" = seq(as.Date("2015/1/1"), as.Date("2015/12/31"), "days"), "key" = "alldates", "value" = 1) # combine two data frames, one after the other n <- rbind(r, mydates) # Transform into a wide data frame based on datetime and key with mean as the value. c <- cast(n, datetime~key, mean) # Turn NaNs into 0 c[is.na(c)] = 0 for(name in c("Read", "Audiobook")){ plt <- c(plt, ggplot(data=c, aes_string(x="datetime", y=name)) + geom_bar(stat="Identity", width=1)) print(plot) } svg("~/Desktop/tagplot.svg") grid.arrange(plt, ncol = 1, main = "Read") dev.off() 

This method does not seem to work.

What is the best way to build event data similar to the example above?

+8
r ggplot2
source share
2 answers

Here is an alternative approach, borrowed heavily from @TylerRinker answer. As far as I can tell, his answer shows only if this activity was carried out two days in a row.

Customization

 library(dplyr) library(ggplot2) 

First, we take these things from Tyler. We need shortcuts.

 d <- d %>% mutate(datetime = as.Date(datetime, "%m/%d/%y")) key <- d %>% group_by(key) %>% summarize(n = length(datetime), perc = n/length(unique(d$datetime))) %>% arrange(perc) %>% mutate( new = paste0(key, " - ", n, "(", 100*perc, "%)"), new = factor(new, levels = new) ) 

Instead of geom_line we use geom_tile to get a filled rectangle for each day with a value of 1, missing days remain empty. We use geom_hline to create some separation in the y direction.

Graphic code

 left_join(d, key) %>% ggplot(aes(datetime, y = new)) + geom_tile(show.legend = FALSE, fill = 'grey50') + geom_hline(yintercept = seq(0.5, length(levels(d$key))), color = 'white', size = 2) + theme_classic() + scale_x_date(date_breaks = "1 month", date_labels = "%b", expand = c(0, 0)) + ylab(NULL) + xlab(NULL) 

Result

enter image description here

+6
source share

Here you will need to develop a decent start, but some of the smaller details:

 library(ggplot2) library(tidyr) library(dplyr) d <- d %>% mutate(datetime = as.Date(datetime, "%m/%d/%y")) key <- d %>% group_by(key) %>% summarize( n = length(datetime), perc = n/length(unique(d$datetime)) ) %>% arrange(perc) %>% mutate( new = paste0(key, " - ", n, "(", 100*perc, "%)"), new = factor(new, levels = new) ) left_join(d, key) %>% ggplot(aes(datetime, y = new)) + geom_line(size = 6, alpha=.3) + theme_minimal() + scale_x_date(date_breaks = "1 month", date_labels = "%b", expand = c(0, 0)) + ylab(NULL) + xlab(NULL) 

enter image description here

+4
source share

All Articles