Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - ggplot geom_dotplot shape option

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

enter image description here

I want to set all the x to be dots, and y to be triangles.

Is this possible? Thanks!

like image 475
Kevin Avatar asked Jan 09 '16 15:01

Kevin


People also ask

How do I create a ggplot2 plot in R?

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.

What is the shape parameter in ggplot2?

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).

What is the width of a dot plot in R?

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.

How do I override the plot data in ggplot?

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.


1 Answers

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)

enter image description here

#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)

enter image description here

like image 92
hmgeiger Avatar answered Oct 12 '22 04:10

hmgeiger