I can't seem to get the geom_smooth data to work.
Example data:
## A tibble: 12 x 4
UID Month n tot
<dbl> <chr> <int> <dbl>
1 1001 2016-04 2 75
2 1001 2016-05 7 500
3 1001 2016-06 3 1673
4 1001 2016-07 5 288
5 1001 2016-08 2 123
6 1001 2016-09 3 739
7 1001 2016-10 4 241
8 1001 2016-12 2 512
9 1001 2017-01 5 350
10 1001 2017-02 1 48
11 1001 2017-03 2 125
12 1001 2017-04 2 NA
Plotting code:
ggplot(one, aes(Month, tot)) + geom_point() + geom_smooth()
Do you think it has something to do with the Character value in the Date field?
Sometimes with lines and smoothers you have to specify which points should actually be joined with the line, so you can add group = 1
to ensure everything is treated as part of the same group:
ggplot(one, aes(Month, tot)) +
geom_point() +
geom_smooth(aes(group=1))
Yes, it is the format of the Month
column which prevents the smooth. The question is: how to convert that column to a date? Given that you have only year and month, but date requires a day.
Two options. You could use as.yearmon
from the zoo package to convert to a yearmon
object:
library(dplyr)
one %>%
mutate(date = zoo::as.yearmon(Month)) %>%
ggplot(aes(date, tot)) + geom_point() + geom_smooth()
Or you could assume that the date is the first day of every month, for conversion to a date:
library(dplyr)
one %>%
mutate(date = as.Date(paste(Month, "01", sep = "-"))) %>%
ggplot(aes(date, tot)) + geom_point() + geom_smooth()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.