R图例未在Plot中显示

问题描述 投票:0回答:1

我有一个简单的情节的以下R代码:

ExperimentDataNames = c('Count', 'HumanData', 'ActualPrices')
ExperimentData <- read_csv("/Users/justin_chudley/desktop/ExperimentData.csv", col_names = ExperimentDataNames)

x <- Count <- ExperimentData$Count
y <- HumanData <- ExperimentData$HumanData
y1 <- ActualPrices <- ExperimentData$ActualPrices

plot(x,y, type = "l", xlab="Trial Number",ylab="$USD",main="Dow Jones Price vs Human Experiment")
lines(x,y1, type = "l", col=2)
legend=c('Human Data', 'Actual Prices') 

由于某种原因,该图中的图例根本没有显示:enter image description here

为什么我的传奇没有显示?

r plot legend
1个回答
5
投票

通过编码,您已将一个字符向量分配给名为legend的对象。

要添加图例,您需要使用legend()函数。

legend(x = 10, y = 4e5, 
       col = c("black", "red"), lty = 1, lwd = 1,
       legend = c('Human Data', 'Actual Prices'))

您可以通过改变xy中的值来使用启发式方法,直到找到您喜欢的位置。或者,您也可以将x设置为以下几个预定义值之一:

legend(x = "top",
       col = c("black", "red"), lty = 1, lwd = 1,
       legend = c('Human Data', 'Actual Prices'))

其他选项是将x设置为“bottomright”,“bottom”,“bottomleft”,“left”,“topleft”,“topright”,“right”或“center”。

© www.soinside.com 2019 - 2024. All rights reserved.