首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R基础图函数:图形的图例不正确

R基础图函数:图形的图例不正确
EN

Stack Overflow用户
提问于 2017-02-14 12:14:14
回答 1查看 101关注 0票数 0

我对剧情的传说有些困难:

代码语言:javascript
复制
    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...

代码语言:javascript
复制
legend("topleft", 
   pch = shape, 
   col = color, 
   legend = c("Block 1","Block 2","Block 3","Block 4", NA, "DS","GS","UB"))

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

~在plot函数中有没有一种方法可以让图例在图形下面居中显示?编辑:我想通了!刚刚调整了ylim为go -3,并为水平图例留出了空间。

~也许是另一件事;我如何具体地为每个处理分配一个特定的颜色和每个块一个特定的形状,而不是让R只使用前几个选项?

谢谢你的帮助!

编辑:我最终制作了两个独立的图例来区分块和治疗。

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

发布于 2017-02-14 15:38:54

也许这会有所帮助。我为plotlegend指定了我想要的形状和颜色。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42217747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档