2016-05-16 52 views
0

私は、シンボルと線種の両方を使用してグループ間を区別する図をプロットしています(ポイントと95%信頼度の楕円)。ここでr ggplot2凡例:記号の下に線種を表示する方法は?

は同様の伝説を持つプロット例である:

bplot<-ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,group=Species,shape=Species,lty=Species))+ 
    geom_point(size=3)+geom_smooth(method="lm",se=F,color="black")+ 
    theme_minimal()+theme(legend.key.size=unit(1.2,"cm")) 
    bplot 

これに伴う問題は、彼らがシンボルを重複しているので、線種は、凡例に見えにくいということです。シンボルと同じ凡例に線種を表示する方法はありますか(単一のラベルを使用しますか?)、シンボルの下か両方が読み込み可能な方法で表示されますか?

+0

'legend.key.width =ユニット(1.5、 "CM")を試みる' –

+0

凡例の背景の幅を制御し、この場合には有用ではなかったlegend.key.width。 – collnell

+0

legend.key.widthは、凡例の背景の幅を制御します。キーを広げることで、より多くの行を見ることができます。したがって、行の種類は読みやすくなります。これはあなたが望むものではありませんか?図形を線の種類から分離するが、ラベルが1つの場合は、プロットgrobの構造を調べる必要があると思います。私はそれができると確信しています。 –

答えて

1

私は鍵が少し広くなっている場合には伝説が読めると思います。

library(ggplot2) 

bplot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, group = Species, 
          shape = Species,lty = Species)) + 
    geom_point(size = 3) + 
    geom_smooth(method = "lm", se = F, color = "black") + 
    theme_minimal() + 
    theme(legend.key.width = unit(1.5, "cm")) 

bplot 

enter image description here

しかし、あなたは、各キーの中にラインからポイントを分離したい場合は、その後、私はあなたがggplotのグロブを詳細に調べる必要があると思います。

enter image description here

library(grid) 

bplot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, group = Species, 
          shape = Species,lty = Species)) + 
    geom_point(size = 3) + 
    geom_smooth(method = "lm", se = F, color = "black") + 
    theme_minimal() + 
    theme(legend.key.width = unit(1.5, "cm"), 
     legend.key.height = unit(1, "cm"), 
     legend.key = element_rect(colour = "grey50", size = .5)) 

# Get the plot grob 
g = ggplotGrob(bplot) 

# Get the legend 
leg = g$grobs[[which(g$layout$name == "guide-box")]] 

# Get the relevant keys 
pos = grep("key-.-1-1", leg$grobs[[1]]$layout$name) 
# pos gets the point; pos+1 gets the line 

# Separate the line from the point within each key 
for(i in pos) { 
    leg$grobs[[1]]$grobs[[i]]$y = unit(0.6, "npc") 

    leg$grobs[[1]]$grobs[[i+1]]$children[[1]]$y0 = unit(0.3, "npc") 
    leg$grobs[[1]]$grobs[[i+1]]$children[[1]]$y1 = unit(0.3, "npc") 
} 

# Put the legend back into the plot 
g$grobs[[which(g$layout$name == "guide-box")]] = leg 

# Draw it 
grid.newpage() 
grid.draw(g) 
それとも、あなたは別の伝説をしたい場合は、ディビの答え@ここの記事の一つに指定されggplotのグロブ内の特定のgrobsを編集するための

+0

素晴らしい、ありがとう!これは非常に有用だった – collnell

1

guides機能が必要です。カスタマイズの詳細については、ドキュメントを参照してください。

bplot<-ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,group=Species,shape=Species,lty=Species))+ 
    geom_point(size=3)+geom_smooth(method="lm",se=F,color="black")+ 
    theme_minimal()+theme(legend.key.size=unit(1.2,"cm")) + 
    guides(
    shape = guide_legend(order = 1), 
    size = guide_legend(order = 2) 
) 
bplot 

enter image description here

+0

私は同じ伝説にそれらを保つためにまだ考えていた – collnell

0

一つのオプションを参照してください。それは、関連するgrobに到達するためにリスト内の長いリストを通る経路をたどることを伴う。場合によっては、gridの編集機能を使用して関連するgrobsを編集する方が簡単かもしれません(わずかながらもそうかもしれません)。

library(ggplot2) 
library(grid) 

# The plot 
bplot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, group = Species, 
          shape = Species,lty = Species)) + 
    geom_point(size = 3) + 
    geom_smooth(method = "lm", se = F, color = "black") + 
    theme_minimal() + 
    theme(legend.key.width = unit(1.5, "cm"), 
     legend.key.height = unit(1, "cm"), 
     legend.key = element_rect(colour = "grey50", size = .5)) 

# Get the plot grob 
g = ggplotGrob(bplot) 

# Get a list of the grobs 
grid.ls(grid.force(g)) 

grobsのリストを調べます。私たちが編集したいgro​​bsはリストの一番下にあります - 名前は "key"で始まります。凡例には3つのキーがあるので、3つのグループの3つのグループが3つあります。

キー-3-1-bg.4-2-4-2
キー3-1-1.4-2-4-2
キー-3:各グループ内で三grobsありますここで興味のない - -1-2.4-2-4-2
      GRID.segments.819

最初はバックグラウンドです。
2番目のポイントは、垂直位置を編集するポイントです。
3番目は、子を含むgrobを指します - GRID.segments - 線分。線分の垂直位置を編集したい。 editGrob()コマンドで

は、gPathグロブを編集することが一覧表示されますが、grep=TRUEは、正規表現を使用することができることを意味し、global=TRUEは、すべての試合には影響されることを意味します。したがって、編集ごとに1つのコマンドだけです。

# Edit the 'key' grobs 
# Move the point up a little 
g = editGrob(grid.force(g), gPath("key-[1-9]-1-1"), grep = TRUE, global = TRUE, 
     y = unit(0.6, "npc")) 

# Move the line segment down a little 
g = editGrob(grid.force(g), gPath("GRID.segments"), grep = TRUE, global = TRUE, 
     y0 = unit(0.3, "npc"), y1 = unit(0.3, "npc")) 

# Draw it 
grid.newpage() 
grid.draw(g) 

enter image description here

関連する問題