0

I am new to R and was fascinated by ggplot2 in combination of plotly. In the below code I am trying to do a box plot jitter with label of WAMP from dataset. I seem to get most of it except the fact that i am unable to sort the x axis by Size and show 2 x axis values, something like this question: Multi-row x-axis labels in ggplot line chart

Here is my R code:

library(ggplot2)
library(plotly)

df <- data.frame(Products = rep(c('Product1','Product2','Product3'), times = 100),
                 Size = rep(c(1000,3000,2000), times = 100),
                 sales = runif(100, min=0, max = 300),
                 WAMP = rep(c(1.5,2.5,3.5), times = 100),
                 PricePerUnit = runif(100, min = 1, max = 5))

p <- ggplot() +
  geom_boxplot(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, group = 1), outlier.colour = NULL) + 
  geom_jitter(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, color = sales)) +
  scale_color_gradientn(colours = c("red", "yellow", "green", "lightblue", "darkblue")) +
  scale_y_continuous(breaks = seq(0, 10, by = .5)) +
  geom_line(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), y = WAMP)) + 
  geom_text(data = df, aes(label = round(WAMP,2), x = Products, y = WAMP), size = 3) +
  ggtitle("Market by Price Distribution and Sales Volume")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
p <- ggplotly(p)
p

It produces this image and as you can see its putting the WAMP chart next to the boxplot jitter (not combined). Any help is greatly appreciated here. Thanks.

Boxplot Jitter

Kim
  • 4,080
  • 2
  • 30
  • 51
Manny
  • 63
  • 3
  • 9
  • Thanks for the comment. I have amended the code to reproduce the issue. Running this code i get 2 charts next to each other. What i want is WAMP on top of the boxplot/scatterplot. Also the labels have merged with Size and Product - How can i format them like the one in the link from my question? – Manny Jan 29 '19 at 22:47

1 Answers1

0

I'm not entirely sure I get it, but perhaps this is what you wanted?

p <- ggplot() +
    geom_boxplot(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, group = 1), outlier.colour = NULL) + 
    geom_jitter(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, color = sales)) +
    scale_color_gradientn(colours = c("red", "yellow", "green", "lightblue", "darkblue")) +
    scale_y_continuous(breaks = seq(0, 10, by = .5)) +
    geom_line(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), y = WAMP)) + 
    geom_text(data = df, aes(label = round(WAMP,2), x = interaction(df$Size, df$Products, lex.order = TRUE), y = WAMP), size = 3) +
    ggtitle("Market by Price Distribution and Sales Volume")+
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
p <- ggplotly(p)
p

It was just a matter of designating the geom_text to the right x.

Kim
  • 4,080
  • 2
  • 30
  • 51
  • Kim it seems like you are missing a bracket as it gives me an error: Error: unexpected symbol in: "theme(axis.text.x = element_text(angle = 45, hjust = 1)) – Manny Jan 29 '19 at 23:20