2013-10-10 3 views
3

나는 한 기간 동안 두 그룹간에 3 개의 다른 결과를 비교하는면 처리 된 꺾은 선형 차트를 만들었습니다. 그룹 중 하나에서 데이터 라벨을 조정할 수 있기를 원합니다. 즉, 한 그룹의 라벨이 데이터 포인트 위에 표시되고 두 번째 그룹의 라벨이 데이터 포인트 아래에 나타납니다. 어떤 도움을 주시면 감사하겠습니다ggplot의 데이터 레이블 조정

Year <- c("Y1", "Y2","Y3", "Y1", "Y2","Y3", "Y1", "Y2","Y3", "Y1", "Y2","Y3","Y1", "Y2","Y3", 
      "Y1","Y2","Y3") 
Group <- c("Group A", "Group A", "Group A", "Group A", "Group A", "Group A", "Group A", "Group A", "Group A", 
      "Group B", "Group B", "Group B", "Group B","Group B", "Group B", "Group B", "Group B", "Group B") 
Test <- c("Test 1", "Test 1", "Test 1", "Test 2", "Test 2", "Test 2", "Test 3", "Test 3", "Test 3", 
      "Test 1", "Test 1", "Test 1","Test 2", "Test 2", "Test 2","Test 3", "Test 3", "Test 3") 
Score <- c(68,70,73,61,62,65,61,62,65, 
      75,74,76,74,74,77,70,71,69) 
df <- data.frame (Year, Group, Test, Score) 

library(ggplot2) 

ggplot (df, aes (x=Year, y=Score, group=Group)) + geom_line(aes(group=Group), size=1.5) + facet_grid(.~ Test) 
ggplot(df, aes(x=Year, y=Score, colour=Group)) + geom_line(aes(group=Group), size=1.5) + 
    facet_grid(.~ Test) + 
    geom_point(size=4, shape=21) + 
    geom_text(aes(label = Score, vjust=-1))+ 
    scale_y_continuous(limits = c(0,100), breaks=seq(0,100,20)) + 
    ylab("Percentage of Students") + xlab ("Year") +    
    labs(title = "Chart Title") + 
    theme(strip.text.x = element_text(size = 15, colour="black", angle = 0), 
     strip.background = element_rect(colour="white", fill="white") 
     ) 

:

여기 내 코드입니다.

답변

3

Group 대한 Y의 상이한 위치를 설정할 geom_text()aes() 내부 ifelse() 기능을 사용할 수 - 5 개 값보다 하나 개의 그룹과 낮은 다른 5 개 값.

ggplot(df, aes(x=Year, y=Score,colour=Group)) + geom_line(aes(group=Group),size=1.5) + 
    facet_grid(.~ Test) + 
    geom_point(size=4, shape=21) + 
    geom_text(aes(y=ifelse(Group=="Group B",Score+5,Score-5),label = Score))+ 
    scale_y_continuous(limits = c(0,100), breaks=seq(0,100,20)) 

enter image description here

+0

브릴리언트! 감사. – Greg