简体   繁体   中英

Text and second line is not being displayed with plot() and line()

None of my text is being displayed when I source my code in RStudio. Also the second line isn't being displayed. I am not sure why this is happening. my vectors alphas , acc_3 , and acc_1 all have values within them.

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")

在此处输入图片说明

We need to set the Y limits, so it includes values for both acc_1 and 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")

在此处输入图片说明


Using matplot (as suggested by @thelatemail in the comments):

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

Plot the columns of one matrix against the columns of another.

在此处输入图片说明


Or using ggplot , prepare tidy data, then plot:

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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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