Curious how to plot this dotplot using ggplot or plotly library functions. Also label the mpg values on individual dots.
# Dotplot: Grouped Sorted and Colored
# Sort by mpg, group and color by cylinder
x <- mtcars[order(mtcars$mpg),] # sort by mpg
x$cyl <- factor(x$cyl) # it must be a factor
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"
dotchart(x$mpg,labels=row.names(x),cex=.7,groups= x$cyl,
main="Gas Milage for Car Models\ngrouped by cylinder",
xlab="Miles Per Gallon", gcolor="black", color=x$color)
With a quick clean up of the rownames to be a column you can do the following.
We used
factor()
for the aesthetics for color so that it becomes discrete/ When faceting to acheive this look you need to specify"free_y"
forscale
andspace
.Base
Adding Text
You can probably use
geom_label
instead butgeom_text
works great here.