2017-02-14 61 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只使用前几个选项?

谢谢你的帮助!

编辑:我最终做出了两个独立的传说,以块和治疗之间的destinguish。

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