I have a dataset that looks like:
ID <- paste("S",seq(1, 120, 1), sep="")
Days <- round(rnorm(120, 100, 20), 0)
Sales <- round(rnorm(120, 16, 10), 2)
mult <-round(rnorm(120, 1.4, 0.4), 2)
mult[mult<1] <-1
Items_Sold <- round(Sales*mult, 2)
Items_Sold_Decile <-factor(ntile(Items_Sold, 10))
reprex_plotly <- data.frame(ID, Days, Sales, Items_Sold, Items_Sold_Decile)
And I want to plot it in using ggplot using plotly for toolips (to allow the IDs to be seen without crowding the graph with labels). I've never used plotly before and I have no knowledge of python and minimal knowledge of HTML, but I know ggplot2 relatively well.
My code is:
# make colour scale:
red_to_green <- colorRampPalette(c("#800000", "#e6194b", "#f58231", "#ffe119", "#bcf60c", "#3cb44b"))
reprex_plot <- ggplot(reprex_plotly, aes(x=Sales, y=Items_Sold, col=Items_Sold_Decile, size=Days)) +
#add three ablines to represent 1, 2 and 3 items per sale thresholds:
geom_abline(slope=1, intercept=0, col="red", linetype="dotted", size=0.7) +
geom_abline(slope=2, intercept=0, col="orange", linetype="dotted", size=0.7) +
geom_abline(slope=3, intercept=0, col="green", linetype="dotted", size=0.7) +
# now add salesperson points:
geom_point(aes(text=paste(ID, "<br>", "Items Per Sale = ", round(Items_Sold/Sales, 2))), alpha=0.7) +
# hline for items sold target (12.5 per day):
geom_hline(yintercept=12.5, linetype="dashed", size=0.5) +
# theme stuff:
theme_bw() +
labs(y="Average Items Sold", x="Average Sales", title = "Salesperson Performance") +
scale_color_manual(values=c(red_to_green(10))) +
theme(panel.grid.minor = element_line(colour="lightgrey", size=0.5)) +
theme(panel.grid.major = element_line(colour="lightgrey", size=0.5)) +
theme(axis.text.x = element_text(face="bold", size=10)) +
theme(axis.text.y = element_text(face="bold", size=12)) +
theme(axis.title.x = element_text(face="bold", size=16)) +
theme(axis.title.y = element_text(face="bold", size=16)) +
theme(legend.text= element_text(face="bold", size=12)) +
theme(legend.title= element_text(face="bold", size=16)) +
theme(plot.title= element_text(face="bold", size=16, hjust=0.5)) +
scale_y_continuous(limits=c(0,70), breaks= seq(0,100,10), minor_breaks=seq(0,100,5)) +
scale_x_continuous(limits=c(0,40), breaks= seq(0,100,2), minor_breaks=seq(0,100,1))
ggplotly(reprex_plot)
I can't seem to embed it here as it keeps crashing, but here is a screenshot of what it looks like (or you can create it yourself from the code):
This is great, but I would also like to add text tooltips on the ablines and hline when you hover over them that display e.g. "Target items sold per day = 12.5" or "Items per Sale = 2". Does anyone know how to add this to the plot?
Also, why does the legend title move away from the legend and get cut off the screen when called with ggplotly() compared to ggplot? How do I prevent it from doing this?
(If anyone can recommend some basic ggplotly tutorials geared at people who are familiar with ggplot but not plotly that would also be really helpful - all the ones I've found so far seem to be for experienced plotly users trying to learn ggplot!)