2017-02-14 4 views
0

에 대한 잘못된 전설 :R 기본 플롯 기능 : 나는 플롯 전설 어려움 데 그래프

head(bee.ground) 
    X Month Treatment Block Bee_Richness Bee_Abundance Bare Grass Forb Dead Moss 
1 1 May  DS  1   0    0 23.20 15.72 37.80 17.00 0 
2 2 May  GS  1   0    0 33.52 21.88 33.60 9.88 0 
3 3 May  UB  1   1    1 0.60 18.28 35.00 43.48 0 
4 4 May  DS  2   7   71 11.20 11.20 58.80 16.68 0 
5 5 May  GS  2   5    6 37.00 12.08 43.92 5.12 0 
6 6 May  UB  2   5   16 4.40 14.88 12.32 67.88 0 

    shape<-as.numeric(as.factor(bee.ground$Block)) 
    color<-as.numeric(as.factor(bee.ground$Treatment)) 

    plot(bee.ground$Bare, bee.ground$Bee_Richness, main = "Bee Richness and Bare Ground Cover", 
xlab = "Percent Bare Ground", ylab = "Bee Richness", 
pch = shape, 
col = color, 
las = 1, 
cex = 1.5) 

test graph

이 내가 얻을 좋은 그래프, 나는 블랙 DS 생각, 빨간색은 GS, 녹색은 치료를위한 UB입니다. 블록 (네 가지 다른 모양)도 정확합니다. 나는이 같은 매개 변수와 함께 전설을 할 때 그러나,이 얻을 : legend's in the top corner...

legend("topleft", 
    pch = shape, 
    col = color, 
    legend = c("Block 1","Block 2","Block 3","Block 4", NA, "DS","GS","UB")) 

을 수행중인 모든 대신에 (내가 생각하는) 것과 그래프 일치의 교대 색으로 모양을 세 번 반복입니다 보여주고있다. 병합 기능을 시도했지만 문제가 해결되지 않았습니다 (동일한 잘못된 범례가 생성됨).

~ 플롯 기능에서 범례를 그래프 아래로 중앙에 배치하는 방법이 있습니까? 편집 : 나는 이것을 알아 냈다! 단지 ylim을 -3로 조정하고 가로 전설을위한 공간을 남겨 두었습니다.

~ 아마도 다른 것; R이 처음 몇 가지 옵션을 사용하는 대신 특정 색상을 처리하고 특정 모양을 차단하는 방법은 무엇입니까?

도움 주셔서 감사합니다.

편집 : 블록과 처리를 구분하기 위해 두 개의 별도 전설을 작성했습니다.

shape <- ifelse(bee.ground$Block == "1", 1,ifelse(bee.ground$Block == "2", 2, ifelse(bee.ground$Block == "3",3,4))) 
color <- ifelse(bee.ground$Treatment == "DS", 'red',ifelse(bee.ground$Treatment == "GS", 'green', 'black')) 

plot(bee.ground$Bare, bee.ground$Bee_Richness, main = "Bee Richness and Bare Ground Cover", 
    xlab = "Percent Bare Ground", ylab = "Bee Richness",pch = c(shape), 
    col = c(color),las = 1,cex = 1.5,ylim = c(0,35)) 
legend("topleft", c('1','2','3','4'),pch = c(1,2,3,4),horiz = TRUE,title = "Block") 
legend("topright",c("DS","GS","UB"),horiz = TRUE, text.col = c("red","green","black"),title = "Treatment",title.col = "black") 

답변

0

아마도 도움이 될 수 있습니다. plotlegend에 대해 원하는 모양과 색상을 지정했습니다.

shape <- as.numeric(as.factor(bee.ground$Block)) 
color <- as.numeric(as.factor(bee.ground$Treatment)) 

plot(bee.ground$Bare, bee.ground$Bee_Richness, 
    main = "Bee Richness and Bare Ground Cover", 
    xlab = "Percent Bare Ground", ylab = "Bee Richness", 
    pch = shape, col = color, las = 1, cex = 1.5) 

    legend("topleft", c('Block DS, Treatment 1', 
         'Block DS, Treatment 2', 
         'Block DS, Treatment 3', 
         'Block GS, Treatment 1', 
         'Block GS, Treatment 2', 
         'Block GS, Treatment 3', 
         'Block UB, Treatment 1', 
         'Block UB, Treatment 2', 
         'Block UB, Treatment 3'), 
         pch = c(1,1,1,2,2,2,3,3,3), 
         col = c('blue', 'green', 'brown', 
           'blue', 'green', 'brown', 
           'blue', 'green', 'brown')) 


shape <- ifelse(bee.ground$Block == 'DS', 3, 
     ifelse(bee.ground$Block == 'GS', 6, 9)) 

color <- ifelse(bee.ground$Treatment == 1, 'red', 
     ifelse(bee.ground$Treatment == 2, 'blue', 'black')) 

plot(bee.ground$Bare, bee.ground$Bee_Richness, 
    main = "Bee Richness and Bare Ground Cover", 
    xlab = "Percent Bare Ground", ylab = "Bee Richness", 
    pch = shape, col = color, las = 1, cex = 1.5) 

    legend("topleft", c('Block DS, Treatment 1', 
         'Block DS, Treatment 2', 
         'Block DS, Treatment 3', 
         'Block GS, Treatment 1', 
         'Block GS, Treatment 2', 
         'Block GS, Treatment 3', 
         'Block UB, Treatment 1', 
         'Block UB, Treatment 2', 
         'Block UB, Treatment 3'), 
         pch = c(3,3,3,6,6,6,9,9,9), 
         col = c('red', 'blue', 'black', 
           'red', 'blue', 'black', 
           'red', 'blue', 'black')) 
+0

고마워요! 내 원래 게시물에 몇 가지 편집을 넣어 :) – Brenna