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.