r - Using `geom_line()` with X axis being factors -
suppose have dataframe:
hist <- data.frame(date=sys.date() + 0:13, counts=1:14)
i want plot total count against weekday, using line connect points. following puts points on each value:
hist <- transform(hist, weekday=factor(weekdays(date), levels=c('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'))) ggplot(hist, aes(x=weekday, y=counts)) + geom_point(stat='summary', fun.y=sum)
when try connect them line (geom_line()
), ggplot complains having 1 data observation per group , hence not able draw line between points.
i understand - it's trying draw 1 line each weekday (factor level).
how can ggplot pretend (for purposes of line only) weekdays numeric? perhaps have have column day_of_week
0 monday, 1 tuesday, etc?
if understand issue correctly, specifying group=1
, adding stat_summary()
layer should trick:
ggplot(hist, aes(x=weekday, y=counts, group=1)) + geom_point(stat='summary', fun.y=sum) + stat_summary(fun.y=sum, geom="line")
Comments
Post a Comment