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

R基本绘图功能:图表的图例不正确,r,plot,legend,R,Plot,Legend,我对剧情传说有困难: 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 3

我对剧情传说有困难:

    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)
它所做的只是用交替的颜色重复形状三次,而不是与(我认为)图形所显示的内容相匹配。我尝试了merge函数,但这并没有纠正这个问题(它会生成相同的错误图例)

~ plot函数中是否也有方法使图例位于图形下方并居中?编辑:我想出来了!只需将ylim调整为go-3,并为水平图例留出空间

~z~也许还有一件事;我如何专门为每个处理指定特定的颜色和每个块指定特定的形状,而不是让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")

shape也许这会有所帮助。我为
绘图和
图例指定了所需的形状和颜色

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'))
形状
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'))