'R- How to set y-axis individually when using `facet_grid_sc()`
I want to plot the trend of the density (Density.est
) of 3 species (Emar
,Ecos
and Sphyr
) over time (2008
,2009
,2010
) for two protection levels of a protected area (RI
and RP
). As an example of the dataframe I have:
set.seed(123)
Year <- c(2008,2009,2010)
Prot <- c("RI","RP")
species <- c('Emar','Ecos','Sphyr')
df <- data.frame(Year=rep(Year, times = 3, each = 2),
Prot=rep(Prot, times = 9, each = 1),
Sp=rep(species, times = 1, each = 6),
Density.est=rnorm(18,5,2),
Density.se=rnorm(18,5,2)-1.5)
df
Year Prot Sp Density.est Density.se
1 2008 RI Emar 3.879049 4.9027118
2 2008 RP Emar 4.539645 2.5544172
3 2009 RI Emar 8.117417 1.3643526
4 2009 RP Emar 5.141017 3.0640502
5 2010 RI Emar 5.258575 1.4479911
6 2010 RP Emar 8.430130 2.0422175
7 2008 RI Ecos 5.921832 2.2499215
8 2008 RP Ecos 2.469878 0.1266134
9 2009 RI Ecos 3.626294 5.1755741
10 2009 RP Ecos 4.108676 3.8067462
11 2010 RI Ecos 7.448164 1.2237261
12 2010 RP Ecos 5.719628 6.0076298
13 2008 RI Sphyr 5.801543 4.3529284
14 2008 RP Sphyr 5.221365 2.9098570
15 2009 RI Sphyr 3.888318 5.2902513
16 2009 RP Sphyr 8.573826 5.2562670
17 2010 RI Sphyr 5.995701 5.1431622
18 2010 RP Sphyr 1.066766 4.8772805
Using the next code...
Plot1 <- ggplot(df, aes(x=Year, y=Density.est, group=Prot, color=Prot)) +
geom_line() +
geom_point()+
geom_errorbar(aes(ymin=Density.est-Density.se, ymax=Density.est+Density.se), width=.2,
position=position_dodge(0.05)) +
facet_wrap(~ Sp, scales = "free")
Plot1
I obtain this graph:
However, in my real dataframe, for the species Sphyr
, one year I have an extreme value for the density, so I am trying to set y-axis individually for each panel. I found this link solving this problem. I tried to implement what they proposed with the next code:
df$Density.se.min <- df$Density.est-df$Density.se
df$Density.se.max <- df$Density.est+df$Density.se
df.Ecos.min <- min(df$Density.se.min[df$Sp=="Ecos"]) # I calculate the maximum and minimum value of the y-axis for each species.
df.Ecos.max <- max(df$Density.se.max[df$Sp=="Ecos"])
df.Emar.min <- min(df$Density.se.min[df$Sp=="Emar"])
df.Emar.max <- max(df$Density.se.max[df$Sp=="Emar"])
df.Sphyr.min <- min(df$Density.se.min[df$Sp=="Sphyr"])
df.Sphyr.max <- max(df$Density.se.max[df$Sp=="Sphyr"])
scales_y <- list(
`Ecos` = scale_y_continuous(limits = c(df.Ecos.min, df.Ecos.max), breaks = seq(df.Ecos.min, df.Ecos.max, (df.Ecos.max-df.Ecos.min)/10)),
`Emar` = scale_y_continuous(limits = c(df.Emar.min, df.Emar.max), breaks = seq(df.Emar.min, df.Emar.max, (df.Emar.max-df.Emar.min)/10)),
`Sphyr` = scale_y_continuous(limits = c(df.Sphyr.min, df.Sphyr.max), breaks = seq(df.Sphyr.min, df.Sphyr.max, (df.Sphyr.max-df.Sphyr.min)/10))
)
Then, I try to do the plot using this code:
Plot2 <- ggplot(df, aes(x=Year, y=Density.est, group=Prot, color=Prot)) +
geom_line() +
geom_point()+
geom_errorbar(aes(ymin=Density.est-Density.se, ymax=Density.est+Density.se), width=.2,
position=position_dodge(0.05)) +
facet_grid_sc(~ Sp, scales = list(y=scales_y))
Plot2
However, I obtain the error message attempt to select less than one element in get1index
.
Does any one know where is the problem?
Solution 1:[1]
facet_grid_sc()
with "~" does not work. Changing to: + facet_grid_sc(rows= vars(Sp), scales = list(y=scales_y))
seems to work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | A. C. Del Re |