2017-12-18 8 views
0

범례가있는 ggplot에 백그라운드 데이터를 회색으로 표시하려고합니다. 내 목표는 범례에 회색 데이터 포인트를 포함 시키거나 매뉴얼 제목으로 두 번째 범례를 만드는 것입니다. 그러나 나는이 두 가지를하는 데 실패한다. 내 데이터가 긴 형식입니다. 여러 데이터 세트에 대해 ggplot2 범례 만들기

enter image description here

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 
내가 좋은 전설을 생산 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

채우기 매개 변수를 필요로하는 다른 기하 구조가없는 플롯을 가정 237,819,          다음은 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 당신에게 기본 라운드 점처럼 보이는 형태를 제공하지만, 색상 매개 변수에 추가하여 채우기 매개 변수를 받아들입니다. 그런 다음 을 aes() 외부로 남겨 두면서 xx의 geom_point 레이어의 채우기를 scale_fill_manual() (회색으로 채우기 범례 만들기)에 회색으로 설정할 수 있습니다 (색상 범례에 추가되지 않음).

yy의 geom_point 레이어의 색조는이 영향을받지 않습니다.

plot

추신 그냥 "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     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를 사용