0

ggplot2 を使用して 2 つの個別の散布図を作成しましたが、それらを 1 つの単一のプロットに結合する必要があります。各プロットは、3 つの異なる処理 (背景) の下にあるトカゲの集団を対象としています。各プロットについて、次のものがあります。

csMS = data.frame()
ellMS = data.frame()
centroidsMS = data.frame()  

csplotMS = ggplot(csMS, aes(x = RG, y =  GB, colour = Background)) + geom_point(size = 3, shape = 17) + #colour by background, circles size 3
  geom_path(data = ell.AS, aes(x = RG, y = GB ,colour = Background), size = 1, linetype = 2) + #adding the ellipses
  geom_point(data = centroidsMS, size = 3, shape = 17) + #added centroids     
  geom_errorbar(data = centroidsMS, aes(ymin = GB - se.GB, ymax = GB + se.GB), width = 0) + #add y error bars
  geom_errorbarh(data = centroidsMS, aes(xmin = RG - se.RG, xmax = RG + se.RG), height = 0) +
  theme_bw() + #white background
  theme(axis.title.y = element_text(vjust = 2), axis.title.x = element_text(vjust = -0.3)) + #distance of axis titles from axis
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #no grids
        axis.line = element_line(colour = "black")) + #black axes
  theme(text = element_text(size = 30)) +  #font size
  ylab("(G-B)/(G+B)") + xlab("(R-G)/(R+G)") + # Set text for axes labels
  scale_colour_manual(values = c("black","#FF6600", "yellow1")) + #changed default colours
  labs(colour = "Murray Sunset NP") +
  theme(legend.title = element_text(size = "20")) + #changes the legend title
  theme(legend.text = element_text(size = "20")) + #changes the legend title
  theme(legend.key = element_blank()) + #removed little squares around legend symbols
  theme(legend.direction = "horizontal", legend.position = c(.5, .85))

私は試した

csASMS = csplotAS + csplotMS

しかし、次のエラー メッセージが表示されます。

私も試しました

csASMS = grid.arrange(csplotAS, csplotMS)

しかし、これは一方のプロットをもう一方の上に配置しますが、両方のプロットを組み合わせて、基本的には 1 つのプロットだけにする必要がありますが、各プロットには異なるトカゲの個体数を示すための異なる規則があるため、2 つの個別の凡例があります。

どんな助けでも大歓迎です。

****編集**** 2014 年 12 月 12 日

2 つのプロットを 1 つにまとめることができましたが、まだ別々の凡例の問題があります。質問を単純化するために、cdeterman の要求に従って、いくつかのサンプル データを使用して、より単純な形式のコードを追加しています。

data frames: p1 and p2

> p1
  treatment x y
1     Black 1 1
2    Orange 2 2
3    Yellow 3 3

> p2
  treatment x y
1    Black  4 4
2    Orange 5 5
3    Yellow 6 6

次のコードを使用して、両方のデータ フレームを含むプロットを作成しました。

plot = ggplot(p1, aes(x = x, y =  y, colour = treatment)) +     geom_point(size = 3) + #colour by background, circles size 3
  theme_bw() + #white background
  theme(axis.title.y = element_text(vjust = 2), axis.title.x = element_text(vjust = -0.3)) + #distance of axis titles from axis
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #no grids
  axis.line = element_line(colour = "black")) + #black axes
  theme(text = element_text(size = 30)) +  #font size
  scale_colour_manual(values = c("black","#FF6600", "yellow1")) + #changed default colours
  labs(colour = "p1") +
  theme(legend.title = element_text(size = "20")) + #changes the legend title
  theme(legend.text = element_text(size = "20")) + #changes the     legend title
  theme(legend.key = element_blank()) + #removed little squares around legend symbols
  theme(legend.direction = "horizontal", legend.position = c(.33, 1)) +

  # Now to add the second plot/ No need to code for axis titles, titles positions,etc b/c it's already coded in the first plot
  geom_point(data = p2, aes(x = x, y = y, colour = treatment), size = 3, shape = 17)

これにより、各データ フレームが異なるシンボル (p1 の円と p2 の三角形) で表されるグラフが生成されますが、円の上に三角形が重ねられた組み合わせの凡例は 1 つだけです)。データ フレームごとに 1 つずつ、2 つの個別の凡例を取得するにはどうすればよいですか?

ありがとうございました!

4

1 に答える 1

1

いくつかの調査を行い、さまざまなことを試した後、問題の一部を解決することができました。2 つのプロットを一緒に追加するには、1 つを最初にプロッタにし、もう 1 つを最初のものの上に追加する必要があります。

geom.point()

私の新しいコードは次のようになります。

csplotASMS = ggplot(csAS, aes(x = RG, y =  GB, colour = Background)) + geom_point(size = 3) + #colour by background, circles size 3
  geom_path(data = ell.AS, aes(x = RG, y = GB ,colour = Background), size = 1, linetype = 1) + #adding the ellipses
  geom_point(data = centroidsAS, size = 4) + #added centroids     
  geom_errorbar(data = centroidsAS, aes(ymin = GB - se.GB, ymax = GB + se.GB), width = 0) + #add y error bars
  geom_errorbarh(data = centroidsAS, aes(xmin = RG - se.RG, xmax = RG + se.RG), height = 0) +
  theme_bw() + #white background
  theme(axis.title.y = element_text(vjust = 2), axis.title.x = element_text(vjust = -0.3)) + #distance of axis titles from axis
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #no grids
  axis.line = element_line(colour = "black")) + #black axes
  theme(text = element_text(size = 30)) +  #font size
  ylab("(G-B)/(G+B)") + xlab("(R-G)/(R+G)") + # Set text for axes labels
  scale_colour_manual(values = c("black","#FF6600", "yellow1")) + #changed default colours
  labs(colour = "Alice Springs") +
  theme(legend.title = element_text(size = "20")) + #changes the legend title
  theme(legend.text = element_text(size = "20")) + #changes the legend title
  theme(legend.key = element_blank()) + #removed little squares around legend symbols
  theme(legend.direction = "horizontal", legend.position = c(.33, 1)) +

  # Now to add the second plot/ No need to code for axis titles, titles positions,etc b/c it's already coded in the first plot
  geom_point(data = csMS, aes(x = RG, y = GB, colour = Background), size = 3, shape = 17) +
  geom_path(data = ell.MS, aes(x = RG, y = GB ,colour = Background), size = 1, linetype = 2) + #adding the ellipses
  geom_point(data = centroidsMS, size = 4, shape = 17) + #added centroids     
  geom_errorbar(data = centroidsMS, aes(ymin = GB - se.GB, ymax = GB + se.GB), width = 0) + #add y error bars
  geom_errorbarh(data = centroidsMS, aes(xmin = RG - se.RG, xmax = RG + se.RG), height = 0) #add x error bars

グラフは、それぞれ 3 つの処理を行った 2 つの集団の散布図を示しています。処理は両方の母集団で同じであるため、母集団の違いを示すために同じ色を使用しますが、異なる記号を使用します。母集団の 1 つは円で、もう 1 つは三角形です。

さて、まだ答えられない部分は、「プロット」ごとに 1 つずつ、2 つの個別の凡例を作成する方法です。つまり、1 つは円用で、もう 1 つは三角形用です。現時点では、円の上に三角形を重ね合わせた「結合された凡例」があります。各凡例には独自のタイトルが必要です。

于 2014-12-12T05:55:12.843 に答える