R图例未在图中显示

use*_*738 0 plot r legend

我有以下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') 
Run Code Online (Sandbox Code Playgroud)

由于某些原因,图例在此图中完全不显示: 在此处输入图片说明

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

NM_*_*NM_ 5

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

为了添加图例,您需要使用该legend()功能。

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

您可以使用启发式方法,方法是更改​​和中的值xy直到找到所需的位置。或者,您也可以设置x为几个预定义值之一:

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

其他选项设置x为“底部”,“底部”,“底部左”,“左侧”,“顶部左”,“顶部右”,“右侧”或“中心”。