R中的图例显示不正确

时间:2018-08-15 14:16:51

标签: r plot legend

几个月来,我一直在试图找出解决这个问题的方法。总是在我要绘制图例时,图例似乎出现在图的底部。

这是我的数据:

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)

在这里您可以在插入图例之前查看该图 Here you can see the plot before inserting the legend

现在我生成图例:

legend("topright", legend=c("male", "female"),
       fill=c("red", "green"), lty=1:2)

在这里您可以看到带有图例的情节:

Here you can see the plot with the legend:

为什么会这样?

0 个答案:

没有答案