简体   繁体   中英

R- how to plot multiple overlaying graphs/scatterplots?

I am working on a Machine Learning problem where I am trying to compare my predictions using 2 different ML methods with the original test-values and see how they compare, and I am trying to visualize the results. I have done 2 separate scatter plots (between the test and predicted values) but I wanted to see how the values differed from point to point. So I tried subsetting my data to where I have 100 ytest values, 100 predicted values (ymle_predict) using the first ML algorithm, and 100 predicted values (ymap_predict) using the 2nd ML algorithm.

I want my x axis to represent each data-point, and the y axis to represent the 3 different values (ytest, ymle_predict and ymap_predict) that data-point.

So let's say we have:

ytest<- c(1, 2, 3, 4, 5, 6, 7 )
ymle_predict<-c(1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1 )
ymap_predict <- c(1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2 )

where the values are in order so 2nd element in ymle_predict represent the data-point prediction for the 2nd element in ytest.

I would like to see how these points vary from point to point, similar to let's say how we can see overlapping plots for price changes over time for different stocks (with a different color representing each stock). Instead of time being the x axis however, here it would just be a counter variable here like :

i<- c(1,2,3,4,5,6,7)

I have tried putting these elements in a dataframe but that didn't help me come up with any answers.

Using ggplot and dplyr:

library(ggplot2)
library(dplyr)

df <- data.frame(ytest = c(1, 2, 3, 4, 5, 6, 7 ),
                 ymle_predict = c(1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1 ),
                 ymap_predict = c(1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2 ),
                 i = c(1,2,3,4,5,6,7))

df.plot <- df %>%
  gather(results, value, -c("i"))


ggplot(df.plot, aes(x=i, y=value, color=results)) +
  geom_point()

情节

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