[英]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")
也許這可能有所幫助。 我指定了plot
和legend
所需的形狀和顏色。
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.