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

Combining rows in a matrix
Least Square Means problems in R
Conditional variable in lmer package for R?
How can I find specific variables have the same names for all countries in original dataset and select them for another new dataset?
R Shiny - Load parameters and data before calling the app
Using “global” variables in function given to apply. We could see them, but could not assign values
Automatic conversion from integer to numeric during integer calculation in R?
Select range ID with the highest score using foverlaps in R
Change border color for gframe widget
double nesting with tidyverse and purrr
Removing date part from timestamp in R
Merge two data.tables where all rows in dt2 are combined with each row in dt1
Extract non intersected element from a string vector in R [closed]
How to divide many columns? [duplicate]
Scraping text with rvest
How to turn several columns into a column of type list in r?

Categories

HOME
nexus
exception
jquery-ui-datepicker
gruntjs
xslt
llvm
phone
icalcreator
torrent
java-9
aes
xilinx
ssdt
directed-acyclic-graphs
teamspeak
android-volley
etw
svelte
rpc
django-forms
idl-programming-language
django-allauth
locality-sensitive-hash
datetime-format
opencvsharp
refresh
zend-db
virtual-dom
dozer
calculated-columns
hiddenfield
datatables-1.10
winbugs
google-analytics-v4
android-asynctask
photo
safety-critical
pebble-sdk
joi
sdwebimage
maven-replacer-plugin
google-inbox
blogdown
netldap
bingbot
merge-module
pry
jasmine-ajax
sccm
xwiki
mediawiki-templates
goaccess
glassfish-4.1.1
sumologic
qmail
np
watin
interstage
hardware-id
urlconnection
c++98
dllregistration
geronimo
repository-pattern
graphing
yii-extensions
short-circuiting
flex4
http-content-length
barcode4j
cam-pdf
multiple-databases
cordova-plugin-file
demorgans-law
servicestack-razor
ng-map
right-join
user-data
libclang
xcode7.1.1
gnu-coreutils
tiddlywiki5
channels
negative-number
sap-smp
websocket++
activity-lifecycle
gpath
print-spooler-api
dual-monitor
elasticlinq
twist
qac
azcopy
c4
array-multisort
dreamspark
befunge
bankers-algorithm
smartcard-reader
mongodb-hadoop
nextgen-gallery
pycassa
shorthand
sketchflow
webtop
aspect
mcms
gnu-toolchain
redcar
eclipse-ecf
webrick
firefox3.6
long-lines
database-installation
brownfield

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