Saturday 15 August 2015

r - ggplot2 manually specifying colour with geom_line -



r - ggplot2 manually specifying colour with geom_line -

i'm trying plot graph below, , want manually specify colours.

i need plot genotype, since there multiple genotypes belonging same bgrnd_all, , want them come separately in lines plotted.

however, want colour lines bgrnd_all, , in order/colour utilize in scale_fill_manual.

when this, values in scale_fill_manual not overwrite existing colour defined in geom_line. how can this?

i'd grateful pointers.

[data graph below][1]https://www.dropbox.com/s/9nmu87wkh2yqfxn/summary_200_exp2.csv?dl=0

pd <- position_dodge(1) ggplot(data=summary.200.exp2, aes(x=time, y=length, colour=genotype, group=genotype)) + geom_errorbar(aes(ymin=length - se, ymax=length + se), colour="black", width=1, position=pd) + geom_line(aes(colour=bgrnd_all), position=pd, size =1) + scale_x_continuous(breaks=c(0,17,22,41,89)) + #using breaks of when sampled scale_fill_manual(values=c(avalon="#000066",av_a="#663399",av_b="#339999",cadenza="cc0033",cad_a="ff6600",cad_b="ff9933"))+ ylab("leaf segment width (mm)") + xlab("time") + theme(axis.title = element_text(size=14,face="bold"), axis.text = element_text(size=14), strip.text.y = element_text(size=14))

a dput of data:

summary.200.exp2 <- structure(list(x = 1:40, genotype = structure(c(1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 4l, 5l, 5l, 5l, 5l, 5l, 6l, 6l, 6l, 6l, 6l, 7l, 7l, 7l, 7l, 7l, 8l, 8l, 8l, 8l, 8l), .label = c("4.18", "4.41", "7.50", "7.59", "8.51", "8.77", "avalon", "cadenza"), class = "factor"), time = c(0l, 17l, 22l, 41l, 89l, 0l, 17l, 22l, 41l, 89l, 0l, 17l, 22l, 41l, 89l, 0l, 17l, 22l, 41l, 89l, 0l, 17l, 22l, 41l, 89l, 0l, 17l, 22l, 41l, 89l, 0l, 17l, 22l, 41l, 89l, 0l, 17l, 22l, 41l, 89l), bgrnd_all = structure(c(4l, 4l, 4l, 4l, 4l, 5l, 5l, 5l, 5l, 5l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 5l, 5l, 5l, 5l, 5l, 4l, 4l, 4l, 4l, 4l, 3l, 3l, 3l, 3l, 3l, 6l, 6l, 6l, 6l, 6l), .label = c("av_a", "av_b", "avalon", "cad_a", "cad_b", "cadenza"), class = "factor"), n = c(43l, 48l, 44l, 47l, 48l, 22l, 21l, 26l, 27l, 25l, 36l, 24l, 44l, 48l, 45l, 50l, 26l, 52l, 54l, 53l, 38l, 52l, 52l, 49l, 50l, 39l, 39l, 42l, 38l, 42l, 84l, 42l, 84l, 42l, 42l, 50l, 26l, 53l, 27l, 27l), length = c(1.17423255813953, 1.58852083333333, 1.71263636363636, 1.86736170212766, 2.0331875, 1.07563636363636, 1.49866666666667, 1.48734615384615, 1.66796296296296, 2.15416, 1.08716666666667, 1.09858333333333, 1.24593181818182, 1.30827083333333, 1.81537777777778, 1.15672, 1.8475, 1.96815384615385, 2.01822222222222, 2.5057358490566, 1.14697368421053, 1.40276923076923, 1.49832692307692, 1.76981632653061, 2.27954, 1.18312820512821, 1.75928205128205, 1.86195238095238, 1.91426315789474, 2.26883333333333, 1.10839285714286, 1.97902380952381, 2.03271428571429, 2.15685714285714, 2.8227380952381, 1.08658, 1.68880769230769, 1.7277358490566, 1.9232962962963, 2.13466666666667), sd = c(0.218740641945063, 0.357307960001092, 0.377931031662453, 0.416137123383518, 0.440003996899158, 0.176915784499843, 0.426273190962478, 0.305677731254037, 0.450036449932454, 0.48642939535627, 0.15212823538055, 0.175160775008132, 0.293836087650785, 0.282464815326021, 0.346608194369436, 0.211422397593258, 0.408328617659845, 0.413460118977535, 0.419730221832425, 0.508692484972064, 0.217587942685885, 0.207510416973071, 0.245473270071832, 0.377310585673427, 0.536134471785516, 0.159925670150259, 0.298319411009668, 0.338847829173593, 0.296186727462412, 0.445638589029855, 0.162594700328365, 0.308723610551514, 0.318831396748337, 0.381781291715339, 0.402059458017902, 0.167826451905484, 0.257140275994371, 0.338637947743116, 0.362428434825926, 0.343680867174096), se = c(0.0333576351702583, 0.0515729617225566, 0.0569752467571038, 0.0606998379642952, 0.06350910651356, 0.0377185719899813, 0.0930204363959963, 0.0599483352513503, 0.0866095551712153, 0.097285879071254, 0.0253547058967583, 0.0357545434766975, 0.0442974569365289, 0.040770284291269, 0.0516692989445678, 0.0298996422065822, 0.0800798303617661, 0.0573366022820362, 0.0571180485063685, 0.0698742866122227, 0.0352974252834232, 0.0287765172534354, 0.0340410177692235, 0.053901512239061, 0.0758208641254813, 0.0256086023072023, 0.0477693365291991, 0.052285355168868, 0.0480478318490224, 0.0687635271596866, 0.0177405362346046, 0.0476370873204908, 0.0347873573697084, 0.0589101322645314, 0.0620391212561054, 0.0237342444409691, 0.0504293571163821, 0.046515499476421, 0.0697493848029077, 0.0661414137260961), ci = c(0.0673184331863912, 0.103751416510302, 0.114901535684132, 0.122182436693452, 0.127763842564108, 0.0784400645137227, 0.194037230170767, 0.123465907623535, 0.178028490322197, 0.200788185881879, 0.0514727894594648, 0.0739639084701291, 0.0893343358495282, 0.0820192326650262, 0.104132629687123, 0.0600855805773719, 0.164927497928001, 0.11510803218647, 0.11456429705202, 0.140213013986381, 0.0715193770736051, 0.0577712690042106, 0.0683401947985261, 0.108376253996364, 0.152367731004308, 0.0518419050566429, 0.0967039660836575, 0.105592416917608, 0.0973541547573791, 0.138870760371045, 0.0352852130493688, 0.0962050495562246, 0.06919065466693, 0.118971425682342, 0.125290547146885, 0.0476957499005439, 0.103861205171753, 0.0933401784102089, 0.143371913789607, 0.135955623027448)), .names = c("x", "genotype", "time", "bgrnd_all", "n", "length", "sd", "se", "ci"), class = "data.frame", row.names = c(na, -40l))

as stated @juba in comments, should utilize scale_colour_manual instead of scale_fill_manual. moreover, trying plot many lines , errorbars in 1 plot. overlap each other much , hence hard distuinguish between lines/errorbars.

an illustration utilize of facetting (and simplification of code):

ggplot(summary.200.exp2, aes(x=time, y=length, group=genotype)) + geom_line(aes(colour=bgrnd_all), size =1) + geom_errorbar(aes(ymin=length-se, ymax=length+se, colour=bgrnd_all), width=2) + scale_x_continuous("time", breaks=c(0,17,22,41,89)) + scale_colour_manual(values=c(avalon="#000066",av_a="#663399",av_b="#339999",cadenza="#cc0033",cad_a="#ff6600",cad_b="#ff9933"))+ ylab("leaf segment width (mm)") + theme_bw() + theme(axis.title = element_text(size=14,face="bold"), axis.text = element_text(size=10)) + facet_wrap(~bgrnd_all, ncol=3)

this gives:

r colors ggplot2

No comments:

Post a Comment