我正在处理3个变量的一个数据框:Date
,Close
和Popularity
。我想在y轴上绘制Date
和Popularity
,在x轴上绘制Date
。在ggplot
我使用以下代码:
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)
然而,我想要做的一件事是,在某个日期之后改变两条线的颜色。
基本上,对于从日期2017-10-22
到2018-01-28
的细分,我希望Popularity
行是不同的颜色(例如yellow
),同样的事情是Close
行为不同的颜色(例如pink
)。我们的想法是在2017-10-22
上设一个断点,之后线条会改变颜色。
这是我正在使用的数据框df
:
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")
答案 0 :(得分:4)
您可以通过几个步骤完成此操作。
首先聚集到长形,所以每一行都是观察:
df2 <- tidyverse::gather(df, color, value, Close:Popularity)
然后根据感兴趣的日期创建一个新列,这是我们想要着色的分组:
df2$new_col <- paste(df2$color,
ifelse(df$Date > as.Date("2017-10-22"), "new", "old"),
sep = "_")
创建一个命名向量作为我们的自定义调色板:
my_pal <- c("red", "pink", "blue", "gold")
names(my_pal) <- unique(df2$new_col)
现在绘制它:
ggplot(data = df2, aes(x = Date, y = value, color = new_col)) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
geom_line(size = .5) +
scale_color_manual(values = my_pal) +
scale_y_continuous(breaks = seq(0,1,by=0.1)) +
ylab(NULL) +
xlab(NULL)
颜色切换的行中有一点间隙,我们可以通过添加一些数据点来填补/平滑过渡来消除这种差距。
答案 1 :(得分:2)
我第二次以Nate的方式重塑它,因为我认为长格式效率要高得多,但是如果你发现它最好保持当前的格式,那么你可以简单地将两个geom_line
s绘制成四个将您的两个变量列变为四个NA
,您不希望绘制数据。
df$Close_after <- ifelse(df$date>=17461, df$Close, NA)
df$Close_before <- ifelse(df$date<17461, df$Close, NA)
df$Popularity_after <- ifelse(df$date>=17461, df$Popularity, NA)
df$Popularity_before <- ifelse(df$date<17461, df$Popularity, NA)
然后将您的两个geom_line
变为四个并调整scale_color_manual
Train.test.plot <- ggplot(data = df, aes(x = Date)) +
scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
geom_line(aes(y = Close_before, colour = "Close_before"), size = 0.5,linetype=1) + # change made here
geom_line(aes(y = Close_after, colour = "Close_after"), size = 0.5,linetype=1) + # change made here
geom_line(aes(y = Popularity_before, colour = "Popularity_before"), size = 0.5,linetype=1) + # change made here
geom_line(aes(y = Popularity_after, colour = "Popularity_after"), size = 0.5,linetype=1) + # change made here
scale_colour_manual("",labels = c("Close_before", "Close_after", "Popularity_before", "Popularity_after"), values = c("blue", "pink", "red", "yellow")) + # change made here
scale_y_continuous(breaks = seq(0,1,by=0.1)) +
ylab(NULL) +
xlab(NULL)