I'm trying to recreate this kind of plot in R but I'm not very successful. Where X = date and Y = frequency of a discrete variable, cumulative on one bar. Also I'm trying to put it in a function so it would be easier to use this kind of plot for different variables.
I'd appreciate any help!
Data example: Excel plot example <---
Purchase_date Phone
2014-10-23 Sony
2014-10-23 Apple
2014-10-23 Nokia
2014-10-23 Nokia
2014-10-24 NA
2014-10-24 Nokia
2014-10-24 Sony
2014-10-24 Other
2014-10-24 Apple
2014-10-25 Sony
2014-10-25 NA
2014-10-25 Apple
2014-10-25 Sony
2014-10-25 Nokia
Also I have something like this but it's definitely far from universal method for different variables:
base_table %>%
filter(year(as.Date(BUY_DATE)) >= 2014, year(as.Date(BUY_DATE)) <= 2017) %>%
mutate(BUY_DATE = as.yearmon(as.Date(BUY_DATE))) %>%
group_by(PHONETYPE, BUY_DATE) %>% summarise(n = n()) -> applPerTypeAndMonth
applPerTypeAndMonth %>% pull(PHONETYPE) %>% table()
filter(applPerTypeAndMonth, PHONETYPE == '') -> x
xts(x$n, order.by = x$BUY_DATE) -> type1
filter(applPerTypeAndMonth, PHONETYPE == 'NOKIA') -> x
xts(x$n, order.by = x$BUY_DATE) -> type2
filter(applPerTypeAndMonth, PHONETYPE == 'APPLE') -> x
xts(x$n, order.by = x$BUY_DATE) -> type3
filter(applPerTypeAndMonth, PHONETYPE == 'SONY') -> x
xts(x$n, order.by = x$BUY_DATE) -> type4
filter(applPerTypeAndMonth, PHONETYPE == 'HUAWEI') -> x
xts(x$n, order.by = x$BUY_DATE) -> type5
filter(applPerTypeAndMonth, PHONETYPE == 'LG') -> x
xts(x$n, order.by = x$BUY_DATE) -> type6
filter(applPerTypeAndMonth, PHONETYPE == 'OTHER') -> x
xts(x$n, order.by = x$BUY_DATE) -> type7
merge(type1,type2,type3,type4,type5,type6,type7) -> types
na.fill(types, fill = 0.0) -> types
barplot(types, col = rainbow(7))
types %>% apply(1, function(x) x / sum(x)) %>% barplot(col = rainbow(7))
# legend("topright", legend = names(types), fill = rainbow(7))