简体   繁体   English

R图例未在图中显示

[英]R legend not showing in Plot

I have the following R code for a simple plot: 我有以下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') 

The legend does not show at all in this plot for some reason: 由于某些原因,图例在此图中完全不显示: 在此处输入图片说明

Why is my legend not showing? 为什么我的传说没有显示?

With your coding, you have assigned a vector of characters to an object named legend . 通过编码,您已将字符向量分配给名为legend的对象。

In order to add a legend, you need to use the legend() function. 为了添加图例,您需要使用legend()函数。

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

You can use a heuristic approach by varying the values in x and y until you find a position you like. 您可以通过改变xy的值来使用启发式方法,直到找到所需的位置为止。 Alternatively, you can also set x to one of several predefined values: 或者,您也可以将x设置为几个预定义值之一:

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

Other options are to set x to "bottomright", "bottom", "bottomleft", "left", "topleft", "topright", "right" or "center". 其他选项是将x设置为“底部”,“底部”,“底部左”,“左侧”,“顶部左”,“顶部右”,“右侧”或“中心”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM