我有以下问题! 我用这段代码用ggplot2创建了一个图:
bi28 <- ggplot(Loc28, aes(patch, biovmean, shape=group,colour=group,group=id)) +
geom_errorbar(aes(ymin=biovmean- biovse,ymax=biovmean+biovse),
colour="black",width=.4,position=pd,size=.7) +
geom_point(size=5,position=pd) +
ylab(expression(paste(Log~biovolume~"[",µm^3~mL^-1,"]"))) +
xlab("Patch-Position") +
theme_bw() +
scale_colour_manual(name="Nutrient supply, Dispersal",
labels=c("Grad, High","Grad, Low", "Uniform, High", "Uniform, Low"),
values=c("black","darkgrey","black","darkgrey")) +
scale_shape_manual(name="Nutrient supply, Dispersal",
labels=c("Grad, High","Grad, Low", "Uniform, High", "Uniform, Low"),
values=c(19,19,17,17)) +
theme(axis.title.y=element_text(size=18, face="plain", colour="black",
vjust=0.3),axis.text.y=element_text(size=12,face="bold",
colour="black",angle=90,hjust=0.4)) +
theme(axis.title.x=element_text(size=18,face="plain",colour="black",
vjust=0),axis.text.x=element_text(size=12,face="bold",
colour="black")) +
theme(legend.key=element_blank())+theme(legend.position=c(0.6,0.14)) +
theme(legend.box="horizontal") +
theme(axis.ticks=element_line(colour="black",size=1),
axis.ticks.length=unit(0.3,"cm")) +
theme(panel.border=element_rect(colour="black",size=1.5)) +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank()) +
annotate("text",label="(a)",x=1,y=18.5,size=9)
现在我想只为Grad,高(黑色圆圈)和Grad,低(灰色圆圈)添加一个非线性曲线,使用geom_smooth中的nls函数......
bi28 + geom_smooth(aes(group=1), method="nls",
formula=biovmean~a*patch^2+b*patch+c,start=list(a=.1,b=.5,c=.2),se=F,
linetype=1, colour="black")
...但我收到了这条消息:
Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
和唯一的锯齿形曲线。我知道我必须重复两次geom_smooth(aes(.....))以获得两条曲线。
但是,我不知道如何在图中拟合曲线。
提前感谢任何建议,
尼尔斯
P.S。我的数据框,是最后一个采样日期(第28天)的子集
structure(list(patch = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 5, 5, 5, 5), treat = structure(c(1L, 1L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L
), .Label = c("grad", "uniform"), class = "factor"), disp = structure(c(1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L), .Label = c("high", "low"), class = "factor"), days = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L), .Label = c("7", "14", "21", "28"), class = "factor"),
biovmean = c(16.1120455927322, 15.3554188684282, 17.3165093040388,
17.6218303026139, 16.6099317757225, 16.1923099447477, 17.6138860131455,
17.929187639503, 17.324281439863, 17.6978828174639, 17.1943857045216,
17.1990430007219, 17.7599841920424, 17.9527426970286, 17.3901664732389,
17.3885901759597, 17.4077117222741, 17.9359587186513, 17.0632166374749,
17.3234243871302), biovsd = c(0.386139235419791, 0.442996309054709,
0.605751887065899, 0.443659180068705, 0.299207353823959,
0.483598811629063, 0.277191716108104, 0.407960548494472,
0.684333385005494, 0.960623164358474, 0.245729109016748,
0.320988261651675, 0.225089646245845, 0.802478513571635,
0.406524651605786, 0.445621562118268, 0.309621120995582,
0.313963803402196, 0.321686079165377, 1.3844064397821), biovse = c(0.222937591514292,
0.255764038282747, 0.349731015059621, 0.256146747041115,
0.172747446273779, 0.279205904073823, 0.160036711912148,
0.235536132492031, 0.39510006404837, 0.554616042532155, 0.141871767238546,
0.185322659271305, 0.129955567851836, 0.463311185829474,
0.23470711703682, 0.257279728845683, 0.178759837553593, 0.181267086410056,
0.185725544400685, 0.79928743067605), richmean = c(4, 4.33333333333333,
4, 5, 4, 4.33333333333333, 4.66666666666667, 5, 4, 5.33333333333333,
4.33333333333333, 4.33333333333333, 3.66666666666667, 4.66666666666667,
4.66666666666667, 4.33333333333333, 3.33333333333333, 5,
4.33333333333333, 4.66666666666667), richsd = c(0, 1.15470053837925,
0, 0, 0, 1.15470053837925, 0.577350269189626, 0, 1, 0.577350269189626,
0.577350269189626, 0.577350269189626, 1.15470053837925, 1.52752523165195,
0.577350269189626, 0.577350269189626, 0.577350269189626,
0, 0.577350269189626, 0.577350269189626), richse = c(0, 0.666666666666667,
0, 0, 0, 0.666666666666667, 0.333333333333333, 0, 0.577350269189626,
0.333333333333333, 0.333333333333333, 0.333333333333333,
0.666666666666667, 0.881917103688197, 0.333333333333333,
0.333333333333333, 0.333333333333333, 0, 0.333333333333333,
0.333333333333333), evenmean = c(0.756050730666667, 0.681044498666667,
0.596813298666667, 0.553620488333333, 0.777150884333333,
0.666109811666667, 0.512830639333333, 0.486772008666667,
0.613568058333333, 0.410651973333333, 0.642752105, 0.500671924333333,
0.678079105666667, 0.501918322, 0.625104587333333, 0.633017123,
0.625349769, 0.312000794333333, 0.792033323666667, 0.663921120333333
), evensd = c(0.121297293578432, 0.095557876163764, 0.220113939363098,
0.163036349226814, 0.0834972915863553, 0.0784293891088788,
0.12115524832891, 0.0364888610238083, 0.173556485434428,
0.194946745237399, 0.144495991615472, 0.216288511672041,
0.166484618361109, 0.354598600750826, 0.181924005800042,
0.175899869477491, 0.173365773474122, 0.0401721093066354,
0.100316932087287, 0.198268629314692), evense = c(0.070031025099481,
0.0551703655263381, 0.127082842143674, 0.0941290801137949,
0.0482071837739869, 0.0452812289143891, 0.0699490152364324,
0.0210668537345185, 0.100202883585172, 0.112552555840454,
0.0834247996560147, 0.124874230436476, 0.0961199392267183,
0.204727597597754, 0.105033873720709, 0.10155583699325, 0.100092775983552,
0.023193378122101, 0.0579180077448726, 0.114470446506696),
rueNmean = c(16.5572610572757, 15.708421773738, 19.5444508541557,
19.8497718526503, 17.9816839091709, 17.6482664544002, 19.8418275631974,
20.157129189519, 19.0790959759891, 19.6778832709719, 19.4223272545649,
19.4269845507481, 19.7917875822563, 20.2757124037856, 19.618108023301,
19.6165317260058, 19.3928384979227, 20.1569434904781, 19.29115818742,
19.5513659372153), rueNsd = c(0.386139235342985, 0.442996308940832,
0.605751886963905, 0.443659180100931, 0.299207353800656,
0.483598811677173, 0.277191716150326, 0.40796054848411, 0.684333384989912,
0.960623164542173, 0.245729108852386, 0.320988261711241,
0.225089646157831, 0.802478513467233, 0.40652465162401, 0.44562156214825,
0.309621121136794, 0.313963803485894, 0.321686079153626,
1.38440643994791), rueNse = c(0.222937591469949, 0.255764038217,
0.349731015000734, 0.256146747059721, 0.172747446260324,
0.279205904101599, 0.160036711936525, 0.235536132486048,
0.395100064039374, 0.554616042638214, 0.141871767143652,
0.185322659305695, 0.129955567801021, 0.463311185769197,
0.234707117047341, 0.257279728862994, 0.178759837635122,
0.181267086458379, 0.185725544393901, 0.799287430771776),
ruePmean = c(21.4280385379173, 20.8031653238865, 22.2268468768554,
22.5321678753869, 22.3060626586716, 22.1474816795235, 22.5242235859877,
22.8395252122633, 22.5865691209464, 23.1211788972626, 22.1047232773335,
22.109380573508, 22.5340390228808, 22.6670960859443, 22.3005040460323,
22.2989277486916, 20.709332934306, 21.1214532969654, 21.9735542101992,
22.2337619600135), ruePsd = c(0.386139235311824, 0.442996309033614,
0.605751887006541, 0.443659180111885, 0.299207353795987,
0.48359881169521, 0.277191716106355, 0.407960548510935, 0.684333384965727,
0.960623164323019, 0.245729109018956, 0.320988261625588,
0.225089646230825, 0.802478513511538, 0.406524651558781,
0.445621562198412, 0.309621121115937, 0.313963803594971,
0.321686079221144, 1.38440643963915), ruePse = c(0.222937591451958,
0.255764038270568, 0.34973101502535, 0.256146747066045, 0.172747446257629,
0.279205904112013, 0.160036711911138, 0.235536132501536,
0.39510006402541, 0.554616042511685, 0.141871767239821, 0.185322659256243,
0.129955567843164, 0.463311185794777, 0.234707117009681,
0.257279728891954, 0.17875983762308, 0.181267086521355, 0.185725544432883,
0.799287430593515), id = 1:20, group = c("gradhigh", "gradlow",
"uniformhigh", "uniformlow", "gradhigh", "gradlow", "uniformhigh",
"uniformlow", "gradhigh", "gradlow", "uniformhigh", "uniformlow",
"gradhigh", "gradlow", "uniformhigh", "uniformlow", "gradhigh",
"gradlow", "uniformhigh", "uniformlow")), .Names = c("patch",
"treat", "disp", "days", "biovmean", "biovsd", "biovse", "richmean",
"richsd", "richse", "evenmean", "evensd", "evense", "rueNmean",
"rueNsd", "rueNse", "ruePmean", "ruePsd", "ruePse", "id", "group"
), row.names = c(3L, 7L, 11L, 15L, 19L, 23L, 27L, 31L, 35L, 39L,
43L, 47L, 51L, 55L, 59L, 63L, 67L, 71L, 75L, 79L), class = "data.frame")
答案 0 :(得分:4)
为geom_smooth
创建公式时,您需要引用aesthetics
x
和y
,而不是映射到它们的变量。因此公式应该是'y ~ a*x^2 + b*x +c'
(它也可以帮助将公式放在引号中)。
如果要按“组”变量进行分组,然后只绘制数据的子集(即group = 'gradhigh'
或group = 'uniformhigh'
),则可以使用subset argument (and
组= group`)
例如
library(plyr) # for `.` function
bi28 + geom_smooth(aes(group=group), method = "nls", formula = 'y ~ a*x^2 + b*x +c',
start=list(a=.1,b=.5,c=.2),se = FALSE, linetype = 1, colour = "black",
subset = .(group %in% c('gradhigh', 'uniformhigh')))
会给出类似
的情节
(定义{{1}},因为这不在问题中)
您似乎拟合二次多项式。您可以使用x
上的多项式的线性模型拟合相同的模型pd= 'dodge'