Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two lines in ggplot: change colors for segments

Tags:

plot

r

ggplot2

I'm working with one data frame of 3 variables: Date, Close and Popularity. I want to plot both Date and Popularity in y-axis with Date on x-axis. In ggplot I am using the following code:

Train.test.plot <- ggplot(data = df, aes(x = Date)) + 
  scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
  geom_line(aes(y = Close, colour = "Close"), size = 0.5,linetype=1) +
  geom_line(aes(y = Popularity, colour = "Popularity"), size = 0.5,linetype=1) +
  scale_colour_manual("",labels = c("Close", "Popularity"), values = c("blue", "red")) +
  scale_y_continuous(breaks = seq(0,1,by=0.1)) +
  ylab(NULL) +
  xlab(NULL)

However, one tricky thing I want to do, is to change the color of both line after a certain date.

Basically, for the segment from date 2017-10-22 until 2018-01-28 I would like the Popularity line to be a different color (say, yellow) and same thing for the Close line to be a different color (say, pink). The idea is to have a break point on 2017-10-22, after which the lines change color.

This is my plot for now: enter image description here

This is the data frame df I am using:

df <- structure(list(Date = structure(c(16727, 16734, 16741, 16748, 
16755, 16762, 16769, 16776, 16783, 16790, 16797, 16804, 16811, 
16818, 16825, 16832, 16839, 16846, 16853, 16860, 16867, 16874, 
16881, 16887, 16894, 16901, 16908, 16915, 16922, 16929, 16936, 
16943, 16950, 16957, 16964, 16971, 16978, 16985, 16992, 16999, 
17006, 17013, 17020, 17027, 17034, 17041, 17048, 17055, 17062, 
17069, 17076, 17083, 17090, 17097, 17105, 17112, 17119, 17126, 
17133, 17140, 17147, 17154, 17161, 17168, 17175, 17182, 17189, 
17196, 17203, 17210, 17217, 17224, 17231, 17238, 17245, 17251, 
17258, 17265, 17272, 17279, 17286, 17293, 17300, 17307, 17314, 
17321, 17328, 17335, 17342, 17349, 17356, 17363, 17370, 17377, 
17384, 17391, 17398, 17405, 17412, 17419, 17426, 17433, 17440, 
17447, 17454, 17461, 17468, 17475, 17482, 17489, 17496, 17503, 
17510, 17517, 17524, 17531, 17538, 17545, 17552, 17559), class = "Date"), 
    Close = c(0.887015478609428, 0.895394650983407, 0.911071982225385, 
    0.878365910952516, 0.904314532833858, 0.950265188696241, 
    0.944859229183019, 0.920532411373522, 0.938101779791492, 
    0.993783178995552, 1, 0.900800610496628, 0.852417289071174, 
    0.905125394325084, 0.928911664836894, 0.838091528796895, 
    0.813764710987398, 0.860526228341007, 0.819711217798306, 
    0.827009295576912, 0.793492362812817, 0.840524194359966, 
    0.777004170079613, 0.796735889867114, 0.789167578984361, 
    0.765651622666091, 0.785113109349445, 0.797817146641273, 
    0.760515977346409, 0.767543740931475, 0.83592909633797, 0.85376877894948, 
    0.817278552235235, 0.76321895710302, 0.719430733699561, 0.651315611397213, 
    0.521031954692818, 0.500759606518237, 0.534817167669411, 
    0.562387593622598, 0.593201546630082, 0.583741117481944, 
    0.641855182249075, 0.646990827568757, 0.652396787081979, 
    0.672128587958873, 0.682670192791776, 0.627259107781256, 
    0.657262199297514, 0.644558162005686, 0.57049653289243, 0.540493441376172, 
    0.543196421132783, 0.51184183973822, 0.523464701345281, 0.531033012228034, 
    0.568063867329359, 0.545899400889394, 0.58455197897317, 0.622123462461574, 
    0.578605472162262, 0.593742175017161, 0.606986759606675, 
    0.561576651041979, 0.596985702071458, 0.581038137725333, 
    0.505895332927312, 0.471837771776138, 0.484271494874426, 
    0.46264765682154, 0.461025852749695, 0.453457460777549, 0.472378319073824, 
    0.463999146699845, 0.494813099707329, 0.506976508612077, 
    0.495083413900869, 0.505895332927312, 0.511030978246994, 
    0.555359830037531, 0.526167681101892, 0.528059718277884, 
    0.53265481629988, 0.544818225204628, 0.521572583079897, 0.496434903779174, 
    0.476973417095819, 0.463458518312766, 0.415075196887312, 
    0.418589038135149, 0.351284858413419, 0.372097834975079, 
    0.373719557957532, 0.377233480294761, 0.315335219541557, 
    0.312091651942564, 0.141803927276086, 0.109097856003217, 
    0.103691896489996, 0.116125619588284, 0.126667224421187, 
    0.171266390405265, 0.182348623625247, 0.154507924023217, 
    0.148561376667613, 0.149394582097471, 0.142341455707527, 
    0.135288329317582, 0.133164160315015, 0.1408979060872, 0.1161005331027, 
    0.105104240802854, 0.10002269736786, 0.0831116562031625, 
    0.0711295724258412, 0, 0.0373074900964456, 0.0401122784812546, 
    0.0478460242534403, 0.0407928978634958), Popularity = c(0.293577981651376, 
    0.128440366972477, 0.137614678899083, 0.155963302752294, 
    0.128440366972477, 0.165137614678899, 0.128440366972477, 
    0.110091743119266, 0.146788990825688, 0.0825688073394495, 
    0, 0.174311926605505, 0.137614678899083, 0.119266055045872, 
    0.339449541284404, 0.165137614678899, 0.192660550458716, 
    0.229357798165138, 0.110091743119266, 0.18348623853211, 0.146788990825688, 
    0.18348623853211, 0.165137614678899, 0.192660550458716, 0.220183486238532, 
    0.174311926605505, 0.174311926605505, 0.119266055045872, 
    0.165137614678899, 0.26605504587156, 0.201834862385321, 0.18348623853211, 
    0.192660550458716, 0.26605504587156, 0.155963302752294, 0.26605504587156, 
    0.385321100917431, 0.36697247706422, 0.284403669724771, 0.311926605504587, 
    0.321100917431193, 0.192660550458716, 0.321100917431193, 
    0.247706422018349, 0.211009174311927, 0.211009174311927, 
    0.293577981651376, 0.275229357798165, 0.247706422018349, 
    0.321100917431193, 0.247706422018349, 0.321100917431193, 
    0.220183486238532, 0.256880733944954, 0.275229357798165, 
    0.247706422018349, 0.201834862385321, 0.229357798165138, 
    0.211009174311927, 0.155963302752294, 0.256880733944954, 
    0.238532110091743, 0.0458715596330275, 0.238532110091743, 
    0.275229357798165, 0.174311926605505, 0.357798165137615, 
    0.284403669724771, 0.321100917431193, 0.247706422018349, 
    0.284403669724771, 0.247706422018349, 0.357798165137615, 
    0.174311926605505, 0.18348623853211, 0.192660550458716, 0.256880733944954, 
    0.238532110091743, 0.192660550458716, 0.229357798165138, 
    0.165137614678899, 0.211009174311927, 0.211009174311927, 
    0.247706422018349, 0.128440366972477, 0.192660550458716, 
    0.26605504587156, 0.155963302752294, 0.302752293577982, 0.26605504587156, 
    0.302752293577982, 0.275229357798165, 0.311926605504587, 
    0.311926605504587, 0.311926605504587, 0.293577981651376, 
    0.81651376146789, 0.403669724770642, 0.36697247706422, 0.339449541284404, 
    0.403669724770642, 0.36697247706422, 0.339449541284404, 0.412844036697248, 
    0.339449541284404, 0.26605504587156, 0.26605504587156, 0.26605504587156, 
    0.220183486238532, 0.0825688073394495, 0.247706422018349, 
    0.284403669724771, 0.26605504587156, 0.357798165137615, 0.403669724770642, 
    1, 0.587155963302752, 0.495412844036697, 0.357798165137615, 
    0.357798165137615)), row.names = c(NA, -120L), .Names = c("Date", 
"Close", "Popularity"), class = "data.frame")
like image 727
Notna Avatar asked Dec 23 '22 12:12

Notna


1 Answers

library(ggplot2)

ggplot(df) +
    geom_line(aes(x = Date, y = Close), color = "red", size = 1) +
    geom_line(aes(x = Date, y = Popularity), color = "blue", size = 1) +
    geom_line(data = df[df$Date >= as.Date("2017-10-22") & df$Date <= "2018-01-28",], 
              aes(x = Date, y = Close), color = "black", size =1) +
    geom_line(data = df[df$Date >= as.Date("2017-10-22") & df$Date <= "2018-01-28",], 
              aes(x = Date, y = Popularity), color = "green", size = 1) +
    theme_classic()

enter image description here

like image 134
d.b Avatar answered Dec 28 '22 07:12

d.b