Regression lines in ggplot - r

I have a 2 x 2 ancova with the resulting plot showing 4 regression lines and 4 groups in various colours on the plot. I wish to retain the 4 colours in the plot but only show 2 regression lines for one of the variables - not all 4 as shown. The data is here-
PLD.df <- structure(list(Site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Inshore", "OffReef"
), class = "factor"), Depth = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Deep", "Shallow"
), class = "factor"), PLD = c(37L, 38L, 47L, 51L, 51L, 53L, 34L,
39L, 40L, 45L, 49L, 49L, 26L, 29L, 35L, 35L, 36L, 36L, 37L, 38L,
38L, 40L, 41L, 46L, 47L, 52L, 37L, 38L, 40L, 45L, 45L), Location = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("ID",
"IS", "OD", "OS"), class = "factor"), b = c(0.052, 0.05, 0.039,
0.043, 0.036, 0.033, 0.055, 0.051, 0.048, 0.046, 0.041, 0.04,
0.05, 0.05, 0.051, 0.049, 0.056, 0.052, 0.047, 0.045, 0.047,
0.045, 0.045, 0.045, 0.039, 0.038, 0.046, 0.049, 0.046, 0.044,
0.041)), .Names = c("Site", "Depth", "PLD", "Location", "b"), class = "data.frame", row.names = c(NA,
-31L))
The plot is below-
ANCOVA Plot:
and the code i used to create it is here-
ggplot(PLD.df, aes(x=PLD, y=b, colour=Location)) +
geom_point(aes(shape=Location),size=3) +
scale_shape(solid=FALSE) +
scale_colour_manual(values=cb_palette) +
geom_smooth(aes(linetype=Location),method=lm, se=FALSE, fullrange=F) +
theme(panel.border=element_rect(colour="black", fill=NA,size=3),
panel.background=element_rect(fill=FALSE),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank()) +
theme(legend.position="NONE")
Would the easiest way to be to remove the lines all together and then use the predictvals() function to redraw the required lines? I would like only show the regression lines for the "Inshore" and "Offreef" locations while retaining the colours for all 4 sites.
Note: This is my first question here so apologies if my question format is not correct or I haven't included all of the necessary information. Thanks!

If I get you correct, you only specify the x and y inside the ggplot(aes(..)) call, then inside geom_smooth, you group according to the site (instead of Location?). This will give you a prediction within the Site:
cb_palette <- c("#999999", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7")
ggplot(PLD.df, aes(x=PLD, y=b)) +
geom_point(aes(shape=Location,colour=Location),size=3) +
scale_shape(solid=FALSE) +
scale_colour_manual(values=cb_palette) +
geom_smooth(aes(linetype=Site),
method=lm, se=FALSE, fullrange=F,col="gray") +
theme(panel.border=element_rect(colour="black", fill=NA,size=3),
panel.background=element_rect(fill=FALSE),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank()) +
theme(legend.position="NONE")

Related

How to remove duplicate x-axis labels in R

I am trying to obtain a barplot representing mean percentage of coloration (valores) grouped both by sex and size intervals (class). However, labels in the x-axis appear duplicated. I would like to get one single label ("50-55" for the first and second columns together, "55-60" for the third and fourth columns together, and so on) for each class level. How could I do this?
Here is my code:
par(mar=c(7,4,4,2)+0.1)
class<-factor(coloration$clase.2,levels=c("50-55","55-60","60-65","65-70","70-75","75-80"))
sex<-factor(coloration$sexo,levels=c("M","H"))
valores<-coloration$perc.greenblue
graf<-barplot(tapply(valores,list(sex,class),mean),beside=T,axes=F,ylim=c(0,50),col=c(grey.colors(2)),axisnames=F ,xlab=("Sex and size"),ylab=("% mean coloration"),las=1)
axis(2,at=c(0,5,10,15,20,25,30,35,40,45,50),labels=c(0,5,10,15,20,25,30,35,40,45,50),las=1)
labs<-as.character(class)
text(graf,par("usr")[3]-0.25,srt=0,adj = c(0,2),labels=labs,xpd=T,cex=1)
legend(locator(1),c("Adult males","Adult females"),fill=c(grey.colors(2)),bty="n")
EDIT: here's some reproducible code:
structure(list(edad = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "ADU", class = "factor"),
sexo = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("H", "M"), class = "factor"),
clase.2 = structure(c(2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L,
6L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L), .Label = c("50-55",
"55-60", "60-65", "65-70", "70-75", "75-80"), class = "factor"),
perc.greenblue = c(0.09, 0.32, 12.8, 94.32, 34.83, 0.04,
45.83, 12.34, 0.75, 34.82, 0.5, 0.05, 3.46, 0, 1.72, 0.07,
0.09, 0.2)), row.names = c(9L, 10L, 12L, 13L, 48L, 49L, 109L,
110L, 194L, 195L, 263L, 264L, 266L, 267L, 332L, 333L, 408L, 409L
), class = "data.frame")

Label an axis generated by axis() function

I'm plotting a dual axis graph in r base and I can't add a label to the secondary axis. Here's what I'm working with:
dados = structure(list(IDADE = c(65L, 35L, 65L, 42L, 50L, 44L, 0L, 58L,
22L, 27L, 34L, 31L, 0L, 24L, 34L, 20L, 4L, 34L, 20L, 27L, 8L,
2L, 20L, 31L, 45L, 26L, 26L, 40L, 50L, 34L), ESTADO = c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L), TIPINT = c(3L,
1L, 3L, 2L, 3L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L,
3L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 3L, 1L), DIARIAS = c(2L,
2L, 4L, 1L, 1L, 1L, 10L, 1L, 2L, 2L, 2L, 3L, 3L, 1L, 2L, 3L,
6L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 6L, 1L, 1L, 2L)), .Names = c("IDADE",
"ESTADO", "TIPINT", "DIARIAS"), row.names = c(NA, 30L), class = "data.frame")
#Define boxplot base
bx = boxplot(dados$IDADE~dados$TIPINT,axes=F, xlab=NA,ylab=NA,col=heat.colors(3,0.6))
#Posiciona eixos
par(mar = c(5,5,2,5))
#Plota bar
barplot(table(dados$TIPINT),col=heat.colors(3,0.4),names=c("Tipo 1","Tipo 2","Tipo 3"),
ylab = "Quantidade de pacientes")
#Plota box
par(new=T)
bx2 = bxp(bx,boxwex = 0.50, at = c(0.9, 2,3.1),axes=F, xlab=NA, ylab="l",boxfill=heat.colors(3,0.7))
axis(side=4)
Which gets me the following graph
I'm trying to label the right axis, but axis(side=4,labels="labels") is for something else and axis(side=4,ylab="label") doesn't work.
Thanks
You can use the function mtext to add a label to a secondary axis, using the line=2 argument to move it the appropriate distance from the axis.
mtext("label", 4, line=2)
Alternatively you could use just text to have finer control, if say, you want to reorient the label. You can pull the plot dimensions from par("usr") and adjust accordingly.
p <- par("usr")
text(p[2]+0.4, mean(p[3:4]), labels = "label", xpd=NA, srt = -90)

Newbie attempting linear mixed effects model in R studio - TOTAL FAIL

After searching over an hour (this forum, Youtube, class notes, google) I've found no help for my question. I'm a complete newb who knows nothing about R or stats.
I'm attempting to create a linear mixed effects model in R. I'm measuring leaf width across three different locations (Jacksonville FL, Augusta GA, & Atlanta GA), and within those three locations there is a high-nitrogen and low-nitrogen plot. I have 150 leaf measurements from 50 trees.
My limited understanding tells me that the leaf width is the continuous response variable, and city and plot are the discrete explanatory variables. The random effect would be the individual trees, since the leaf width within a single tree is non-independent.
I've used "nlme" to make a model:
leaf.width.model <- lme(width ~ city*plot, (1|tree.id), data=leaf)
I then ran an ANOVA test, and it suggested there's something going on with city and the interaction between city and plot. This is where I'm stuck. I want to make a plot that has lines for all three cities, but I haven't a clue how to do that. When I try to use the plot function, I just get a boxplot.
I've literally tried for hours and am more lost and confused than before.
1) How can I make this graph?
2) What other tests should I do to analyze and/or visualize this data?
I am forever grateful for any help at all. I really want to learn R and stats very badly, but I'm getting discouraged.
Thank you,
Rich
P.S Here is the output of the dput function:
> dput(tree) structure(list(tree.id = structure(c(24L, 24L, 32L, 25L, 25L, 24L, 24L, 32L, 25L, 25L, 43L, 45L, 45L, 43L, 23L, 23L, 45L, 45L, 23L, 23L, 41L, 41L, 38L, 11L, 11L, 38L, 41L, 41L, 11L, 11L, 14L, 14L, 29L, 13L, 13L, 14L, 14L, 29L, 13L, 13L, 4L, 4L, 1L, 1L, 20L, 1L, 1L, 20L, 6L, 8L, 8L, 5L, 5L, 6L, 4L, 4L, 8L, 8L, 5L, 5L, 9L, 9L, 10L, 10L, 12L, 12L, 13L, 13L, 22L, 22L, 23L, 23L, 24L, 24L, 25L, 25L, 25L, 25L, 40L, 40L, 41L, 41L, 38L, 38L, 39L, 39L, 14L, 14L, 14L, 15L, 15L, 28L, 28L, 29L, 29L, 35L, 35L, 36L, 36L, 37L, 37L, 42L, 42L, 43L, 43L, 44L, 44L, 45L, 45L, 46L, 46L, 47L, 47L, 2L, 1L, 3L, 3L, 4L, 4L, 7L, 11L, 11L, 16L, 16L, 20L, 20L, 21L, 21L, 17L, 17L, 18L, 18L, 19L, 19L, 26L, 26L, 27L, 27L, 30L, 30L, 31L, 31L, 32L, 32L, 33L, 33L, 34L, 34L, 48L), .Label = c("Tree_112", "Tree_112 ", "Tree_115", "Tree_130", "Tree_137", "Tree_139", "Tree_140", "Tree_141", "Tree_153", "Tree_154", "Tree_156", "Tree_159", "Tree_166", "Tree_169", "Tree_171", "Tree_180", "Tree_182", "Tree_184", "Tree_185", "Tree_202", "Tree_213", "Tree_218", "Tree_222", "Tree_227", "Tree_239", "Tree_242", "Tree_246", "Tree_247", "Tree_252", "Tree_260", "Tree_267", "Tree_269", "Tree_271", "Tree_272", "Tree_291", "Tree_293", "Tree_298", "Tree_327", "Tree_329", "Tree_336", "Tree_350", "Tree_401", "Tree_403", "Tree_405", "Tree_407", "Tree_409", "Tree_420", "Tree_851"), class = "factor"), city = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Atlanta", "Augusta", "Jacksonville"), class = "factor"), plot = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("High-N", "Low-N"), class = "factor"), width = c(0.66, 0.716, 0.682, 0.645, 0.645, 0.696, 0.733,
0.707, 0.668, 0.686, 0.617, 0.733, 0.73, 0.615, 0.669, 0.746, 0.687, 0.682, 0.76, 0.713, 0.651, 0.664, 0.679, 0.729, 0.756,
0.669, 0.647, 0.713, 0.767, 0.685, 0.69, 0.731, 0.781, 0.729,
0.725, 0.739, 0.769, 0.791, 0.676, 0.688, 0.719, 0.753, 0.748,
0.791, 0.785, 0.78, 0.723, 0.756, 0.664, 0.645, 0.653, 0.615,
0.591, 0.642, 0.693, 0.716, 0.694, 0.676, 0.662, 0.629, 0.665,
0.748, 0.726, 0.693, 0.715, 0.714, 0.764, 0.732, 0.61, 0.721,
0.703, 0.713, 0.746, 0.752, 0.662, 0.733, 0.707, 0.674, 0.734,
0.79, 0.732, 0.794, 0.703, 0.712, 0.737, 0.731, 0.747, 0.746,
0.787, 0.709, 0.716, 0.764, 0.77, 0.764, 0.802, 0.663, 0.777,
0.642, 0.779, 0.81, 0.724, 0.645, 0.68, 0.637, 0.695, 0.768,
0.761, 0.7, 0.759, 0.726, 0.696, 0.794, 0.774, 0.799, 0.747,
0.606, 0.691, 0.733, 0.707, 0.698, 0.706, 0.72, 0.694, 0.697,
0.737, 0.716, 0.73, 0.706, 0.667, 0.734, 0.528, 0.695, 0.684,
0.763, 0.733, 0.809, 0.6, 0.676, 0.718, 0.759, 0.609, 0.665,
0.667, 0.647, 0.701, 0.663, 0.688, 0.693, 0.899)), .Names = c("tree.id", "city", "plot", "width"), class = "data.frame", row.names = c(NA, -149L))
Thank you all so much for your comments, I sincerely appreciate everyone's help!
As suggested in comments, a line plot might not make sense for your data, as you are studying how width varies in discrete categories (in separate cities and separate plots). Boxplots would make sense as you can make them for each of the interactions of city and plot. To give you a sense of what you can do I generated some fake data and made an example of the sort of plot that might be helpful to you:
# fake data
leaf <- data.frame(tree.id = rep(1:50, each = 3),
city = rep(c("Jackson", "Augusta", "Atlanta"), each = 50),
plot = rep(1:6, each = 25))
# I'll make the average of width different for each plot
leaf$width <- rnorm(nrow(leaf), leaf$plot, 1)
# plotting the data
library(ggplot2) # this is a great library for plotting in R
ggplot(leaf, aes(x = factor(plot), y = width, color = factor(plot))) +
facet_grid(~city, scales = 'free_x') + # This creates a subplot for each city
geom_boxplot() +
geom_point(position = "jitter") +
theme_bw()
In this plot I added the points (the leaf widths for each individual tree) but I 'jittered' them, meaning perturbing their position slightly so that they do not pile up on top of each other and are all visible. You could remove this if you liked.
Exploratory data analysis should be fun! And I think visualization is a good place to start when beginning in statistics. Hopefully this will prove helpful to you.
leaf.width.model <- lme(width ~ city*plot, (1|tree.id), data=leaf)
In this model if you want to plot something, you are probably trying to answer:
How much is the average leaf width for all trees in each city for each type of plot.
To show this information in a figure, you need to plot width on y axis plot plot(high and low nitrogen) on x axis and group the data by city. Then you will get the 3 lines you are taking about. However, you need to get the average width in each group as you only want to show city variation.
To get this plot from raw data: (Using fake data provided by gfgm)
set.seed(100)
leaf <- data.frame(tree.id = rep(1:50, each = 3),
city = rep(c("Jackson", "Augusta", "Atlanta"), each = 50),
plot = rep(c(1, 0), each = 25))
# I'll make the average of width different for each plot
leaf$width <- rnorm(nrow(leaf), leaf$plot, 1)
library(plotly)
library(tidyverse)
leaf %>%
group_by(city,plot) %>%
summarise(avwidth = mean(width, na.rm=T),
avsd = 1.96*sd(width, na.rm=T)/sqrt(25)) %>%
plot_ly(x = ~plot, y = ~avwidth, color= ~city,
type="scatter", mode="markers+lines",
error_y = ~list(array=avsd)
)

Faceting bars in ggplot2

I have this problem: I want to build a stacked bar plot with the faceting capabilities, so I can compare the distribution of frequencies for five common categories, within two different objects, separated according to three groups. I have six objects, five categories and three groups. The problem is that each group has only two different and exclusive objects to plot, but so far I can only produce a plot in which the six objects are plotted across the three groups. This is not optimal, since for each group I have four objects with no data.
Is it possible to plot just two objects for each group with the faceting capabilities?
EDITED
This is my data:
structure(list(Face = 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), .Label = c("LGH002", "LGH003", "LGM009",
"SCM018", "VAH022", "VAM028"), class = "factor"), Race = structure(c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L),
.Label = c("1. Amerindian", "2. White", "3. Mestizo", "4. Other races",
"5. Cannot tell"), class = "factor"), Count = c(19L, 0L, 13L, 8L, 0L, 2L,
7L, 23L, 6L, 2L, 1L, 1L, 29L, 6L, 3L, 29L, 0L, 11L, 0L, 0L, 0L, 38L, 1L, 0L,
1L, 0L, 30L, 9L, 0L, 1L), Density = c(0.475, 0, 0.325, 0.2, 0,
0.05, 0.175, 0.575, 0.15, 0.05, 0.025, 0.025, 0.725, 0.15,
0.075, 0.725, 0, 0.275, 0, 0, 0, 0.95, 0.025, 0, 0.025, 0,
0.75, 0.225, 0, 0.025), School = structure(c(1L, 1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Municipal",
"Private Fee-Paying", "Private-Voucher"), class = "factor")),
.Names =c("Face", "Race", "Count", "Density", "School"),
class = "data.frame", row.names = c(NA, -30L))
This is the code I'm using to build the plot:
P <- ggplot(data = races.df, aes(x = Face, y = Density, fill = Race)) +
geom_bar(stat="identity") +
scale_y_continuous(labels=percent)
P + facet_grid(School ~ ., scales="free") + coord_flip()
As you can imagine, I only want to see the x-values "SCM018" and "LGH002" in "Municipal"; "LGM009" and "LGH003" in "Private-Voucher"; and "VAH022" and "VAM028" in "Private Fee-Paying" (only two objects per group). Is it possible? Any help?
All the best,
Mauricio.

Stat_function range truncated

I am trying to use ggplot2 to plot some data and fitted non-linear curves. I want to use stat_function with nls objects that I have already defined, but the result produces a truncated curve. I have looked at the following pages but so far I haven't found a solution:
R - ggplot2 extrapolated regression lines in linear region
Plotting a large number of custom functions in ggplot in R using stat_function()
http://docs.ggplot2.org/0.9.3/stat_function.html
Equivalent of curve() for ggplot
I am not very experienced with ggplot2, so I apologize if I am missing something simple and I'm grateful for any help. Here my example data:
df=structure(list(Spp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Dugentia", "Eugenia",
"Faramea", "Licania", "Mouriri"), class = "factor"), Tx = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Control",
"Drought"), class = "factor"), no. = structure(c(1L, 3L, 4L,
5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L,
6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L,
5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L,
5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L), .Label = c("1",
"101", "2", "3", "4", "5", "6", "7", "7A", "9"), class = "factor"),
Fv.Fm = c(0.74, 0.702, 0.797, 0.782, 0.769, 0.759, 0.701,
0.805, 0.79, 0.775, 0.763, 0.725, 0.8, 0.786, 0.774, 0.759,
0.664, 0.791, 0.776, 0.758, 0.729, 0.592, 0.757, 0.722, 0.681,
0.66, 0.084, 0.652, 0.633, 0.63, 0.569, 0.259, 0.424, 0.376,
0.432, 0.771, 0.696, 0.685, 0.761, 0.782, 0.772, 0.736, 0.775,
0.784, 0.755, 0.707, 0.746, 0.777, 0.765, 0.705, 0.744, 0.706,
0.55, 0.582, 0.635, 0.615, 0.384, 0.504, 0.513, 0.584, 0.378,
0.328, 0.302), Temp. = c(27L, 27L, 27L, 27L, 27L, 30L, 30L,
30L, 30L, 30L, 35L, 35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L,
40L, 45L, 45L, 45L, 45L, 45L, 48L, 48L, 48L, 48L, 48L, 50L,
50L, 50L, 50L, 50L, 27L, 27L, 27L, 27L, 30L, 30L, 30L, 30L,
35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L, 45L, 45L, 45L, 45L,
48L, 48L, 48L, 48L, 50L, 50L, 50L, 50L)), .Names = c("Spp",
"Tx", "no.", "Fv.Fm", "Temp."), class = "data.frame", row.names = c(NA,
63L))
Here is what I have so far for making the plot with ggplot2:
library(ggplot2)
f1 = ggplot(data = df, aes(x = Temp., y = Fv.Fm, group = Tx) )
f2<-f1+
geom_point(aes(shape=Tx, fill=Tx), size=4)
f3 <- f2 + scale_x_continuous("Temperature (°C)", limits=c(25,55)) +
scale_y_continuous("Fv/Fm", limits = c(0, 1)) +
scale_shape_manual(values=c(24,21)) +
scale_fill_manual(values=c("#4D4D4D","#E6E6E6")) +
theme_bw()
d4 <- f3 + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x = element_text(size = 11),
axis.text.y = element_text(size = 11),
legend.title = element_blank(),
legend.text = element_text(size=12))
All of that works well, but then when I make an nls object and try to use it with stat_function I get a curve that is truncated. I would like to extend the curve down to the x-axis. There is also an error generated.
my.nls<-nls(Fv.Fm~a*(-exp(Temp.)+b), data = df, start=list(a=1*10^-20, b=5*10^21))
new<-d4+stat_function(fun=function(x){coef(my.nls)[1]*(-exp(x)+coef(my.nls)[2])})
new
What I want is something more like the following using basic r functionality, but with all the bells and whistles offered by ggplot2, and ultimately one nls curve for each treatment (Tx).
x<-seq(27, 55, length.out = 200)
y <- predict(my.nls,list(Temp. = x))
plot(df$Temp., df$Fv.Fm)
lines(x,y)
Try this:
df.new=data.frame(x=seq(20,60,0.1),
y=coef(my.nls)[1]*(-exp(seq(20,60,0.1))+coef(my.nls)[2]))
d4+geom_line(aes(Temp.,y,group=NULL),data=df.new)+
xlim(20,60)+ylim(-.1,1)
The problem is that stat_function(...) evaluates the function for x in the range given in your default dataset, df. Since max(df$temp.) = 50, the function is only evaluated out to x=50.
Not a great fit by the way. I'd definitely be looking for a different model.

Resources