2017-09-01 3 views
1

で2つのオーバーレイプロットの形状をマージ:How to merge color, line style and shape legends in ggplotggplotが、ここで提起したとして、私は同様の問題を抱えている伝説

私が行っクラスタリングと一緒にアイリスデータセットから種をプロットしたいと思います。簡単にするために、のは、どういうわけか

c(rep(1,45),rep(2,59),rep(3,46))) 

によって与えられたクラスタリングイストを想定してみましょう、上に引用ポストからの提案は、私は15(箱)形状たい、伝説で私

library(ggplot2) 
library(scales) 

ggplot(iris, aes(Petal.Length, Petal.Width, color = Species)) + 
geom_point(shape = 18, size = 7) + 
geom_point(shape = 15, size = 3,data = iris, 
    aes(iris$Petal.Length, iris$Petal.Width, 
     color = as.factor(c(rep(1,45),rep(2,59),rep(3,46))))) + 
scale_color_manual(values=rep(alpha(c("#00B6EB","#F8766D", "#53B400"),0.5),2)) + 
scale_shape_manual("",values=c(18,18,18,15,15,15)) 

のために動作しません。種のためのクラスター1,2,3および形状18(菱形)のために。ここで

enter image description here

答えて

2

非常に無粋なソリューションです:

library(ggplot2) 
library(scales) 
iris$clust <- factor(c(rep(1,45),rep(2,59),rep(3,46))) 

p <- ggplot(iris, aes(Petal.Length, Petal.Width, color = Species))+ 
geom_point(shape=18, size=7) + 
geom_point(shape=15, size=3, data=iris, 
      aes(iris$Petal.Length, iris$Petal.Width, color=clust)) + 
scale_color_manual(values=rep(alpha(c("#00B6EB","#F8766D", "#53B400"),0.5),2))+ 
scale_shape_manual("",values=c(18,18,18,15,15,15)) 

# Generate a ggplot2 plot grob 
g <- ggplotGrob(p) 

# Set the color of unwanted shapes in legend to background color 
g$grobs[[15]][[1]][[1]][[1]][[4]]$gp$col <- NA 
g$grobs[[15]][[1]][[1]][[1]][[7]]$gp$col <- NA 
g$grobs[[15]][[1]][[1]][[1]][[10]]$gp$col <- NA 

g$grobs[[15]][[1]][[1]][[1]][[14]]$gp$col <- NA 
g$grobs[[15]][[1]][[1]][[1]][[17]]$gp$col <- NA 
g$grobs[[15]][[1]][[1]][[1]][[20]]$gp$col <- NA 

library(grid) 
grid.draw(g) 

enter image description here

+0

は確かに働くあなたのポスト、をいただき、ありがとうございます。どのように簡単な解決策が私の投稿と引用されたポストを参照してください動作しませんか? – Dan

関連する問題