I'm looking to take a data set that for example where, company1 has "lenovo" products and "dell" products. I would like to break out each part of this data set to show "these are the lenovo customers (were lenovo == 1), which of these also have dell products, or also have samsung products, or maybe only have lenovo products. I'd like this to be shown in a stacked bar chart if possible, or facet grid. Ideal chart would show each product as a singular bar, where the bar is colored by the count of the other products that those customer contain.
For example if there are 100 lenovo customers, and 20 of them also have dell, and 25 also have apple, and 10 of them have samsung, (noting that some of these could overlap, there could be a customer with samsung and dell and that would be counted in the 20 for dell and the 10 for samsung), that the bar would show 20 colored for dell, 25 colored for apple, 10 colored for samsung, and the remainder colored for just lenovo with no additional products. -- then that would be replicated for dell, and which of the dell customers had products with the other groups.. etc.
reproducible data:
a <- paste0("abcd", c(1:185))
dell <- sample(c(1, 0, 0), size = 185, replace = TRUE)
apple <- sample(c(1, 0, 0, 0, 0), size =185, replace = TRUE)
lenovo <- sample(c(1, 0), size = 185, replace = TRUE)
samsung <- sample(c(1, 0), size = 185, replace = TRUE)
df <- data.frame(a, dell, apple, lenovo, samsung)
I've dug into trying to do something like:
ggplot(df, aes(x = Dell)) +
geom_histogram(stat = 'count', position = "dodge") +
geom_text(stat = 'count', aes(label = ..count..), position = position_dodge(width = .9), vjust = -1) +
scale_y_continuous(labels = comma)
The other way I was trying to do this was where the data was line by line and if there was a customer with dell and apple and samsung, they would be represented by 3 lines. That way I could facet_grid by the product. The problem is that I have a hard time showing that this one line of customer abcd1 that has apple, also has samsung and dell, and visualizing that.
Any help is appreciated!
