DN1
DN1

Reputation: 218

How to plot across time with multiple groups in r?

I've got a dataset with 2 columns:

Group   Date
Group1  29/12/2008  
Group1  29/12/2008  
Group2  29/12/2008
Group3  10/05/2009
Group1  10/05/2009
Group1  10/05/2009

I am looking to create a bubble plot where date (by the years in general) is the x-axis and the y-axis is a count of high many times each of the groups appears in that year.

I have no idea how to do this so I've been starting with ggplot:

ggplot(df, aes(x=Date, y=Group, size = Group)) +
  geom_point(alpha=0.7)

This runs but gives me y as the Group (as I know I have set) but I don't know what to set y to so it just is count per group that can be plotted for each year. As well as the x-axis being all the specific dates when I just want it to be the general year. I'm new to plotting, how I can I adjust the example to give y as a count of the times the groups in Group appear and how I can I adjust the x-axis to be years an not specific dates?

Upvotes: 1

Views: 379

Answers (1)

Duck
Duck

Reputation: 39585

Maybe try this aggregating your data:

library(tidyverse)
#Code
df %>% mutate(Date=as.Date(Date,'%d/%m/%Y'),
              Year=format(Date,'%Y')) %>%
  group_by(Year,Group) %>%
  summarise(N=n()) %>%
  ggplot(aes(x=factor(Year), y=Group, size = N,color=Group)) +
  geom_point(alpha=0.7)

Output:

enter image description here

Some data used:

#Data
df <- structure(list(Group = c("Group1", "Group1", "Group2", "Group3", 
"Group1", "Group1"), Date = c("29/12/2008", "29/12/2008", "29/12/2008", 
"10/05/2009", "10/05/2009", "10/05/2009")), class = "data.frame", row.names = c(NA, 
-6L))

Upvotes: 2

Related Questions