1

I have following problem! I create a figure with ggplot2 using this code:

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)

Now I would like to add only for Grad, high (black circle) and for Grad, low (grey circle) a non-linear curve using the nls function in geom_smooth...

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")

...but I got this message:

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

and the only a zigzag curve. I know that I have to repeat geom_smooth(aes(.....)) twice to get both curves.

However, I have no idea how to fit a curve in the figure.

Thanks in advance for any suggestions,

Nils

p.s. My dataframe, a subset of the last sampling date (day 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")
bdemarest
  • 14,397
  • 3
  • 53
  • 56
Nils Guelzow
  • 31
  • 2
  • 3
  • 2
    Welcome to Stack Overflow. You have supplied code with your first question - well done - and some data. I would offer two suggestions. (1) Make the data reproducible by assigning the `structure` to something, presumably `Loc28` and making sure that all the other objects are supplied. What is `pd` for example? Include library calls also. Ideally we should be able to copy your code "as is" then paste and run it in our own installations. (2) Prune your code to make it minimal. There's a lot of code for font sizes etc in the above, which distracts from the key issue. Keep it simple. – SlowLearner Aug 17 '13 at 07:17
  • Thanks SlowLearner for your helpful comments. I will consider it next time and it makes sense to keep it simple as possible. – Nils Guelzow Aug 19 '13 at 07:45

1 Answers1

5

When creating a formula for geom_smooth, you need to reference the aesthetics x and y, not the variables mapped to them. Therefore the formula should be 'y ~ a*x^2 + b*x +c' (it can help to put the formula in quotes as well).

If you want to group by the "group" variable, and then only plot a subset of the data (ie group = 'gradhigh' or group = 'uniformhigh'), you can use the subset argument (andgroup = group`)

eg

 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')))

which will give a plot something like

enter image description here

(having defined pd= 'dodge' as this was not in the question)

You appear to be fitting a quadratic polynomial. You could fit the same model using a linear model with a polynomial on x

 bi28 + geom_smooth(aes(group=group), method = "lm", formula = 'y ~poly(x,2)', 
   se = FALSE, linetype = 1, colour = "black", 
   subset = .(group %in% c('gradhigh', 'uniformhigh')))
mnel
  • 113,303
  • 27
  • 265
  • 254