首页 > 解决方案 > 在ggplot中为特定颜色着色最大,最小和中值价格路径

问题描述

目前我有一个包含 20 个模拟价格路径的数据表“test_data”。数据片段如下所示:

Month variable     value
1       V1         1.0000000
2       V1         1.0107850
3       V1         1.0265488
4       V1         1.0636623
5       V1         1.0941731
6       V1         1.1097758
7       V1         1.1116170
8       V1         1.1229932
9       V1         1.1779669
10      V1         1.1610330
11      V1         1.1408806
1       V2         1.0000000
2       V2         1.0109236
3       V2         1.0053303
4       V2         1.0126677
5       V2         1.0461632
6       V2         1.0747788
7       V2         1.0997711
8       V2         1.1196782
9       V2         1.1354743
10      V2         1.1602896
11      V2         1.1596831
1       V3         1.0000000
2       V3         1.0572598
3       V3         1.0374854

20 条价格路径 V(1 到 20)中的每一条都从 1 开始,并以某个值结束。我使用 ggplot 绘制了 20 条价格路径

ggplot(test_data, aes(Month,value, col=variable)) + 
  geom_line()+
  ggtitle("Simulated Price Paths") +
  theme_light() +
  theme(legend.title = element_blank()) +
  theme(legend.position = "none")

给出了下图

在此处输入图像描述

我想要三个特定的价格路径,颜色为黑色,线条更粗:

- 价格路径以第 11 个月的最高值结束

- 第 11 个月的最低路径结束值

- 第 11 个月的中位数

我是 ggplot 的新手,完全不确定如何实现这一点。感谢所有帮助!

标签: rggplot2colors

解决方案


您需要有另一列来指定要变黑的变量。您可以创建另一个具有该列的数据框,也可以将该列添加到原始数据框中。我认为在这种情况下,只有一个单独的数据框会更容易,这样您就不必玩弄这些scale_color_*功能了。仅供参考,您应该以文本格式提供数据,以便人们更轻松地提供帮助。R 中的dput函数使您可以轻松地做到这一点。

library(tidyverse)
test_data = expand.grid(Month = 1:11,variable=paste0('V',1:20),stringsAsFactors = F) %>%
  mutate(value=runif(nrow(.)))
dput(test_data) # the data is shown in code cell below the graph

# make a df that says which variables have the max/min/median month 11 value
foo = test_data %>% filter(Month == 11) %>%
  # order the value column
  arrange(value) %>%
  # add columns to find min, max, and "median" the middle value
  mutate(row_num = row_number(),
         new_col = if_else(row_num == 1,'min',
                           if_else(row_num ==  nrow(.),'max',
                                   # may have to play with median because no value guaranteed to exactly equal the median
                                   if_else(row_num == round(nrow(.)/2),'median','other')))) %>%
  select(variable,new_col)

# add that new variable to the data based on the variable column
test_data_with_classifier = test_data %>%
  left_join(foo,by='variable') %>%
  filter(new_col %in% c('min','max','median'))

# build your initial plot but remove the color aesthetic and data from the ggplot call
# so it doesn't apply to second geom_line call
ggplot(mapping=aes(Month,value)) +
  # original geom_line
  geom_line(aes(color=variable),data=test_data) +
  ggtitle("Simulated Price Paths") +
  theme_light() +
  theme(legend.title = element_blank()) +
  theme(legend.position = "none") +
  # new geom_line to plot the black min,max,median data
  geom_line(data=test_data_with_classifier,mapping=aes(group=new_col),
            color='black',size=1)

新图

structure(list(Month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L), variable = c("V1", "V1", "V1", "V1", "V1", "V1", "V1", 
"V1", "V1", "V1", "V1", "V2", "V2", "V2", "V2", "V2", "V2", "V2", 
"V2", "V2", "V2", "V2", "V3", "V3", "V3", "V3", "V3", "V3", "V3", 
"V3", "V3", "V3", "V3", "V4", "V4", "V4", "V4", "V4", "V4", "V4", 
"V4", "V4", "V4", "V4", "V5", "V5", "V5", "V5", "V5", "V5", "V5", 
"V5", "V5", "V5", "V5", "V6", "V6", "V6", "V6", "V6", "V6", "V6", 
"V6", "V6", "V6", "V6", "V7", "V7", "V7", "V7", "V7", "V7", "V7", 
"V7", "V7", "V7", "V7", "V8", "V8", "V8", "V8", "V8", "V8", "V8", 
"V8", "V8", "V8", "V8", "V9", "V9", "V9", "V9", "V9", "V9", "V9", 
"V9", "V9", "V9", "V9", "V10", "V10", "V10", "V10", "V10", "V10", 
"V10", "V10", "V10", "V10", "V10", "V11", "V11", "V11", "V11", 
"V11", "V11", "V11", "V11", "V11", "V11", "V11", "V12", "V12", 
"V12", "V12", "V12", "V12", "V12", "V12", "V12", "V12", "V12", 
"V13", "V13", "V13", "V13", "V13", "V13", "V13", "V13", "V13", 
"V13", "V13", "V14", "V14", "V14", "V14", "V14", "V14", "V14", 
"V14", "V14", "V14", "V14", "V15", "V15", "V15", "V15", "V15", 
"V15", "V15", "V15", "V15", "V15", "V15", "V16", "V16", "V16", 
"V16", "V16", "V16", "V16", "V16", "V16", "V16", "V16", "V17", 
"V17", "V17", "V17", "V17", "V17", "V17", "V17", "V17", "V17", 
"V17", "V18", "V18", "V18", "V18", "V18", "V18", "V18", "V18", 
"V18", "V18", "V18", "V19", "V19", "V19", "V19", "V19", "V19", 
"V19", "V19", "V19", "V19", "V19", "V20", "V20", "V20", "V20", 
"V20", "V20", "V20", "V20", "V20", "V20", "V20"), value = c(0.3207616454456, 
0.763921687379479, 0.816522044595331, 0.749660312896594, 0.59870492015034, 
0.598363760858774, 0.724914398510009, 0.209003237076104, 0.17182691837661, 
0.724648888222873, 0.971318073105067, 0.369079714873806, 0.766701503191143, 
0.80729194264859, 0.511416860157624, 0.944176482968032, 0.0978654811624438, 
0.716958795208484, 0.38553729141131, 0.327095735818148, 0.918577790725976, 
0.865334192989394, 0.834687531692907, 0.638412003172562, 0.199683424085379, 
0.109074046369642, 0.584284831071272, 0.675479859812185, 0.713516661897302, 
0.0794180030934513, 0.384025492705405, 0.828940884210169, 0.699888165574521, 
0.735423399368301, 0.907396653899923, 0.248153271153569, 0.65956291789189, 
0.606529506854713, 0.516759118298069, 0.425723660970107, 0.162580149481073, 
0.610716496128589, 0.829937220551074, 0.00947263417765498, 0.511702742660418, 
0.652116083307192, 0.590301762800664, 0.909111243207008, 0.952054579043761, 
0.799994048662484, 0.158715128898621, 0.809286607196555, 0.0451009501703084, 
0.725396532332525, 0.0322660498786718, 0.112002467270941, 0.613799379440024, 
0.362014023819938, 0.271398104028776, 0.215616049477831, 0.307330428389832, 
0.757429565768689, 0.689513321500272, 0.408647255739197, 0.343476827954873, 
0.996903440682217, 0.685957340057939, 0.286161876749247, 0.0998274721205235, 
0.349568751640618, 0.448729029623792, 0.109180265571922, 0.606069714995101, 
0.744512610835955, 0.094664296368137, 0.502116194460541, 0.501746303169057, 
0.43027435336262, 0.358490471262485, 0.445414373651147, 0.299434100743383, 
0.484706832794473, 0.886581416241825, 0.662599268835038, 0.0641198861412704, 
0.834478059783578, 0.121147126657888, 0.912385334260762, 0.821587839862332, 
0.172848681919277, 0.889836875488982, 0.329370712861419, 0.72060881392099, 
0.952831936301664, 0.738097631372511, 0.15180463087745, 0.808533443138003, 
0.191091127460822, 0.721191958058625, 0.204728928627446, 0.358556402148679, 
0.274529718095437, 0.758775806752965, 0.842871483648196, 0.432739966316149, 
0.59929970279336, 0.86859522620216, 0.360078621422872, 0.182892573997378, 
0.453561760019511, 0.0182492395397276, 0.6718317004852, 0.193607804365456, 
0.352003240492195, 0.655018073506653, 0.00389497983269393, 0.212779281195253, 
0.382988286204636, 0.302621328504756, 0.826272390317172, 0.965064536314458, 
0.768436536425725, 0.486362407449633, 0.192464913008735, 0.126183372689411, 
0.642314247321337, 0.067070194054395, 0.214890752686188, 0.559360482031479, 
0.600042793899775, 0.062608469510451, 0.327152049634606, 0.567664009751752, 
0.184450926026329, 0.99510698672384, 0.896966585190967, 0.796468431828544, 
0.646989026572555, 0.609200611012056, 0.661479892674834, 0.0861319426912814, 
0.0504770788829774, 0.992110759951174, 0.242711320985109, 0.981608556117862, 
0.116498072398826, 0.32264380552806, 0.452757821884006, 0.427302462747321, 
0.604806548450142, 0.134451362304389, 0.89202874340117, 0.365254261530936, 
0.130264255683869, 0.953156332718208, 0.820071451831609, 0.516127375187352, 
0.265554389217868, 0.51679350156337, 0.994173315353692, 0.0734945498406887, 
0.851095038000494, 0.396602528868243, 0.711098247207701, 0.382826760644093, 
0.895369519013911, 0.684021956985816, 0.837272961856797, 0.94457382359542, 
0.843695271993056, 0.216426289873198, 0.412310103652999, 0.984022463904694, 
0.0559382131323218, 0.374431659234688, 0.106870166258886, 0.237605754984543, 
0.601918715750799, 0.634860647842288, 0.376950473990291, 0.625570185715333, 
0.163354992168024, 0.221454897429794, 0.174484543036669, 0.35767737054266, 
0.829755877144635, 0.435555089730769, 0.67005174444057, 0.995627127122134, 
0.126283596735448, 0.455085375113413, 0.707600452937186, 0.546373196877539, 
0.950488998554647, 0.881859563523903, 0.995245850877836, 0.692439902573824, 
0.279077719897032, 0.542088820366189, 0.809259225381538, 0.517200969392434, 
0.403256884543225, 0.984078820096329, 0.0350806810893118, 0.58890008716844, 
0.0394498331006616, 0.71059784758836, 0.176494438899681, 0.247808628715575, 
0.692503416212276, 0.581507980125025, 0.0534774195402861, 0.146403533173725, 
0.990876362659037, 0.654077555285767, 0.655882946448401, 0.627923337742686, 
0.422042631078511, 0.446751018986106, 0.110627941554412)), class = "data.frame", row.names = c(NA, 
-220L))

推荐阅读