1

I am trying to divide my UI values in positive and negative on a scatter plot. With the values of upwelling being positive and the downwelling being negative and represented on the column Event:

structure(list(Dia = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 1, 2, 
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3, 4, 5, 
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 
27, 28, 29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 
18), Mes = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7), Ano = c(2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012), UI = c(18.0647497177124, 51.0355095863342, 47.5402774810791, 
340.893737792969, 247.539836883545, 399.510726928711, 122.39359664917, 
98.836296081543, 35.0952081680298, 50.6278982162476, 87.2319049835205, 
77.6166572570801, 112.672420501709, 2.67568874359131, -392.484573364258, 
618.681500434875, 692.416625976562, 236.788291931152, 51.255313873291, 
71.8802337646484, 345.232437133789, 236.85432434082, -0.00471636652946472, 
-27.9389114379883, 146.920881986618, 341.540222167969, 1208.46826171875, 
454.171142578125, 94.2786865234375, -23.250904083252, 6.60693836212158, 
11.3990550041199, 461.585052490234, 110.249738693237, 282.145706176758, 
30.0083067417145, 922.615142822266, 665.348541259766, 280.90966796875, 
129.148958206177, 97.8483505249023, 151.280666351318, 41.1154479980469, 
462.513458251953, 223.375587463379, 243.899364471436, 91.236083984375, 
-33.4530096054077, 419.289459228516, 216.38060760498, 223.88525390625, 
42.9771499633789, 63.462251663208, 135.543815612793, 103.11067199707, 
-2.97302103042603, 50.1539402008057, 31.7654695510864, 4.74521112442017, 
116.475090026855, -31.0308532714844, 95.1957511901855, -74.0424919128418, 
74.0312874317169, 370.580841064453, 439.345306396484, 288.222801208496, 
245.310737609863, 163.978561401367, -43.6043725013733, 90.422176361084, 
13.5707854032516, 35.8152360916138, -251.285369873047, -396.40592956543, 
27.4398317337036, 119.581299781799, 374.314788818359, 895.551239013672, 
583.116119384766, 83.0863494873047, -162.666477203369, -326.430221557617, 
-219.034694671631, 246.566764831543, 142.276351928711, 167.783622741699, 
116.547183990479, -962.846618652344, -199.737373352051, 581.776489257812, 
127.202213287354, 243.02904510498, 546.869003295898, 322.698364257812, 
416.055419921875, -35.2042512893677, 4.76407432556152, 59.2325496673584, 
109.642734527588, 39.2587299346924, 70.3620586395264, 3161.94677734375, 
1006.70434570312, 2140.01275634766, 2786.77355957031, 1705.16961669922, 
944.830413818359, 655.952301025391, 338.197479248047, 63.7034378051758, 
519.655364990234, 658.758819580078, 443.135345458984, 365.920120239258, 
-533.251229286194, 514.586601257324, 67.87255859375, -295.31233215332, 
175.399646759033, 27.4736328125, -3460.8505859375, -2083.35198974609, 
-1276.77111816406, -203.34831237793, 121.706581115723, -1469.41851806641, 
-224.111042022705, 191.504776000977, 172.109447479248, 26.7028465270996, 
113.872447013855, 115.648100852966, 365.883389472961, 2044.16656494141, 
-32.7207946777344, 24.2001395225525, 1416.10055541992, -49.864501953125, 
953.696166992188, 1353.91192626953, 331.314163208008, 755.130172729492, 
504.707885742188, 1100.87829589844, 410.127822875977, 1059.75042724609, 
1814.80114746094, 732.802597045898, 2145.48461914062, 2912.95367431641, 
210.142471313477, 39.4968557357788, 380.054672241211, 278.151397705078, 
1334.32025146484, 681.097854614258, -69.2400665283203, 567.077270507812, 
624.232055664062, 391.584182739258, 586.699615478516, 1279.32067871094, 
3932.2900390625, 789.148895263672, 474.529556274414, 316.368942260742, 
589.92162322998, 107.363967418671, 3477.34375, 241.996397972107, 
350.299514770508, 3326.77624511719, 2569.17126464844, 4582.50708007812, 
1923.22076416016, -61.1435556411743, -85.2687129974365, 217.463203430176, 
517.184478759766, 172.391616821289, 1056.78869628906, 2622.95007324219, 
2552.87609863281, 1118.66555786133, 1658.28143310547, 1830.66302490234, 
1645.97857666016, 874.851470947266, 1195.69207763672, 771.39143371582, 
1265.49255371094, 1729.88415527344, 4629.59558105469, 3249.35119628906, 
2862.19885253906, 2368.9833984375, 380.631881713867, 276.73908996582, 
5071.85693359375), Date = structure(c(1325376000, 1325462400, 
1325548800, 1325635200, 1325721600, 1325808000, 1325894400, 1325980800, 
1326067200, 1326153600, 1326240000, 1326326400, 1326412800, 1326499200, 
1326585600, 1326672000, 1326758400, 1326844800, 1326931200, 1327017600, 
1327104000, 1327190400, 1327276800, 1327363200, 1327449600, 1327536000, 
1327622400, 1327708800, 1327795200, 1327881600, 1327968000, 1328054400, 
1328140800, 1328227200, 1328313600, 1328400000, 1328486400, 1328572800, 
1328659200, 1328745600, 1328832000, 1328918400, 1329004800, 1329091200, 
1329177600, 1329264000, 1329350400, 1329436800, 1329523200, 1329609600, 
1329696000, 1329782400, 1329868800, 1329955200, 1330041600, 1330128000, 
1330214400, 1330300800, 1330387200, 1330473600, 1330560000, 1330646400, 
1330732800, 1330819200, 1330905600, 1330992000, 1331078400, 1331164800, 
1331251200, 1331337600, 1331424000, 1331510400, 1331596800, 1331683200, 
1331769600, 1331856000, 1331942400, 1332028800, 1332115200, 1332201600, 
1332288000, 1332374400, 1332460800, 1332547200, 1332633600, 1332720000, 
1332806400, 1332892800, 1332979200, 1333065600, 1333152000, 1333238400, 
1333324800, 1333411200, 1333497600, 1333584000, 1333670400, 1333756800, 
1333843200, 1333929600, 1334016000, 1334102400, 1334188800, 1334275200, 
1334361600, 1334448000, 1334534400, 1334620800, 1334707200, 1334793600, 
1334880000, 1334966400, 1335052800, 1335139200, 1335225600, 1335312000, 
1335398400, 1335484800, 1335571200, 1335657600, 1335744000, 1335830400, 
1335916800, 1336003200, 1336089600, 1336176000, 1336262400, 1336348800, 
1336435200, 1336521600, 1336608000, 1336694400, 1336780800, 1336867200, 
1336953600, 1337040000, 1337126400, 1337212800, 1337299200, 1337385600, 
1337472000, 1337558400, 1337644800, 1337731200, 1337817600, 1337904000, 
1337990400, 1338076800, 1338163200, 1338249600, 1338336000, 1338422400, 
1338508800, 1338595200, 1338681600, 1338768000, 1338854400, 1338940800, 
1339027200, 1339113600, 1339200000, 1339286400, 1339372800, 1339459200, 
1339545600, 1339632000, 1339718400, 1339804800, 1339891200, 1339977600, 
1340064000, 1340150400, 1340236800, 1340323200, 1340409600, 1340496000, 
1340582400, 1340668800, 1340755200, 1340841600, 1340928000, 1341014400, 
1341100800, 1341187200, 1341273600, 1341360000, 1341446400, 1341532800, 
1341619200, 1341705600, 1341792000, 1341878400, 1341964800, 1342051200, 
1342137600, 1342224000, 1342310400, 1342396800, 1342483200, 1342569600
), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 
200L), class = "data.frame")

Using ggplot2 the only way I could separate the colors was:

ggplot(dailyUI2018) +
 aes(x = Date, y = UI, colour = Event) +
 geom_line(size = 1L) +
 scale_color_hue() +
 theme_minimal()

and the result was:

enter image description here

But my idea of the graphic was to be similar to this, with the line transition:

enter image description here

I've tried to use xyplot to change the colors but the graphic looks like this:

enter image description here

and the code is:(with no changes in the colors)

xyplot(UI ~ Date,data = dailyUI2018,
       type="l",
       grid=TRUE,
       panel.groups=panel.breakline,
       xlab = NULL,
       ylab = expression(paste("Upwelling Index  ","(",m^3*s^-1*Km^-1,")")))

Thank you!

macs2021
  • 85
  • 1
  • 7
  • Maybe this method will work for you? https://stackoverflow.com/a/35040744/10276092 – M.Viking Feb 17 '21 at 00:46
  • 1
    A different method for changing the color of a line - https://stackoverflow.com/a/37242250/10276092 . Can you include sample data that includes both events? – M.Viking Feb 17 '21 at 00:53

2 Answers2

0

Update with geom_segment

new.x <- seq(from = min(dailyUI2018$Date),
             to = max(dailyUI2018$Date),
             by = "1 days")

new.y <-dailyUI2018$UI

DF <- data.frame(x1 = head(new.x, -1), x2 = tail(new.x, -1) , 
                 y1 = head(new.y, -1), y2 = tail(new.y, -1))

DF$col <- tail(dailyUI2018$Event, -1)=="Upwelling"

ggplot(data=DF, aes(x=x1, y=y1, xend = x2, yend = y2, colour=col)) +
  geom_segment(size = 2)

.......

A different idea - box plots:

ggplot(dailyUI2018, aes(x = Date, y = UI, fill=Event) ) +
    geom_bar(stat='identity')

...

maybe this needs geom_segement... https://ggplot2.tidyverse.org/reference/geom_segment.html

or ggforce::geom_link2 https://ggforce.data-imaginist.com/reference/geom_link.html

,,,

Version 2. This might look good with your real data

ggplot(dailyUI2018) +
    aes(x = Date, y = UI, colour = UI) +
    geom_line(size = 1L) +
    scale_colour_steps(n.break=2, low = "Red", high="Blue")

.......

Version 1

ggplot(dailyUI2018) +
  aes(x = Date, y = UI, colour = UI) +
  geom_line(size = 1L) +
  scale_color_gradient(low="red", high="Blue")

fake data

dailyUI2018<-read_table("
x  Dia   Mes   Ano     UI   Date                   Event    
1     1     1  2012   18.1  2012-01-01 00:00:00   Upwelling
2     2     1  2012   51.0  2012-01-02 00:00:00   Upwelling
3     3     1  2012   47.5  2012-01-03 00:00:00   Upwelling
4     4     1  2012  -341.  2012-01-04 00:00:00   Downwelling
5     5     1  2012  -248.  2012-01-05 00:00:00   Downwelling
6     6     1  2012  -400.  2012-01-06 00:00:00   Downwelling
7     7     1  2012  -122.  2012-01-07 00:00:00   Downwelling
8     8     1  2012   98.8  2012-01-08 00:00:00   Upwelling
9     9     1  2012   35.1  2012-01-09 00:00:00   Upwelling
10    10     1  2012   50.6  2012-01-10 00:00:00   Upwelling")
M.Viking
  • 5,067
  • 4
  • 17
  • 33
  • Hello! Thank you for the answer! I tried using geom_bar but it doesn't fit the data well, same thing with the gradient. I am trying to work with the scale_color_steps, but there's still some mixing colors. I've tried to apply the geom_segment, but I don't know how to use positive and negative values without transforming it in logical values. – macs2021 Feb 17 '21 at 13:40
  • I experimented with `geom_segment`, see the update! – M.Viking Feb 17 '21 at 14:41
  • Thank you for the help, but it's not what I wanted et, this one has the same behavior as the scale_colour_steps, some data are still mixing. – macs2021 Feb 17 '21 at 15:00
  • Please post more representative data, where both up and downwelling is present. ... Is the idea you have, that the color changes as the line crosses over "zero"? – M.Viking Feb 17 '21 at 15:04
  • Yes! that's the idea, I noticed that when changing positive and negative values it only have one of the colors, what makes the entire line one color, when it should be dividing the colors after passing y=0. The data is very big, but I 'll edit the question. – macs2021 Feb 17 '21 at 15:17
  • Well then we can't use the 'event' label as color at all. and will need to split the segments a further time, at the line crossing of zero. – M.Viking Feb 17 '21 at 15:26
  • I see you asked this question before. this answer is 99% of the way there. i think just a small bug. please give him credit https://stackoverflow.com/a/65793035/10276092 – M.Viking Feb 17 '21 at 15:28
  • Thank you a lot! I saw that answer but for some reason my plot was all red. But it works now with the update! Thank you again. – macs2021 Feb 17 '21 at 15:35
0

Modification of @Rui Barradas answer. just this line mutate(line_color = (UI <= 0 & yend <= 0)) %>%

https://stackoverflow.com/a/65793035/10276092

lin_zeros <- function(data, x, y){
  xname <- deparse(substitute(x))
  yname <- deparse(substitute(y))
  x <- data[[xname]]
  y <- data[[yname]]
  ix <- which(c(FALSE, abs(diff(sign(y))) == 2))
  res <- lapply(ix, function(i) approx(x = y[c(i - 1, i)], y = x[c(i - 1, i)], xout = 0))
  res <- do.call(rbind.data.frame, res)
  res <- res[2:1]
  row.names(res) <- NULL
  names(res) <- c(xname, yname)
  res
}
dailyUI2018 %>%
  bind_rows(dailyUI2018 %>% 
              lin_zeros(Date, UI) %>%
              mutate(Date = as.POSIXct(Date, origin = "1970-01-01"))) %>%
  arrange(Date) %>%
  mutate(xend = lead(Date),
         yend = lead(UI, default = 0)) %>%
  mutate(line_color = (UI <= 0 & yend <= 0)) %>%
  mutate(Index = ifelse(line_color, "Downwelling", "Upwelling"),
         Index = factor(Index)) %>%
  select(-line_color) %>% 
  ggplot(aes(Date, UI, color = Index)) +
  geom_segment(aes(xend = xend, yend = yend)) +
  scale_color_manual(breaks = c("Downwelling", "Upwelling"),
                     values = c("dodgerblue4", "firebrick")) +
  ggtitle("Upwelling Index during the period 2012-2019") +
  xlab("Year") + 
  ylab("Upwelling index") +
  theme(plot.title = element_text(hjust = 0.5))

enter image description here

M.Viking
  • 5,067
  • 4
  • 17
  • 33