Mactilda
Mactilda

Reputation: 393

Can't add geom_point to line plot with stat_count

I have a df where I have made a nice line plot using stat_count, but when I try to add geom_point it won't work.

Without the last part (geom_point(size=2)) it produces a line plot, but with it I get error:

Don't know how to automatically pick scale for object of type function. Defaulting to continuous. Error: Column y must be a 1d atomic vector or a list

df <- data.frame("id" = c(1, 1, 1, 2, 2, 3, 3, 3, 4, 4), 
                 "bowl" = c("red", "red", "red","green", "green", "green",  
                            "green", "green", "red", "red"),
                 "year"=c(2001:2003, 2002:2003, 2001:2003, 2001:2002))

library(dplyr)
library(ggplot2)
df %>% 
  ggplot(aes(x=year, y=count, colour=bowl)) +
  stat_count(geom = "line", 
             aes(y=..count..))+
  geom_point(size=2) 

I suspect there's just a small adjustment to be made, but I can't seem to find it on my own.

Upvotes: 1

Views: 1560

Answers (1)

Uwe
Uwe

Reputation: 42544

There are two possible approaches:

  1. Using stat_count() and specifying geom
  2. Using geom_line() and geom_point(), resp., and specifying stat

There is a difference in the default value for position which will create different plots.

1. Stacked plot of counts (total counts)

As already mentioned by Z.Lin,

library(ggplot2)
ggplot(df, aes(x = year, y = stat(count), colour = bowl)) + 
  stat_count(geom = "line") + 
  stat_count(geom = "point")

will create a stacked line and point plot of counts, i.e., the total number of records per year (regardless of bowl):

enter image description here

As of version 3.0.0 of gplot2 it is possible to use the new stat() function for calculated-aesthetic variables. So, stat(count) replaces ..count...

The same plot is created by

ggplot(df, aes(x = year, y = stat(count), colour = bowl)) + 
  geom_line(stat = "count", position = "stack") + 
  geom_point(stat = "count", position = "stack")

but we have to specify explicitely that the counts have to be stacked.

2. Line and point plot of counts by colour

If we want to show the counts per year for each value of bowl separately, we can use

ggplot(df, aes(x = year, y = stat(count), colour = bowl)) + 
  geom_line(stat = "count") + 
  geom_point(stat = "count")

which produces a line and point plot for each colour.

enter image description here

This can also be achieved by

ggplot(df, aes(x = year, y = stat(count), colour = bowl)) + 
  stat_count(geom = "line", position = "identity") + 
  stat_count(geom = "point", position = "identity")

but know we have to specify explicitely not to stack.

Upvotes: 3

Related Questions