繁体   English   中英

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

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

我在剧情传说上遇到了困难:

    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)

测试图

这是我得到的漂亮图表,我认为黑色是DS,红色是GS,绿色是UB。 块(四种不同形状)似乎也是正确的。 但是,当我使用相同的参数创建图例时,我得到的是: 图例位于右上角...

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

它所做的只是用交替的颜色重复该形状3次,而不是与图形显示的内容匹配。 我尝试了合并功能,但是并没有解决问题(它会产生相同的错误图例)。

〜绘图功能中是否还可以使图例位于图形下方并居中? 编辑:我想通了! 只需将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")

也许这可能有所帮助。 我指定了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