Ignacio
Ignacio

Reputation: 7938

Plot one dot per each observation in a day

My data has 2 variables, a time-stamp and a group. Something like this:

df.plot <- structure(list(Timestamp = structure(c(1441814593, 1441818193, 
                                                  1441821398, 1441821449, 1441828375, 1441837436, 1441843661, 1441873127, 
                                                  1441885583, 1441966341, 1441985621, 1442048926, 1442321691, 1442329081, 
                                                  1442349761, 1442408140, 1442417679, 1442508871, 1442513339, 1442514010, 
                                                  1442525088, 1442562304, 1442564744, 1442569290, 1442569482, 1442571416, 
                                                  1442606687), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
                          group = c("A", "B", "B", "B", "B", "B", "B", "A", "B", "A", 
                                    "B", "A", "A", "A", "B", "B", "B", "B", "A", "B", "B", "A", 
                                    "A", "A", "A", "B", "A")), class = "data.frame", .Names = c("Timestamp", 
                                                                                                "group"), row.names = c(NA, -27L))

The time-stamp has second in it, but I would like to only use the date. That is, convert 2015-09-09 16:03:13 to 2015-09-09.

For each date, I would like to plot a dot. If I have to observations in the same date, I would like to plot a dot on top of each-other. On addition, I would like to use facets for the group variable.

I was able to do something similar to what I want:

ggplot(df.plot, aes(x=Timestamp)) + 
  geom_dotplot(method="histodot") + 
  facet_grid(group ~ .)

How can I tell ggplot to only use the date, and how can I change the scale of the y axis to display the count?

Upvotes: 1

Views: 484

Answers (2)

Ignacio
Ignacio

Reputation: 7938

ggplot(df.plot, aes(x=as.Date(Timestamp))) + 
  geom_dotplot(binwidth=1) +
  coord_fixed(ratio=1) + 
  ylim(0,7) +
  facet_grid(group ~ .) 

enter image description here

Upvotes: 2

SymbolixAU
SymbolixAU

Reputation: 26258

Create a date column from your Timestamp, and plot as per usual

df.plot$date <- as.Date(df.plot$Timestamp, format="%Y-%m-%d %H:%M:%s")

ggplot(data=df.plot, aes(x=date)) +
    geom_dotplot(method="histodot") + ylim(0,7) +
    facet_wrap(~group)

Of course, you don't actually have to create a new variable, you can do it all wihtin the ggplot call

ggplot(data=df.plot, aes(x=as.Date(Timestamp))) +
    geom_dotplot(method="histodot") + ylim(0,7) +
    facet_wrap(~group)

To get you your counts, you can modify the data to include a 'count' variable (using whatever method you prefer , dplyr, data.table, etc..)

here I'm using data.table

library(data.table)
setDT(df.plot)

df.plot[, date := as.Date(Timestamp)]
df.plot[, nCount := seq(1:.N), by=.(date, group)]

ggplot(data=df.plot, aes(x=date, y=nCount)) +
    geom_point() +
    facet_wrap(~group)

Upvotes: 3

Related Questions