2017-12-21 5 views
-3

ggplot2를 사용하여 플롯을 생성했지만 사용자 정의 범례 작성에 어려움이 있습니다. 빨간색 부분은 내가 추가 할 필요 동안 지점 옆에 사각형이 실제로 레이블 검은 색 enter image description here그리기 범례 in ggplot

부품이 ggplot으로 무엇을 만들었습니다,하지만 난이 필요합니다. 내 줄거리는 다음과 같습니다 범례의 일부가되는 윤곽선 라벨 (직사각형). 나는 관련 문제를 언급했지만, "매핑"보다는 범례에서 더 많은 "그림"으로 해석 할 때까지 비슷한 문제를 보지 못했습니다.

누구든지 나를 도울 수 있을까요? 나는 많은 수의 플롯을 가지고 있기 때문에 이미지 편집기로 플롯을 수동으로 편집하는 것보다 R을 사용하여 문제를 해결하는 것을 선호합니다. 감사!

편집 : 요청에 따라 재현 가능한 샘플. 미리 추가하지 않으셔서 죄송합니다!

library(ggrepel) 
dput(df) 
structure(list(ID = 1L, Date = structure(16259, class = "Date"), 
    Primary = 0.009, Secondary = structure(1L, .Label = "Label ABC", class = "factor")), row.names = c(NA, 
-1L), class = "data.frame", .Names = c("ID", "Date", "Primary", 
"Secondary")) 

ggplot(df, aes(Date, Primary)) + geom_point(aes(shape=ifelse(Primary>0, "Detected", "Not Detected"))) + geom_label_repel(aes(label=Secondary)) + scale_shape_manual(values=c("Not Detected"=1,"Detected"=19),name="Primary") + theme_bw() 
+0

당신은 당신이 플롯을 생성하는 데 사용되는 코드를 게시 할 수 있을까요? – guscht

+0

이 [게시물] (https://stackoverflow.com/questions/23635662/editing-legend-text-labels-in-ggplot)을 보았습니까? "예"이고 문제가 해결되지 않은 경우 [재현 가능한 예] (https://stackoverflow.com/questions/5963269/how-to-make-a-great-)를 입력하여 질문을 편집하시는 것이 좋습니다. r 재현 가능한 예). 이렇게하면 다른 사람들이 실제로 당신을 더 잘 돕도록 도울 것입니다. – Ashish

+0

죄송합니다. 감사합니다. – phusion

답변

2

귀하의 데이터 :

df <- data.frame(stringsAsFactors=FALSE, 
      ID = c(1L, 2L), 
      Date = c("2014-07-08", "2017-03-12"), 
      Primary = c(0.009, -0.05), 
      Secondary = c("Label ABC", "Label BCD") 
) 

여기 ggplot 솔루션 기본입니다. 기본적으로 숨겨진 사각형이 각 점 뒤에 있으며 범례에 표시되는 사각형입니다. color과 같은 다른 표정으로 배치해야하며 포인트는 shape으로 구분되어 하나의 범례 항목으로 포인트와 결합되지 않도록해야합니다.

library(ggrepel) 


df %>% 
    mutate(isDetected=ifelse(Primary>0, "Detected", "Not Detected")) %>% 
    ggplot(aes(Date, Primary)) + 
    geom_rect(aes(xmin=Date, xmax=Date, ymin=Primary, ymax=Primary, color=isDetected), 
       fill="white")+ 
    geom_point(aes(shape=isDetected), size=3) + 
    geom_label_repel(aes(label=Secondary, color=isDetected), show.legend = FALSE) + 
    scale_shape_manual(values=c("Not Detected"=1,"Detected"=19))+ 
    labs(shape="Primary", 
     color="Secondary")+ 
    theme_bw() 

enter image description here