ggplot2 - Colouring a specific line in a line chart, in r, after combining two data frames -


i using r , ggplot2, have 2 data frames. both contain lot more data showing. go 1970-2016.

head(attendance)

tm attendance attend/g batage page bpf ppf #hof #a-s #a-ta-s est. payroll time managers year chall succ succ% atl  1078848    13319   29.5  28.7 105 105   4    4      13               2:33  harris 1970  <na> <na>  <na>  bal  1057069    13050   28.8 28.5 101  98    3    7      15               2:34  weaver 1970  <na> <na>  <na>   head(df) year avg_attendance 1970   1197806 1971   1216392 

i've combined them create graphic

enter image description here

using code,

r <- ggplot() r + geom_line(data = attendance, aes(x = year, y = attendance, group = tm), colour = "grey") + geom_line(data = df, aes(x = year, y = avg_attendance), colour = "red")  

but, in attendance data frame want colour 1 of lines tm (teams). value mil, average attendance.

i able colour mil using code,

p <- ggplot(data = attendance, aes(x = year, y = attendance, group = tm)) p + geom_line(aes(colour = tm == "mil")) + scale_color_manual(values = c("#cccccc","#000066")) 

enter image description here

when tried add df data frame, however, kept getting error.

error in eval(expr, envir, enclos): object 'tm' not found.  

basically, can colour mil variable in original chart? possible make stand out more? example, in second graph it's being overlapped few lines.

really, want i'm trying compare mil attendance average attendance.

all , appreciated.

apologies, should able use data. df data can use 1 on top , should work. line graph won't same, but, think, should work code i'm looking for.

   tm attendance  attend/g  batage page bpf ppf hof time year chall succ succ% 1  atl    1078848    13319   29.5 28.7 105 105    4 2:33 1970  <na> <na>  <na> 2  bal    1057069    13050   28.8 28.5 101  98    3 2:34 1970  <na> <na>  <na> 3  bos    1595278    19695   26.5 27.9 108 107    1 2:34 1970  <na> <na>  <na> 4  cal    1077741    13305   26.6 26.0  96  97    0 2:35 1970  <na> <na>  <na> 5  chc    1642705    20534   30.0 27.7 111 110    5 2:34 1970  <na> <na>  <na> 6  chw     495355     5897   27.0 27.0 101 102    1 2:29 1970  <na> <na>  <na> 7  cin    1803568    22266   25.9 25.2 104 103    2 2:31 1970  <na> <na>  <na> 8  cle     729752     9009   26.2 25.9 104 105    0 2:36 1970  <na> <na>  <na> 9  det    1501293    18534   28.8 26.6 101 101    1 2:39 1970  <na> <na>  <na> 10 hou    1253444    15475   26.6 26.3  96  96    1 2:36 1970  <na> <na>  <na> 11 kcr     693047     8773   26.4 26.2  99 100    0 2:36 1970  <na> <na>  <na> 12 lad    1697142    20952   27.9 26.7  95  94    1 2:36 1970  <na> <na>  <na> 13 mil     933690    11527   29.2 27.8 100 101    0 2:36 1970  <na> <na>  <na> 14 min    1261887    15579   28.6 28.1 103 102    3 2:38 1970  <na> <na>  <na> 15 mon    1424683    17809   27.7 26.7  99 101    0 2:35 1970  <na> <na>  <na> 16 nym    2697479    32896   26.5 26.1 100  99    2 2:36 1970  <na> <na>  <na> 17 nyy    1136879    14036   27.0 27.5  95  95    0 2:30 1970  <na> <na>  <na> 18 oak     778355     9609   27.7 27.2  97  95    4 2:31 1970  <na> <na>  <na> 19 phi     708247     8853   26.7 28.8  97  99    1 2:33 1970  <na> <na>  <na> 20 pit    1341947    16365   27.7 27.9  97  96    3 2:30 1970  <na> <na>  <na> 21 sdp     643679     7947   26.6 26.4  95  98    0 2:32 1970  <na> <na>  <na> 22 sfg     740720     9145   28.0 28.4  99  99    4 2:48 1970  <na> <na>  <na> 23 stl    1629736    20120   27.7 26.4 102 102    4 2:31 1970  <na> <na>  <na> 24 wsa     824789    10183   28.1 27.6  95  95    0 2:33 1970  <na> <na>  <na> 25 atl    1006320    12272   27.2 28.5 106 107    5 2:26 1971  <na> <na>  <na> 26 bal    1023037    13286   29.9 29.5  99  97    3 2:25 1971  <na> <na>  <na> 27 bos    1678732    20984   27.9 29.1 108 107    3 2:27 1971  <na> <na>  <na> 28 cal     926373    11437   27.7 26.4  92  93    0 2:31 1971  <na> <na>  <na> 29 chc    1653007    20407   30.2 28.7 113 112    4 2:26 1971  <na> <na>  <na> 30 chw     833891    10295   25.6 25.9 103 104    0 2:30 1971  <na> <na>  <na> 31 cin    1501122    18532   26.8 25.1  95  94    2 2:21 1971  <na> <na>  <na> 32 cle     591361     7301   26.3 25.1 109 110    0 2:36 1971  <na> <na>  <na> 33 det    1591073    19643   29.6 26.7 105 104    1 2:42 1971  <na> <na>  <na> 34 hou    1261589    15575   26.1 26.5  97  97    1 2:27 1971  <na> <na>  <na> 35 kcr     910784    11244   27.2 26.7  98  98    0 2:29 1971  <na> <na>  <na> 36 lad    2064594    25489   28.7 28.4  94  93    2 2:28 1971  <na> <na>  <na> 37 mil     731531     8921   27.1 26.0  98  99    0 2:29 1971  <na> <na>  <na> 38 min     940858    11910   28.5 27.3 103 102    3 2:36 1971  <na> <na>  <na> 39 mon    1290963    16137   28.3 27.0  99 101    0 2:27 1971  <na> <na>  <na> 40 nym    2266680    27984   26.6 26.1  98  98    2 2:28 1971  <na> <na>  <na> 41 nyy    1070771    13219   28.3 27.3  94  94    0 2:28 1971  <na> <na>  <na> 42 oak     914993    11296   26.8 26.5  98  96    4 2:34 1971  <na> <na>  <na> 43 phi    1511223    18657   26.3 28.6 100 102    1 2:29 1971  <na> <na>  <na> 44 pit    1501132    18764   27.7 27.7 102  99    3 2:27 1971  <na> <na>  <na> 45 sdp     557513     6883   26.4 26.3  93  95    0 2:25 1971  <na> <na>  <na> 46 sfg    1106043    13655   27.3 28.5  99  99    4 2:38 1971  <na> <na>  <na> 47 stl    1604671    19569   28.5 27.1 104 104    4 2:26 1971  <na> <na>  <na> 48 wsa     655156     8088   27.6 26.4  93  95    0 2:28 1971  <na> <na>  <na> 

ggplot looking group variable again, , not finding it. should able either add column tm df (set "avg" if like), or set group null.

to bring line front, need set factor levels (for points, sorting works, lines calculated first, sorted factor levels, implied or explicit, , plotted).

since didn't provide sample data, here working versions of each builtin set.

# generate average line average <-   chickweight %>%   group_by(time) %>%   summarise(weight = mean(weight))  # specify levels desired 1 @ end # bit trickier text, doable chicklevs <- c((1:50)[-20], 20)   # plot adding variable: ggplot(chickweight %>%          mutate(plotchick = factor(chick, levels = chicklevs))        , aes(x = time, y = weight, group = plotchick)) +   geom_line(aes(col = chick == 20)) +   geom_line(data = average %>% mutate(plotchick = "avg")             , col = "red3") +   scale_color_manual(values = c("#cccccc","#000066"))   # plot setting group null ggplot(chickweight %>%          mutate(plotchick = factor(chick, levels = chicklevs))        , aes(x = time, y = weight, group = plotchick)) +   geom_line(aes(col = chick == 20)) +   geom_line(data = average             , aes(group = null)             , col = "red3") +   scale_color_manual(values = c("#cccccc","#000066")) 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

Sound is not coming out while implementing Text-to-speech in Android activity -