简体   繁体   English

R基本绘图功能:图形的图例不正确

[英]R base plot function: incorrect legend for the graph

I am having difficulties with the plot legends: 我在剧情传说上遇到了困难:

    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 测试图

This is the nice graph I get, which I think the black is DS, red is GS, and green is UB for treatments. 这是我得到的漂亮图表,我认为黑色是DS,红色是GS,绿色是UB。 The blocks (four different shapes) seem to be correct as well. 块(四种不同形状)似乎也是正确的。 However, when I make a legend with these same parameters, I get this: 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"))

All it is doing is repeating the shape three times with alternating colors, instead of matching with what (I think) the graph is showing. 它所做的只是用交替的颜色重复该形状3次,而不是与图形显示的内容匹配。 I tried the merge function, but that did not correct the issue (it produces the same incorrect legend). 我尝试了合并功能,但是并没有解决问题(它会产生相同的错误图例)。

~Is there also a way in the plot function to make the legend go underneath the graph and centered? 〜绘图功能中是否还可以使图例位于图形下方并居中? EDIT: I figured this out! 编辑:我想通了! just adjusted the ylim to go -3 and left space for a horizontal legend. 只需将ylim调整为-3并留出空间以放置水平图例。

~Maybe another thing; 〜也许是另一回事; how do I specifically assign each Treatment a specific color and each Block a specific shape, instead of letting R just use the first few options? 我该如何为每个“处理”分配一个特定的颜色,向每个“块”分配一个特定的形状,而不是让R仅使用前几个选项?

Thank you for your help! 谢谢您的帮助!

EDIT: I ended up making two separate legends to destinguish between the blocks and treatments. 编辑:我最终制作了两个单独的图例以区分块和处理之间。

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")

Perhaps this might help. 也许这可能有所帮助。 I specified the shapes and colors I wanted for the plot and for the legend . 我指定了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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM