r


R adding layers to a ggplot plot with vectors of different length


I research this problem, and all tricks fail here. I am trying to build ROC curve in ggplot. Different models of machine learning return a different length of observations vectors. My goal is to gather them in one plot, color and put legend.
It is easy when all your data has the same X axis length - (number of observations). All x and y are in a range of [0 to 1]. A problem is a different number of observations. I'd like to avoid interpolating and coercing vectors to the same length because each observation carries important information.
So far I figured out that the best approach would be put new layers of lines on the same plot.
Maybe other types of plot? or specialized plots from other packages.
Please help. Thanks!
ggplot(vector_1, aes(x, y ) ) + geom_line() + ???? new layers ???
My goal is to obtain a plot like this but with more lines:
Here is vector1 - 34 observations:
vector_1_x:
0
0.02325581
0.02325581
0.04651163
0.04651163
0.06976744
0.09302326
0.11627907
0.13953488
0.1627907
0.18604651
0.20930233
0.23255814
0.25581395
0.27906977
0.30232558
0.3255814
0.37209302
0.39534884
0.41860465
0.44186047
0.46511628
0.48837209
0.51162791
0.53488372
0.62790698
0.65116279
0.74418605
0.76744186
0.86046512
0.90697674
0.93023256
0.97674419
1
vector_1_y:
0
0
0.1666667
0.1666667
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.5
0.5
0.5
0.6666667
0.6666667
0.6666667
0.6666667
0.6666667
0.6666667
0.6666667
0.8333333
0.8333333
0.8333333
1
1
1
1
1
Here is vector2 - 50 observations:
vector_2_x:
0
0
0.02325581
0.04651163
0.04651163
0.06976744
0.09302326
0.11627907
0.13953488
0.1627907
0.18604651
0.20930233
0.23255814
0.25581395
0.25581395
0.27906977
0.30232558
0.3255814
0.34883721
0.37209302
0.39534884
0.39534884
0.41860465
0.44186047
0.46511628
0.48837209
0.51162791
0.51162791
0.53488372
0.55813953
0.58139535
0.60465116
0.62790698
0.65116279
0.65116279
0.6744186
0.69767442
0.72093023
0.74418605
0.76744186
0.79069767
0.81395349
0.8372093
0.86046512
0.88372093
0.90697674
0.93023256
0.95348837
0.97674419
1
vector_2_x:
0
0.1666667
0.1666667
0.1666667
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.3333333
0.5
0.5
0.5
0.5
0.5
0.5
0.5
0.6666667
0.6666667
0.6666667
0.6666667
0.6666667
0.6666667
0.8333333
0.8333333
0.8333333
0.8333333
0.8333333
0.8333333
0.8333333
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Put vector_1 and vector_2 into their own data frames like so:
df1 <- data.frame(x = vector_1_x, y = vector_1_y)
df2 <- data.frame(x = vector_2_x, y = vector_2_y)
Then you can specify a different data.frame for each ggplot layer:
ggplot() +
geom_line(data = df1, aes(vector_1_x, vector_1_y), colour = "red") +
geom_line(data = df2, aes(vector_2_x, vector_2_y), colour = "green")
Update
What I wrote above originally is okay for a few lines but it will be much cleaner to put all the vectors in their own data.frame with an indicator variable stating which line it belongs to. This would look something like:
x y vector
1 1 "line1"
2 3 "line1"
3 6 "line1"
... ... ...
1 8 "line2"
2 6 "line2"
3 2 "line2"
... ... ...
Then in you can have a single ggplot layer:
ggplot(data = df) + geom_line(aes(x, y, colour = vector))

Related Links

How can i split a string based on word boundary in R? [duplicate]
Error in Knn, 'train' and 'class' have different lengths - R Code
How could a matrix hold different data types when coercing from a list?
Why is webshot not working with leaflets in R shiny?
How do I split a list containing data frames into the individual data frames?
No neighbours in sample using spatcounts package in R - Error: NA/NaN/Inf in foreign function call (arg 1)
R weird data frame subset formula vs. no formula
Split a whole column of a data frame and keep only the first part
Apply an element-wise function to data.table columns
Exclude data in R based on minimum sample size
How to make bar plot with dplyr in shiny dashboard
Elementwise vector multiplication between all rows of two data frames / matrices
R: network plot
Loop to create dummy variables
Splitting strings and stacking them in one column
Linux R server (Shiny): Install Windows binaries

Categories

HOME
osx
wcf
video
notifications
hibernate
app-config
vtk
ibm-content-navigator
vsts-release
unicode
nw.js
phpdoc
peoplesoft
chisel
codemirror
sqf
java-9
mpeg-2
metrics
liferay-6
xilinx
ghostscript
julius
prepared-statement
django-views
siebel
converse.js
leaflet.draw
codeship
openjdk
anonymous-class
vertices
bitmapfactory
bond
android-asynctask
photo
netflow
pebble-sdk
multiple-return-values
biom
rjava
interpretation
geoxml3
gpuimage
motionevent
siege
variadic-macro
sage-erp
watin
opencv3.1
hardware-id
email-ext
android-keypad
sliding-window
ergoemacs
xposed-framework
jasmine-spec-reporter
realurl
substr
thread-dump
ng-storage
has-many
merchant-account
asp-net-mvc-1
rackspace-cloudfiles
web-inf
pocket
fann
cilk
kaminari
zorba
mysql-connect
numerical-integration
project-siena
palm-os
angularjs-model
qstring
moxy
quickbuild
autosave
tanuki
datagridviewrow
title-case
editorfor
optgroup
swingbuilder
hud
efi
directory-listing
jar-signing
childbrowser
trending
elapsedtime
atomikos
detours
rfc2616
key-generator
boehm-gc
novell-idm
future-proof
fingerprinting
instant-message

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App