1

I have a set of data, and I made a variable to show

data$win <- ifelse((data$distance>data$time), 1, 0)

Now I want to make a ggplot that had a legend and where if the points are 1 they show as green and if they are 0 the points show as red

ggplot(data, aes(x=time, y=distance, col=factor(win))) + geom_point()

Here it shows everything I want, the legend and the plotted points, but how do I get my dots to be green and red? right now they are blue and orange

1
  • Check out the help for ?scale_color_manual, which is the most common way to specify specific colors for specific values. Commented Mar 10, 2022 at 23:51

3 Answers 3

1

In your case, the following code should also work

ggplot(data, aes(x=time, y=distance, col=distance>time)) + geom_point()
Sign up to request clarification or add additional context in comments.

Comments

0

As Jon Spring pointed out, scale_colour_manual() is the way to do this. Here is an example:

library(ggplot2)
library(dplyr)

mtcars %>%
  mutate(win = ifelse(cyl <= 4, 1, 0)) %>%
  ggplot(aes(x = wt, y = mpg, col = factor(win))) +
  geom_point() +
  scale_colour_manual(values = c("1" = "green",
                                 "0" = "red"))

Created on 2022-03-11 by the reprex package (v2.0.1)

Comments

0

Your question piqued my interest because of how the date is defined. In the example below, data defined as hours-minutes-seconds is transformed to minutes and then compared to distance.

Sample code:

library(lubridate)
  library()ggplot2

    data$Time = hour(data$time)*60 + minute(data$time) # convert time to minutes
    
    data$win <- ifelse((data$distance>data$Time), 1, 0)
    data$win<-factor(data$win, levels=c("1","0"))
    
    ggplot(data, aes(x=Time, y=distance, col=win)) + 
      geom_point()+
      labs(x="Minutes", y="Meter", color="Effect")+
      scale_colour_manual(values = c("1" = "green","0" = "red"))+
      theme_minimal()

Plot:

enter image description here

Sample data:

data<-structure(list(time = structure(c(34, 334, 634, 934, 1234, 1535, 
1834, 2134, 2434, 2734, 3034, 3334, 3634, 3935, 4234, 4534, 4834, 
5135, 5434, 5735, 6034, 6335, 6634, 6935, 7235, 7534, 7834, 8134, 
8434, 8734, 9034, 9334, 9636, 9934, 10235, 10534, 10834, 11136, 
11434, 11734, 12034, 12334, 12635, 12934, 13236, 13534, 13835, 
14134, 14435, 14735, 15035, 15334, 15635, 15934, 16234, 16535, 
16834, 17134, 17435, 17734, 18035, 18334, 18635, 18935, 19234, 
19535, 19834, 20134, 20435, 20734, 21035, 21335, 21635, 21935, 
22235, 22534, 22834, 23134, 23434, 23735, 24036, 24334, 24635, 
24934, 25235, 25535, 25834, 26134, 26434, 26735, 27034, 27335, 
27635, 27935, 28234, 28534, 28835, 29134, 29434, 29734, 30034, 
30335, 30634, 30934, 31235, 31534, 31834, 32135, 32434, 32735, 
33035, 33335, 33634, 33934, 34236, 34535, 34835, 35134, 35434, 
35735, 36034, 36334, 36634, 36934, 37234, 37536, 37836, 38134, 
38434, 38734, 39034, 39335, 39634, 39934, 40234, 40535, 40834, 
41135, 41436, 41734, 42034, 42335, 42634, 42934, 43235, 43534, 
43835, 44134, 44434, 44735, 45034, 45335, 45634, 45935, 46234, 
46534, 46834, 47134, 47434, 47735, 48034, 48335, 48634, 48935, 
49235, 49534, 49834, 50135, 50434, 50735, 51034, 51334, 51635, 
51934, 52234, 52535, 52835, 53134, 53435, 53734, 54034, 54334, 
54635, 54935, 55237, 55536, 55839, 56136, 56436, 56735, 57034, 
57334, 57635, 57934, 58235, 58534, 58838, 59135, 59437, 59734, 
60034, 60334, 60634, 60934, 61235, 61534, 61835, 62134, 62435, 
62734, 63035, 63335, 63635, 63934, 64234, 64534, 64834, 65134, 
65434, 65734, 66035, 66334, 66634, 66934, 67235, 67534, 67834, 
68133, 68434, 68734, 69034, 69334, 69635, 69934, 70235, 70534, 
70834, 71134, 71434, 71735, 72034, 72334, 72634, 72934, 73234, 
73535, 73834, 74134, 74435, 74734, 75034, 75335, 75634, 75934, 
76234, 76534, 76834, 77134, 77434, 77734, 78035, 78335, 78634, 
78934, 79235, 79534, 79834, 80135, 80434, 80735, 81034, 81334, 
81634, 81934, 82233, 82534, 82834, 83134, 83434, 83734, 84034, 
84334, 84634, 84935, 85234, 85534, 85834, 86135), class = c("hms", 
"difftime"), units = "secs"), distance = c(21.489, 21.493, 21.669, 
21.914, 21.92, 21.761, 21.708, 21.526, 21.718, 21.841, 22.055, 
22.122, 22.125, 22.085, 22.001, 21.894, 21.828, 21.772, 21.754, 
21.763, 21.77, 21.717, 21.671, 21.622, 21.529, 21.456, 21.506, 
21.469, 21.424, 21.42, 21.268, 21.138, 21.153, 21.153, 21.12, 
21.101, 21.072, 21.061, 21.251, 21.302, 21.374, 21.344, 21.302, 
21.311, 21.407, 21.467, 21.506, 21.483, 21.457, 21.468, 21.326, 
21.291, 21.229, 21.106, 21.12, 21.196, 21.471, 21.699, 21.823, 
21.999, 22.26, 21.992, 21.696, 21.921, 21.762, 21.551, 21.506, 
21.577, 21.682, 21.898, 22.041, 22.143, 22.236, 22.462, 22.766, 
22.87, 23.056, 23.287, 23.499, 23.614, 23.849, 24.081, 24.274, 
24.588, 24.746, 24.737, 24.763, 25.002, 25.046, 25.01, 25.027, 
25.051, 25.051, 25.072, 25.048, 25.033, 24.982, 24.954, 24.856, 
24.824, 24.87, 24.835, 24.734, 24.774, 24.734, 24.608, 24.534, 
24.536, 24.458, 24.395, 24.29, 24.337, 24.392, 24.319, 24.22, 
24.115, 24.108, 24.053, 23.926, 23.834, 23.827, 23.798, 23.702, 
23.619, 23.509, 23.564, 23.727, 23.657, 23.675, 23.694, 23.572, 
23.62, 23.702, 23.951, 23.902, 23.972, 23.992, 23.971, 23.976, 
23.924, 23.877, 23.83, 23.802, 23.766, 23.689, 23.619, 23.541, 
23.515, 23.384, 23.118, 22.919, 22.876, 22.904, 22.816, 22.719, 
22.653, 22.588, 22.493, 22.518, 22.615, 22.609, 22.59, 22.418, 
22.262, 22.34, 22.33, 22.314, 22.379, 22.415, 22.371, 22.242, 
22.215, 22.308, 22.466, 22.411, 22.366, 22.533, 22.762, 22.863, 
22.903, 22.915, 22.84, 23.138, 23.137, 23.391, 23.614, 23.898, 
24.18, 24.434, 24.603, 24.965, 25.348, 25.791, 26.128, 26.674, 
26.914, 26.892, 27.068, 27.413, 27.682, 27.996, 28.39, 28.621, 
28.876, 28.808, 29.034, 29.339, 29.493, 29.553, 29.744, 29.994, 
30.23, 30.087, 30.098, 30.109, 30.109, 30.156, 30.085, 30.226, 
30.337, 30.325, 30.518, 30.629, 30.677, 30.854, 30.867, 30.818, 
30.89, 30.863, 30.917, 31.255, 31.238, 31.222, 31.144, 31.132, 
31.035, 31.081, 31.058, 30.884, 30.844, 30.715, 30.657, 30.521, 
30.383, 30.114, 29.903, 29.595, 29.336, 29.104, 28.922, 28.618, 
28.275, 28.108, 27.862, 27.749, 27.617, 27.387, 27.151, 26.963, 
26.687, 26.583, 26.35, 26.072, 25.905, 25.738, 25.52, 24.979, 
24.749, 24.601, 24.403, 24.204, 24.071, 24.048, 23.963, 23.677, 
23.524, 23.463, 23.411, 23.273, 23.097, 23, 22.949, 22.854, 22.805, 
22.815, 22.761, 22.699, 22.594), win = structure(c(1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", 
"0"), class = "factor"), Time = c(0, 5, 10, 15, 20, 25, 30, 35, 
40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 
115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 
180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 
245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300, 305, 
310, 315, 320, 325, 330, 335, 340, 345, 350, 355, 360, 365, 370, 
375, 380, 385, 390, 395, 400, 405, 410, 415, 420, 425, 430, 435, 
440, 445, 450, 455, 460, 465, 470, 475, 480, 485, 490, 495, 500, 
505, 510, 515, 520, 525, 530, 535, 540, 545, 550, 555, 560, 565, 
570, 575, 580, 585, 590, 595, 600, 605, 610, 615, 620, 625, 630, 
635, 640, 645, 650, 655, 660, 665, 670, 675, 680, 685, 690, 695, 
700, 705, 710, 715, 720, 725, 730, 735, 740, 745, 750, 755, 760, 
765, 770, 775, 780, 785, 790, 795, 800, 805, 810, 815, 820, 825, 
830, 835, 840, 845, 850, 855, 860, 865, 870, 875, 880, 885, 890, 
895, 900, 905, 910, 915, 920, 925, 930, 935, 940, 945, 950, 955, 
960, 965, 970, 975, 980, 985, 990, 995, 1000, 1005, 1010, 1015, 
1020, 1025, 1030, 1035, 1040, 1045, 1050, 1055, 1060, 1065, 1070, 
1075, 1080, 1085, 1090, 1095, 1100, 1105, 1110, 1115, 1120, 1125, 
1130, 1135, 1140, 1145, 1150, 1155, 1160, 1165, 1170, 1175, 1180, 
1185, 1190, 1195, 1200, 1205, 1210, 1215, 1220, 1225, 1230, 1235, 
1240, 1245, 1250, 1255, 1260, 1265, 1270, 1275, 1280, 1285, 1290, 
1295, 1300, 1305, 1310, 1315, 1320, 1325, 1330, 1335, 1340, 1345, 
1350, 1355, 1360, 1365, 1370, 1375, 1380, 1385, 1390, 1395, 1400, 
1405, 1410, 1415, 1420, 1425, 1430, 1435)), row.names = c(NA, 
-288L), spec = structure(list(cols = list(time = structure(list(
    format = ""), class = c("collector_time", "collector")), 
    distance = structure(list(), class = c("collector_double", 
    "collector")), win = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.