DVCar
DVCar

Reputation: 11

How to plot data that is separated into multiple categories using ggplot2?

A market survey on consumer satisfaction when using two cleaning products (Product A and Product B) was separated according to room (Kitchen, Bathroom, Bedroom), frequency of use (Daily, Weekly, Monthly) and product bottle size (Small, Medium, Large).

I thought the best way to represent these results is column plots. However, I think there are better ways to represent these results, so I come here to ask for suggestions for a better graphical representation, as I am not very satisfied with column plots.

In MWE the data is arranged ascending, but with real data, the different column sizes make the plot visually very polluted.

library(ggplot2)

Product <- c("Product A", "Product B")
Place <- c("Kitchen", "Bathroom", "Bedroom")
Bottle_size <- c("Small", "Medium", "Large")
Frequency <- c("Daily", "Weekly", "Monthly")

DF <- expand.grid(Bottle_size = Bottle_size,
                  Place = Place,
                  Frequency = Frequency,
                  Product = Product)

DF$Consumer_Approval <- seq(from = 10,
                            to = 100,
                            by = (100-10)/53)

ggplot(data = DF,
       aes(x = Frequency,
           y = Consumer_Approval)) +
  geom_col(aes(fill = Bottle_size),
           position = "dodge") +
  facet_grid(Product ~ Place)

enter image description here

The real data is something like: enter image description here

Upvotes: 1

Views: 943

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173803

One option would be a lollipop chart, where the height of the lollipop shows the difference between the consumer rating for product A and product B. This allows a quick visual estimate of the preferred product in a given setting:

ProdA <- split(DF, Product)[[1]]
ProdB <- split(DF, Product)[[2]]
DF <- dplyr::left_join(ProdA, ProdB, c("Bottle_size", "Place", "Frequency"))
DF$AvsB <- DF$Consumer_Approval.x - DF$Consumer_Approval.y

ggplot(data = DF, aes(x = Frequency, y = AvsB, color = Bottle_size)) +
  geom_point(position = position_dodge(width = 0.5)) +
  geom_linerange(aes(ymin = AvsB, ymax = 0), position = position_dodge(width = 0.5)) +
  geom_hline(yintercept = 0, linetype = 2) +
  ylim(-100, 100) +
  facet_grid(.~Place) +
  labs(y = "Prefers Product B <- No preference  -> Prefers Product A")

enter image description here

Upvotes: 1

Related Questions