I want to use geom_dotplot
to distinguish two different variables by shape of the dots (rather than colours as the documentation suggests). For example:
library(ggplot2)
set.seed(1)
x = rnorm(20)
y = rnorm(20)
df = data.frame(x,y)
ggplot(data = df) +
geom_dotplot(aes(x = x), fill = "red") +
geom_dotplot(aes(x=y), fill = "blue")
i.e. to distinguish the x and y in the below example
I want to set all the x to be dots, and y to be triangles.
Is this possible? Thanks!
All ggplot2 plots begin with a call to ggplot(), supplying default data and aesthethic mappings, specified by aes(). You then add layers, scales, coords and facets with +. To save a plot to disk, use ggsave(). ggplot() Create a new ggplot.
The ggplot2 shape parameter corresponds to the pch parameter of the R base graphics package (see the "pch" description on the help page of the points() function).
Dot plot. Source: R/geom-dotplot.r. geom_dotplot.Rd. In a dot plot, the width of a dot corresponds to the bin width (or maximum width, depending on the binning algorithm), and dots are stacked, with each dot representing one observation.
There are three options: If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot (). A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify () for which variables will be created.
You could probably hack together something similar to what you want using the information from geom_dotplot plus base R's stripchart function.
#Save the dot plot in an object.
dotplot <- ggplot(data = df) +
geom_dotplot(aes(x = x), fill = "red") +
geom_dotplot(aes(x=y), fill = "blue")
#Use ggplot_build to save information including the x values.
dotplot_ggbuild <- ggplot_build(dotplot)
main_info_from_ggbuild_x <- dotplot_ggbuild$data[[1]]
main_info_from_ggbuild_y <- dotplot_ggbuild$data[[2]]
#Include only the first occurrence of each x value.
main_info_from_ggbuild_x <-
main_info_from_ggbuild_x[which(duplicated(main_info_from_ggbuild_x$x) == FALSE),]
main_info_from_ggbuild_y <-
main_info_from_ggbuild_y[which(duplicated(main_info_from_ggbuild_y$x) == FALSE),]
#To demonstrate, let's first roughly reproduce the original plot.
stripchart(rep(main_info_from_ggbuild_x$x,
times=main_info_from_ggbuild_x$count),
pch=19,cex=2,method="stack",at=0,col="red")
stripchart(rep(main_info_from_ggbuild_y$x,
times=main_info_from_ggbuild_y$count),
pch=19,cex=2,method="stack",at=0,col="blue",add=TRUE)
#Now, redo using what we actually want.
#You didn't specify if you want the circles and triangles filled or not.
#If you want them filled in, just change the pch values.
stripchart(rep(main_info_from_ggbuild_x$x,
times=main_info_from_ggbuild_x$count),
pch=21,cex=2,method="stack",at=0)
stripchart(rep(main_info_from_ggbuild_y$x,
times=main_info_from_ggbuild_y$count),
pch=24,cex=2,method="stack",at=0,add=TRUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With