我试图使用ggplot2绘制来自线图中两种不同颜色的独立研究数据。这些数据来自两项独立研究,这些研究使用了组内设计(同一家族中的多个兄弟姐妹,但每个都在不同的治疗组中)。因此,我使用了" group"通过家庭对个体进行分组的美学,从而在同一家庭中的个体之间划清界线。这部分效果很好,现在我想要的是在两个单独的研究中收集的数据用不同的颜色表示。我已经玩过一堆,手动改变颜色等等,我只是无法让它发挥作用。我在网上看到的关于手动改变线条颜色的一切都取决于" group"在这种情况下,我需要颜色和群体彼此独立。
数据采用长格式,研究是一个因素。然后我使用了summarySEwithin函数,它来自用于绘图的R-cookbook代码(在此处可用:http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_%28ggplot2%29/)来构建下面复制的数据框。
Similarity90SUM<- summarySEwithin(Day90, measurevar="Similarity", betweenvars=c("Condition","Study"),withinvars=c("Family"), idvar="Male_Num",na.rm=TRUE)
然后我使用ggplot来构建线图。这段代码完成了我想要的一切,除了以不同的颜色绘制两个研究中的数据。
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"))
不幸的是,情节中的所有线都是黑色的,而不是蓝色和绿色(抱歉,我无法发布图像)。
我认为问题来自于用于组和颜色的变量不同的事实,因为如果我在aes()中包含color = Study,那么我得到错误信息:&#34;错误:美学必须要么是长度一,要么与dataProblems相同:条件,相似度,家庭&#34;。任何的意见都将会有帮助!我确信有一些相对简单的解决方法,我只是不知道。提前谢谢!
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")
答案 0 :(得分:1)
这是你需要做的,我假设你比较相似性(y轴)和治疗(x轴)。 正如@Gregor所说,你需要在aes调用中传递color = study
您可以在ggplot调用或geom_line()图层调用中传递它 传递颜色=在ggplot调用的aes内学习 这将改变线条和点的颜色(查看图例)
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()
在geom_line调用中传递它只更改线条的颜色(还要注意图例如何更改)
ggplot(Similarity90SUM, aes(x = Treatment, y = Similarity,
group = Family)) +
geom_line(aes(colour = Study)) +
geom_point(shape = 19, size = 2)+
scale_colour_manual(values = c("blue","darkgreen")) +
xlab("Condition") +theme_bw()
结果图如下所示: