2016-01-09 13 views
7

geom_dotplotを使って、ドットの形で2つの異なる変数を区別したいと思います。例えば:R - ggplot geom_dotplotシェイプオプション

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

すなわち

enter image description here

私はドットであることをすべてのXを設定する以下の例では、xとyを区別するために、yは三角形であること。

これは可能ですか? ありがとう!

+1

確かではないことが容易に可能。私が想定しているような新鮮な 'ggplot2'拡張の1つを書くことができます。 –

+0

あなたは[これは答え]を見ましたか(http://stackoverflow.com/a/25632604/1305688)? –

+3

が表示されないhttps://github.com/hadley/ggplot2/issues/1111 – MLavoie

答えて

0

おそらくgeom_dotplotと基数Rのストリップチャート関数の情報を使いたいものと似たものを一緒にハックすることができます。

#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