简体   繁体   English

R-如何绘制多个叠加图/散点图?

[英]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. 我正在研究一个机器学习问题,试图将使用两种不同的ML方法的预测与原始测试值进行比较,并查看它们的比较结果,并试图将结果可视化。 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. 我已经完成了2个单独的散点图(在测试值和预测值之间),但是我想查看这些值之间的差异。 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. 因此,我尝试将数据细分为使用第一个ML算法的100个ytest值,100个预测值(ymle_predict)和使用第二个ML算法的100个预测值(ymap_predict)。

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. 我希望我的x轴代表每个数据点,而y轴代表该数据点的3个不同值(ytest,ymle_predict和ymap_predict)。

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. 其中值是按顺序排列的,因此ymle_predict中的第二个元素代表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 : 但是,这里的时间不是x轴,而是一个计数器变量,例如:

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: 使用ggplot和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()

情节

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

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