I am trying to plot the following data using ggplot
The data I have looks like:
# A tibble: 200 x 4
svm randomForest Petal.Length Sepal.Width
<fct> <fct> <dbl> <dbl>
1 0 0 3 2
2 0 0 3.02 2.01
3 0 0 3.04 2.02
4 0 0 3.06 2.03
5 0 0 3.08 2.04
This table should plot all the Petal.Length
and Sepal.Width
points using geom_contour
and colour them according to the column svm
.
I also have the iris
dataset, called df
where I plot the geom_points
.
# A tibble: 100 x 5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<dbl> <dbl> <dbl> <dbl> <int>
1 7 3.2 4.7 1.4 0
2 6.4 3.2 4.5 1.5 0
3 6.9 3.1 4.9 1.5 0
4 5.5 2.3 4 1.3 0
I try to plot the both data using:
ggplot() +
geom_point(aes(x = Petal.Length, y = Sepal.Width, colour = svm),
data = plt_Data) +
geom_contour(aes(x = Petal.Length, y = Sepal.Width, z = as.numeric(as.character(svm))),
bins = 2,
data = plt_Data, size = 0.01) +
geom_point(aes(x = Petal.Length, y = Sepal.Width, color = factor(Species)),
size = 3,
data = df) +
geom_point(aes(x = Petal.Length, y = Sepal.Width),
size = 3,
shape = 1,
data = df)
But I cannot seem to get the geom_contour
working. I am hoping the plot will look something very loosly - similar to the following.
Where in my problem I have a 2 class-classification problem (as opposed to the image which is a multi class problem).
Data:
df <- structure(list(Sepal.Length = c(7, 6.4, 6.9, 5.5, 6.5, 5.7, 6.3,
4.9, 6.6, 5.2, 5, 5.9, 6, 6.1, 5.6, 6.7, 5.6, 5.8, 6.2, 5.6,
5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7, 6, 5.7, 5.5, 5.5, 5.8,
6, 5.4, 6, 6.7, 6.3, 5.6, 5.5, 5.5, 6.1, 5.8, 5, 5.6, 5.7, 5.7,
6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3, 6.5, 7.6, 4.9, 7.3, 6.7, 7.2,
6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5, 7.7, 7.7, 6, 6.9, 5.6, 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4` = 1L, `5` = 1L, `6` = 1L, `7` = 1L, `8` = 1L, `9` = 1L, `10` = 1L,
`11` = 1L, `12` = 1L, `13` = 1L, `14` = 1L, `15` = 1L, `16` = 1L,
`17` = 1L, `18` = 1L, `19` = 1L, `20` = 1L, `21` = 1L, `22` = 1L,
`23` = 1L, `24` = 1L, `25` = 1L, `26` = 1L, `27` = 1L, `28` = 1L,
`29` = 1L, `30` = 1L, `31` = 1L, `32` = 1L, `33` = 1L, `34` = 1L,
`35` = 1L, `36` = 1L, `37` = 1L, `38` = 1L, `39` = 1L, `40` = 1L,
`41` = 1L, `42` = 1L, `43` = 1L, `44` = 1L, `45` = 1L, `46` = 1L,
`47` = 1L, `48` = 1L, `49` = 1L, `50` = 1L, `51` = 1L, `52` = 1L,
`53` = 1L, `54` = 1L, `55` = 1L, `56` = 1L, `57` = 1L, `58` = 1L,
`59` = 1L, `60` = 1L, `61` = 1L, `62` = 1L, `63` = 1L, `64` = 1L,
`65` = 1L, `66` = 1L, `67` = 1L, `68` = 1L, `69` = 1L, `70` = 1L,
`71` = 1L, `72` = 1L, `73` = 1L, `74` = 1L, `75` = 1L, `76` = 1L,
`77` = 1L, `78` = 1L, `79` = 1L, `80` = 1L, `81` = 1L, `82` = 1L,
`83` = 1L, `84` = 1L, `85` = 1L, `86` = 1L, `87` = 1L, `88` = 1L,
`89` = 1L, `90` = 1L, `91` = 1L, `92` = 1L, `93` = 1L, `94` = 1L,
`95` = 1L, `96` = 1L, `97` = 2L, `98` = 2L, `99` = 2L, `100` = 2L,
`101` = 2L, `102` = 2L, `103` = 2L, `104` = 2L, `105` = 2L, `106` = 2L,
`107` = 2L, `108` = 2L, `109` = 2L, `110` = 2L, `111` = 2L, `112` = 2L,
`113` = 2L, `114` = 2L, `115` = 2L, `116` = 2L, `117` = 2L, `118` = 2L,
`119` = 2L, `120` = 2L, `121` = 2L, `122` = 2L, `123` = 2L, `124` = 2L,
`125` = 2L, `126` = 2L, `127` = 2L, `128` = 2L, `129` = 2L, `130` = 2L,
`131` = 2L, `132` = 2L, `133` = 2L, `134` = 2L, `135` = 2L, `136` = 2L,
`137` = 2L, `138` = 2L, `139` = 2L, `140` = 2L, `141` = 2L, `142` = 2L,
`143` = 2L, `144` = 2L, `145` = 2L, `146` = 2L, `147` = 2L, `148` = 2L,
`149` = 2L, `150` = 2L, `151` = 2L, `152` = 2L, `153` = 2L, `154` = 2L,
`155` = 2L, `156` = 2L, `157` = 2L, `158` = 2L, `159` = 2L, `160` = 2L,
`161` = 2L, `162` = 2L, `163` = 2L, `164` = 2L, `165` = 2L, `166` = 2L,
`167` = 2L, `168` = 2L, `169` = 2L, `170` = 2L, `171` = 2L, `172` = 2L,
`173` = 2L, `174` = 2L, `175` = 2L, `176` = 2L, `177` = 2L, `178` = 2L,
`179` = 2L, `180` = 2L, `181` = 2L, `182` = 2L, `183` = 2L, `184` = 2L,
`185` = 2L, `186` = 2L, `187` = 2L, `188` = 2L, `189` = 2L, `190` = 2L,
`191` = 2L, `192` = 2L, `193` = 2L, `194` = 2L, `195` = 2L, `196` = 2L,
`197` = 2L, `198` = 2L, `199` = 2L, `200` = 2L), .Label = c("0",
"1"), class = "factor"), randomForest = structure(c(`1` = 1L,
`2` = 1L, `3` = 1L, `4` = 1L, `5` = 1L, `6` = 1L, `7` = 1L, `8` = 1L,
`9` = 1L, `10` = 1L, `11` = 1L, `12` = 1L, `13` = 1L, `14` = 1L,
`15` = 1L, `16` = 1L, `17` = 1L, `18` = 1L, `19` = 1L, `20` = 1L,
`21` = 1L, `22` = 1L, `23` = 1L, `24` = 1L, `25` = 1L, `26` = 1L,
`27` = 1L, `28` = 1L, `29` = 1L, `30` = 1L, `31` = 1L, `32` = 1L,
`33` = 1L, `34` = 1L, `35` = 1L, `36` = 1L, `37` = 1L, `38` = 1L,
`39` = 1L, `40` = 1L, `41` = 1L, `42` = 1L, `43` = 1L, `44` = 1L,
`45` = 1L, `46` = 1L, `47` = 1L, `48` = 1L, `49` = 1L, `50` = 1L,
`51` = 1L, `52` = 1L, `53` = 1L, `54` = 1L, `55` = 1L, `56` = 1L,
`57` = 1L, `58` = 1L, `59` = 1L, `60` = 1L, `61` = 1L, `62` = 1L,
`63` = 1L, `64` = 1L, `65` = 1L, `66` = 1L, `67` = 1L, `68` = 1L,
`69` = 1L, `70` = 1L, `71` = 1L, `72` = 1L, `73` = 1L, `74` = 1L,
`75` = 1L, `76` = 1L, `77` = 1L, `78` = 1L, `79` = 1L, `80` = 1L,
`81` = 1L, `82` = 1L, `83` = 1L, `84` = 1L, `85` = 1L, `86` = 1L,
`87` = 1L, `88` = 1L, `89` = 1L, `90` = 1L, `91` = 2L, `92` = 2L,
`93` = 2L, `94` = 2L, `95` = 1L, `96` = 2L, `97` = 2L, `98` = 2L,
`99` = 2L, `100` = 2L, `101` = 2L, `102` = 2L, `103` = 2L, `104` = 2L,
`105` = 2L, `106` = 2L, `107` = 2L, `108` = 2L, `109` = 2L, `110` = 2L,
`111` = 2L, `112` = 2L, `113` = 2L, `114` = 2L, `115` = 2L, `116` = 2L,
`117` = 2L, `118` = 2L, `119` = 2L, `120` = 2L, `121` = 2L, `122` = 2L,
`123` = 2L, `124` = 2L, `125` = 2L, `126` = 2L, `127` = 2L, `128` = 2L,
`129` = 2L, `130` = 2L, `131` = 2L, `132` = 2L, `133` = 2L, `134` = 2L,
`135` = 2L, `136` = 2L, `137` = 2L, `138` = 2L, `139` = 2L, `140` = 2L,
`141` = 2L, `142` = 2L, `143` = 2L, `144` = 2L, `145` = 2L, `146` = 2L,
`147` = 2L, `148` = 2L, `149` = 2L, `150` = 2L, `151` = 2L, `152` = 2L,
`153` = 2L, `154` = 2L, `155` = 2L, `156` = 2L, `157` = 2L, `158` = 2L,
`159` = 2L, `160` = 2L, `161` = 2L, `162` = 2L, `163` = 2L, `164` = 2L,
`165` = 2L, `166` = 2L, `167` = 2L, `168` = 2L, `169` = 2L, `170` = 2L,
`171` = 2L, `172` = 2L, `173` = 2L, `174` = 2L, `175` = 2L, `176` = 2L,
`177` = 2L, `178` = 2L, `179` = 2L, `180` = 2L, `181` = 2L, `182` = 2L,
`183` = 2L, `184` = 2L, `185` = 2L, `186` = 2L, `187` = 2L, `188` = 2L,
`189` = 2L, `190` = 2L, `191` = 2L, `192` = 2L, `193` = 2L, `194` = 2L,
`195` = 2L, `196` = 2L, `197` = 2L, `198` = 2L, `199` = 2L, `200` = 2L
), .Label = c("0", "1"), class = "factor"), Petal.Length = c(3,
3.01959798994975, 3.0391959798995, 3.05879396984925, 3.07839195979899,
3.09798994974874, 3.11758793969849, 3.13718592964824, 3.15678391959799,
3.17638190954774, 3.19597989949749, 3.21557788944724, 3.23517587939698,
3.25477386934673, 3.27437185929648, 3.29396984924623, 3.31356783919598,
3.33316582914573, 3.35276381909548, 3.37236180904523, 3.39195979899498,
3.41155778894472, 3.43115577889447, 3.45075376884422, 3.47035175879397,
3.48994974874372, 3.50954773869347, 3.52914572864322, 3.54874371859296,
3.56834170854271, 3.58793969849246, 3.60753768844221, 3.62713567839196,
3.64673366834171, 3.66633165829146, 3.68592964824121, 3.70552763819095,
3.7251256281407, 3.74472361809045, 3.7643216080402, 3.78391959798995,
3.8035175879397, 3.82311557788945, 3.8427135678392, 3.86231155778894,
3.88190954773869, 3.90150753768844, 3.92110552763819, 3.94070351758794,
3.96030150753769, 3.97989949748744, 3.99949748743719, 4.01909547738693,
4.03869346733668, 4.05829145728643, 4.07788944723618, 4.09748743718593,
4.11708542713568, 4.13668341708543, 4.15628140703518, 4.17587939698493,
4.19547738693467, 4.21507537688442, 4.23467336683417, 4.25427135678392,
4.27386934673367, 4.29346733668342, 4.31306532663317, 4.33266331658291,
4.35226130653266, 4.37185929648241, 4.39145728643216, 4.41105527638191,
4.43065326633166, 4.45025125628141, 4.46984924623116, 4.4894472361809,
4.50904522613065, 4.5286432160804, 4.54824120603015, 4.5678391959799,
4.58743718592965, 4.6070351758794, 4.62663316582915, 4.64623115577889,
4.66582914572864, 4.68542713567839, 4.70502512562814, 4.72462311557789,
4.74422110552764, 4.76381909547739, 4.78341708542714, 4.80301507537688,
4.82261306532663, 4.84221105527638, 4.86180904522613, 4.88140703517588,
4.90100502512563, 4.92060301507538, 4.94020100502513, 4.95979899497487,
4.97939698492462, 4.99899497487437, 5.01859296482412, 5.03819095477387,
5.05778894472362, 5.07738693467337, 5.09698492462312, 5.11658291457286,
5.13618090452261, 5.15577889447236, 5.17537688442211, 5.19497487437186,
5.21457286432161, 5.23417085427136, 5.25376884422111, 5.27336683417085,
5.2929648241206, 5.31256281407035, 5.3321608040201, 5.35175879396985,
5.3713567839196, 5.39095477386935, 5.4105527638191, 5.43015075376884,
5.44974874371859, 5.46934673366834, 5.48894472361809, 5.50854271356784,
5.52814070351759, 5.54773869346734, 5.56733668341709, 5.58693467336683,
5.60653266331658, 5.62613065326633, 5.64572864321608, 5.66532663316583,
5.68492462311558, 5.70452261306533, 5.72412060301508, 5.74371859296482,
5.76331658291457, 5.78291457286432, 5.80251256281407, 5.82211055276382,
5.84170854271357, 5.86130653266332, 5.88090452261306, 5.90050251256281,
5.92010050251256, 5.93969849246231, 5.95929648241206, 5.97889447236181,
5.99849246231156, 6.01809045226131, 6.03768844221106, 6.0572864321608,
6.07688442211055, 6.0964824120603, 6.11608040201005, 6.1356783919598,
6.15527638190955, 6.1748743718593, 6.19447236180905, 6.21407035175879,
6.23366834170854, 6.25326633165829, 6.27286432160804, 6.29246231155779,
6.31206030150754, 6.33165829145729, 6.35125628140704, 6.37085427135678,
6.39045226130653, 6.41005025125628, 6.42964824120603, 6.44924623115578,
6.46884422110553, 6.48844221105528, 6.50804020100503, 6.52763819095477,
6.54723618090452, 6.56683417085427, 6.58643216080402, 6.60603015075377,
6.62562814070352, 6.64522613065327, 6.66482412060301, 6.68442211055276,
6.70402010050251, 6.72361809045226, 6.74321608040201, 6.76281407035176,
6.78241206030151, 6.80201005025126, 6.821608040201, 6.84120603015075,
6.8608040201005, 6.88040201005025, 6.9), Sepal.Width = c(2, 2.00904522613065,
2.01809045226131, 2.02713567839196, 2.03618090452261, 2.04522613065327,
2.05427135678392, 2.06331658291457, 2.07236180904523, 2.08140703517588,
2.09045226130653, 2.09949748743719, 2.10854271356784, 2.11758793969849,
2.12663316582915, 2.1356783919598, 2.14472361809045, 2.15376884422111,
2.16281407035176, 2.17185929648241, 2.18090452261307, 2.18994974874372,
2.19899497487437, 2.20804020100503, 2.21708542713568, 2.22613065326633,
2.23517587939698, 2.24422110552764, 2.25326633165829, 2.26231155778894,
2.2713567839196, 2.28040201005025, 2.2894472361809, 2.29849246231156,
2.30753768844221, 2.31658291457286, 2.32562814070352, 2.33467336683417,
2.34371859296482, 2.35276381909548, 2.36180904522613, 2.37085427135678,
2.37989949748744, 2.38894472361809, 2.39798994974874, 2.4070351758794,
2.41608040201005, 2.4251256281407, 2.43417085427136, 2.44321608040201,
2.45226130653266, 2.46130653266332, 2.47035175879397, 2.47939698492462,
2.48844221105528, 2.49748743718593, 2.50653266331658, 2.51557788944724,
2.52462311557789, 2.53366834170854, 2.5427135678392, 2.55175879396985,
2.5608040201005, 2.56984924623116, 2.57889447236181, 2.58793969849246,
2.59698492462312, 2.60603015075377, 2.61507537688442, 2.62412060301508,
2.63316582914573, 2.64221105527638, 2.65125628140704, 2.66030150753769,
2.66934673366834, 2.67839195979899, 2.68743718592965, 2.6964824120603,
2.70552763819095, 2.71457286432161, 2.72361809045226, 2.73266331658291,
2.74170854271357, 2.75075376884422, 2.75979899497487, 2.76884422110553,
2.77788944723618, 2.78693467336683, 2.79597989949749, 2.80502512562814,
2.81407035175879, 2.82311557788945, 2.8321608040201, 2.84120603015075,
2.85025125628141, 2.85929648241206, 2.86834170854271, 2.87738693467337,
2.88643216080402, 2.89547738693467, 2.90452261306533, 2.91356783919598,
2.92261306532663, 2.93165829145729, 2.94070351758794, 2.94974874371859,
2.95879396984925, 2.9678391959799, 2.97688442211055, 2.98592964824121,
2.99497487437186, 3.00402010050251, 3.01306532663317, 3.02211055276382,
3.03115577889447, 3.04020100502513, 3.04924623115578, 3.05829145728643,
3.06733668341709, 3.07638190954774, 3.08542713567839, 3.09447236180905,
3.1035175879397, 3.11256281407035, 3.121608040201, 3.13065326633166,
3.13969849246231, 3.14874371859296, 3.15778894472362, 3.16683417085427,
3.17587939698492, 3.18492462311558, 3.19396984924623, 3.20301507537688,
3.21206030150754, 3.22110552763819, 3.23015075376884, 3.2391959798995,
3.24824120603015, 3.2572864321608, 3.26633165829146, 3.27537688442211,
3.28442211055276, 3.29346733668342, 3.30251256281407, 3.31155778894472,
3.32060301507538, 3.32964824120603, 3.33869346733668, 3.34773869346734,
3.35678391959799, 3.36582914572864, 3.3748743718593, 3.38391959798995,
3.3929648241206, 3.40201005025126, 3.41105527638191, 3.42010050251256,
3.42914572864322, 3.43819095477387, 3.44723618090452, 3.45628140703518,
3.46532663316583, 3.47437185929648, 3.48341708542714, 3.49246231155779,
3.50150753768844, 3.5105527638191, 3.51959798994975, 3.5286432160804,
3.53768844221106, 3.54673366834171, 3.55577889447236, 3.56482412060301,
3.57386934673367, 3.58291457286432, 3.59195979899497, 3.60100502512563,
3.61005025125628, 3.61909547738693, 3.62814070351759, 3.63718592964824,
3.64623115577889, 3.65527638190955, 3.6643216080402, 3.67336683417085,
3.68241206030151, 3.69145728643216, 3.70050251256281, 3.70954773869347,
3.71859296482412, 3.72763819095477, 3.73668341708543, 3.74572864321608,
3.75477386934673, 3.76381909547739, 3.77286432160804, 3.78190954773869,
3.79095477386935, 3.8)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -200L))