r - Line plot using ggplot2 with manual line/fill color independent of group aesthetic -



r - Line plot using ggplot2 with manual line/fill color independent of group aesthetic -

i trying plot info 2 separate studies in different colors in line plot using ggplot2. info comes 2 independent studies used within-group design (multiple siblings in same family, each in different treatment groups). thus, i've used "group" aesthetic grouping individuals family and, thus, draw lines between individuals within same family. part works well, want the info collected in 2 separate studies represented in different colors. i've played around bunch, manually changing color, etc. , can't work. i've seen online manually changing line color depends on "group" aesthetic, , need color , grouping independent of each other in case.

the info in long format, study factor. used summarysewithin function, comes r-cookbook code plotting means (available here: http://www.cookbook-r.com/graphs/plotting_means_and_error_bars_%28ggplot2%29/) build info frame copied below.

similarity90sum<- summarysewithin(day90, measurevar="similarity", betweenvars=c("condition","study"),withinvars=c("family"), idvar="male_num",na.rm=true)

i used ggplot build line plot. code want, except plot info 2 studies in separate colors.

ggplot(similarity90sum, aes(x=condition, y=similarity, group=family),colour=study) + geom_line() + geom_point(shape=19, size=2)+ scale_colour_manual(values=c("blue","darkgreen"))

unfortunately, of lines in plot black, rather bluish , greenish (sorry, can't post image).

i think issue derives fact variables used grouping , colour different because if include colour=study in aes(), error message: "error: aesthetics must either length one, or same length dataproblems:condition, similarity, family". suggestions helpful! i'm sure there relatively simple work-around i'm not aware of. thanks, in advance!

dput(similarity90sum)

structure(list(condition= structure(c(2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l), .label = c("control", "avt", "mc"), class = "factor"), study = structure(c(1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l ), .label = c("study 1", "study 2"), class = "factor"), family = structure(c(7l, 19l, 20l, 21l, 1l, 4l, 5l, 8l, 12l, 17l, 3l, 6l, 18l, 19l, 22l, 1l, 4l, 5l, 8l, 12l, 17l, 6l, 7l, 10l, 13l, 14l, 18l, 19l, 20l, 1l, 4l, 5l, 12l, 17l), .label = c("1", "150-2", "150-8", "2", "3", "34-4", "34-8", "4", "48-1", "48-2", "48-6", "5", "54-1", "54-5", "54-8", "6", "7", "79-2", "79-4", "87-4", "87-6", "87-8" ), class = "factor"), n = c(1, 1, 1, 1, 10, 9, 10, 10, 10, 4, 1, 1, 1, 2, 1, 10, 4, 7, 7, 10, 10, 1, 1, 1, 1, 1, 1, 1, 1, 9, 10, 10, 10, 9), similarity = c(73, 89, 80, 91, 93.3, 80.3333333333333, 46.1, 90.4, 81, 86.25, 62, 74, 75, 76.5, 66, 83.9, 79.75, 92.1428571428571, 68.7142857142857, 47.9, 91.2, 61, 76, 52, 41, 47, 60, 78, 61, 64.8888888888889, 49.1, 26.4, 44.3, 75), similarity_norm = c(69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503, 69.1437125748503), sd = c(na, na, na, na, 2.68860027311079, 5.29375193259087, 8.29349206197309, 3.34980059423901, 5.68856645911677, 4.12599194422499, na, na, na, 0, na, 4.13515065015422, 4.37253331823185, 4.63154911077632, 5.5344724669591, 4.58880625622207, 3.47264441877645, na, na, na, na, na, na, na, na, 10.5806302009727, 0.896200091036931, 7.30470602822113, 12.0629302291892, 3.02765035409749), se = c(na, na, na, na, 0.850210058078086, 1.76458397753029, 2.62263246723613, 1.05929995851808, 1.79888266320481, 2.0629959721125, na, na, na, 0, na, 1.30764945224135, 2.18626665911592, 1.75056101887093, 2.09183396935827, 1.4511079510892, 1.09814658672052, na, na, na, na, na, na, na, na, 3.52687673365756, 0.283403352692695, 2.3099508687141, 3.81463347799347, 1.00921678469916), ci = c(na, na, na, na, 1.92330877276442, 4.06913794909986, 5.93280682114534, 2.39630298871351, 4.06935530160228, 6.56537390926458, na, na, na, 0, na, 2.95810857481691, 6.95767625166843, 4.28346850322768, 5.11853333025038, 3.28263424554985, 2.48418016695222, na, na, na, na, na, na, na, na, 8.13299233216971, 0.641102924254807, 5.22547190337353, 8.62930044569276, 2.32725807883372)), .names = c("condition", "study", "family", "n", "similarity", "similarity_norm", "sd", "se", "ci"), row.names = c(na, -34l), class = "data.frame")

here's need do, assumed comparing similarity (y-axis) vs treatment (x-axis). @gregor said need pass colour = study in aes call

you can pass within ggplot phone call or geom_line() layer phone call passing colour = study within aes of ggplot phone call alter colour of both lines , points (look @ legend)

ggplot(similarity90sum, aes(x=treatment, y=similarity, group=family,colour=study)) + geom_line() + geom_point(shape=19, size=2)+ scale_colour_manual(values=c("blue","darkgreen")) + xlab("condition") +theme_bw()

passing within geom_line phone call alter colour of lines (note how legend changes)

ggplot(similarity90sum, aes(x = treatment, y = similarity, grouping = family)) + geom_line(aes(colour = study)) + geom_point(shape = 19, size = 2)+ scale_colour_manual(values = c("blue","darkgreen")) + xlab("condition") +theme_bw()

the resultant plots shown below:

r ggplot2

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -