2017-12-18 152 views
0

凡例を含むggplotに背景データをグレーで自動的に表示しようとしています。私の目的は、凡例に灰色のデータポイントを含めるか、手動のタイトルで2番目の凡例を作成することです。しかし、私は2つのいずれかを行うことに失敗します。私のデータは長い形式です。複数のデータセットのggplot2凡例を作成

require(ggplot2) 

xx<-data.frame(observation="all cats",x=1:2,y=1:2) 
yy<-data.frame(observation=c("red cats","blue cats"),x=3:4,y=3:4) 

g<-ggplot() + 
    geom_point(aes(x,y, colour=factor(observation)), colour="grey60", size=5, data=xx) + 
    geom_point(aes(x,y, colour=factor(observation)), size=5, data=yy) + 
    scale_color_discrete(name = "ltitle") 

g 

enter image description here

私は素敵な伝説を生成する、rbind.data.frameでdata.framesをマージしようとしたが、その後私は灰色の背景データを色と同時にggplot色を維持することはできませんよ。私はその後、geom_pointsを追加する前に、複雑な空のプロットを作成する機能を、使用していますので、

g<-ggplot(aes(x,y, colour=factor(observation)), colour="grey60", data=xx) + 
    geom_point(size=5) + 
    geom_point(aes(x,y, colour=factor(observation)), size=5, data=yy) + 
    scale_color_discrete(name = "ltitle") 
g 

しかし、私は、これを行うことはできません。

また、私は、これは問題を解決することを実現しました。

答えて

0

、以下は他のgeom_point層に影響を与えることなく、あなたのバックグラウンドデータgeom_point層の色を修正する回避策です。

g <- ggplot() + 
    geom_point(aes(x, y, 
       fill = "label"),        # key change 1 
      shape = 21,          # key change 2 
      color = "grey50", size = 5, 
      data = xx) + 
    geom_point(aes(x, y, colour = factor(observation)), size = 5, data = yy) + 
    scale_color_discrete(name = "ltitle") + 
    scale_fill_manual(name = "", values = c("label" = "grey50")) # key change 3 
g 

shape = 21あなたのデフォルト丸い点のように見えますが、色パラメータに加えて、フィルパラメータを受け入れる形状を与えます。 color = "grey50"aes()の外側に残して、xxのgeom_pointレイヤの塗りつぶしをscale_fill_manual()(これは塗りの凡例を作成)にグレーに設定できます(これは色の凡例には追加されません)。

yyのgeom_pointレイヤーのカラースケールは、この影響を受けません。

plot

P.S.私は "grey60"の代わりに "grey50"を使用したことを認識しました...しかし、それ以外のものはまだ適用されます。 :)

0

カラーベクトルを作成してscale_color_manualに渡す方法があります。

xx <- data.frame(observation = "all cats",x = 1:2,y = 1:2) 
yy <- data.frame(observation = c("red cats", "blue cats"),x = 3:4,y = 3:4) 
# rbind both datasets 
# OP tried to use rbind.data.frame here 
plotData <- rbind(xx, yy) 

# Create color vector 
library(RColorBrewer) 
# Extract 3 colors from brewer Set1 palette 
colorData <- brewer.pal(length(unique(plotData$observation)), "Set1") 
# Replace first color first wanted grey 
colorData[1] <- "grey60" 

# Plot data 
library(ggplot2) 
ggplot(plotData, aes(x, y, colour = observation)) + 
    geom_point(size = 5)+ 
    scale_color_manual(values = colorData, name = "ltitle") 

                                                          01あなたのプロットはフィルのパラメータを必要とする他のgeomを持っていないと仮定すると、              enter image description here

0

私はZ.Linと同じ解決策を出しましたが、rbind.data.frameの結合データフレームを使用しました。

require(ggplot2) 

xx<-data.frame(observation="all cats",x=1:2,y=1:2) 
yy<-data.frame(observation=c("red cats","blue cats"),x=3:4,y=3:4) 

zz <- rbind.data.frame(xx,yy) 

colors <- c(
    "all cats" = "grey60", 
    "red cats" = "red", 
    "blue cats" = "blue" 
) 

g<-ggplot() + 
    geom_point(aes(x,y, colour=factor(observation)), size=5, data=zz) + 
    scale_color_manual(values= colors, name = "ltitle") 
g 

enter image description here

:同様に、カラーマッピングを指定するベクトル colorsscale_colour_manualを使用します
関連する問題