使用plot()和line()不显示文本和第二行

时间:2018-04-10 20:08:55

标签: r

当我在RStudio中source我的代码时,我的文字都没有显示出来。第二行也没有显示出来。我不确定为什么会这样。我的向量alphasacc_3acc_1都包含值。

alphas = c(0.050, 0.075, 0.100, 0.150, 0.175, 0.200, 0.225, 0.250, 0.275, 0.300)
acc_1 = c(0.9997631, 0.9999210, 0.9995263, 0.9980261, 1.0000000, 0.9996052, 1.0000000, 0.9999210, 1.0000000, 0.9996052)
acc_3 = c(0.9526814, 0.9626709, 0.9563617, 0.9447950, 0.9616193, 0.9600421, 0.9521556, 0.9505783, 0.9490011, 0.9463722)  
plot(alphas, acc_1, type="l", xlab="Alpha", ylab="Acc", col="red")
lines(alphas, acc_3, col="green")

enter image description here

1 个答案:

答案 0 :(得分:3)

我们需要设置Y限制,因此它包含 acc_1 acc_3 的值:

myYlim <- range(c(acc_1, acc_3))

plot(alphas, acc_1, type = "l", xlab = "Alpha", ylab = "Acc", col = "red", ylim = myYlim)
lines(alphas, acc_3, col = "green")

enter image description here

使用 matplot (在评论中@thelatemail建议):

matplot(alphas, cbind(acc_1,acc_3),
        type = "l", lty = 1, col = c("red", "green"), ylab = "value")
  

将一个矩阵的列绘制在另一个矩阵的列上。

enter image description here

或者使用 ggplot ,准备整洁的数据,然后绘制:

library(dplyr)
library(tidyr)
library(ggplot2)

plotDat <- data.frame(alphas, acc_1, acc_3) %>% 
  gather(key = "acc", value = "value", -alphas)

ggplot(plotDat, aes(alphas, value, col = acc)) +
  geom_line()

enter image description here