几个月来,我一直在试图找出解决这个问题的方法。总是在我要绘制图例时,图例似乎出现在图的底部。
这是我的数据:
weight <- data.frame(c("Ben", "Tom", "Toni", "Clive", "Alex"),
c(72, 65, 87, 75,77),
c("m", "m", "f", "m", "f"))
colnames(weight) <- c("Name", "Weight", "Gender")
现在我生成图:
plot(sort(weight$Weight, decreasing = T), type = "h",
col = ifelse(weight$Gender == "f",'red','green'),
pch = 19, xaxt = "n")
axis(side=1, at=1:5, labels=weight$Name)
在这里您可以在插入图例之前查看该图
现在我生成图例:
legend("topright", legend=c("male", "female"),
fill=c("red", "green"), lty=1:2)
在这里您可以看到带有图例的情节:
为什么会这样?