2016-01-09 13 views
7

Ich möchte geom_dotplot verwenden, um zwei verschiedene Variablen durch die Form der Punkte zu unterscheiden (anstatt Farben wie in der Dokumentation vorgeschlagen). Zum Beispiel:R - ggplot geom_dotplot Form Option

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

das heißt den x- und y in dem unten stehenden Beispiel

enter image description here

ich all x gesetzt werden soll, unterscheiden Punkte zu sein, und y Dreiecken zu sein.

Ist das möglich? Danke!

+1

Sicher nicht ohne weiteres möglich. Du könntest eine dieser neumodischen 'ggplot2' Erweiterungen schreiben, nehme ich an. –

+0

Haben Sie [diese SO-Antwort] (http://stackoverflow.com/a/25632604/1305688) angeschaut? –

+3

sieht nicht möglich https://github.com/hadley/ggplot2/issues/1111 – MLavoie

Antwort

0

Mit den Informationen von geom_dotplot und der Stripchart-Funktion von Base R könnten Sie wahrscheinlich etwas Ähnliches hacken.

#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