For months I've been trying to figure out how to solve this problem. Always when I want to plot a legend, the legend seem to appear at the bottom of the plot.
Here is my data:
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")
Now I generate the plot:
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
Now I generate the legend:
legend("topright", legend=c("male", "female"),
fill=c("red", "green"), lty=1:2)
Here you can see the plot with the legend:
Why does this happen?