0

I would like to draw a plot like the figure 2, but I am not able to plot the trend lines for each type of habitat, but only a general line.

Does someone knows which command should I write to get that?

In addition, I would like also to specify the scale of the y-axis (0, 0.5, 1, 1.5 ...).

library(ggplot2)
pd <- position_dodge(0.1)
ggplot(dados, aes(x=sp, y=abund, colour=habitat, group = 1)) + 
  geom_errorbar(aes(ymin=abund-dv, ymax=abund+dv), colour="black", width=.1, position=pd) +
 geom_point(position=pd, size=4)+
  geom_smooth(method = "lm", aes(fill=habitat))+
  scale_fill_brewer(palette="Dark2") + 
  scale_colour_brewer(palette="Dark2")+
  theme_bw()
sp  body_mass   habitat abund   dv
AAOtol_cras 1206.61 FO  -0.021666667    -0.010833333
ABMiop_tal  1248.86 FO  -0.021666667    -0.010833333
ACCLep_cap  1500    FO  -0.041666667    -0.102062073
ADGenet_gen 1756.17 FO  -0.047619048    -0.162590417
BAThry_swin 4000    FO  -0.382978723    -0.28152599
BDPhil_mont 6000    FO  -0.725806452    -0.377581583
CALept_serv 11999.96    FO  -0.433333333    -0.287270381
AAOtol_cras 1206.61 S   -0.021666667    -0.010833333
ABMiop_tal  1248.86 S   -0.021666667    -0.010833333
ACCLep_cap  1500    S   -0.047619048    -0.109108945
ADGenet_gen 1756.17 S   -0.236842105    -0.118421053
BAThry_swin 4000    S   -0.475  -0.29930475
BDPhil_mont 6000    S   -0.944444444    -0.446325385
CALept_serv 11999.96    S   -0.590909091    -0.311003287

Current Plot:

enter image description here

Desired Plot:

enter image description here

M--
  • 25,431
  • 8
  • 61
  • 93
Fran Braga
  • 197
  • 2
  • 14
  • **For Specifying the breaks**, look here: [R ggplot Y axis breaks settings](https://stackoverflow.com/questions/22818899/r-ggplot-y-axis-breaks-settings). – M-- Jun 19 '19 at 14:13

1 Answers1

1

You need to use the group in aes() with the variable habitat so it will be passed over to geom_smooth as well.

library(ggplot2)

ggplot(dados, aes(x=sp, y=abund, colour=habitat, group = habitat)) + 
  geom_errorbar(aes(ymin=abund-dv, ymax=abund+dv), 
                colour="black", width=.1, position=position_dodge(0.1)) +
  geom_point(position=position_dodge(0.1), size=4) +
  geom_smooth(method = "lm", aes(color=habitat)) +
  scale_fill_brewer(palette="Dark2") + 
  scale_colour_brewer(palette="Dark2") +
  theme_bw()

You can use se=FALSE in geom_smooth to get rid of grey confidence intervals or use fill instead of/with color to get them as the same color of your points.

Data:

dados <- read.table(text="sp    body_mass   habitat abund   dv
AAOtol_cras 1206.61 FO  -0.021666667    -0.010833333
ABMiop_tal  1248.86 FO  -0.021666667    -0.010833333
ACCLep_cap  1500    FO  -0.041666667    -0.102062073
ADGenet_gen 1756.17 FO  -0.047619048    -0.162590417
BAThry_swin 4000    FO  -0.382978723    -0.28152599
BDPhil_mont 6000    FO  -0.725806452    -0.377581583
CALept_serv 11999.96    FO  -0.433333333    -0.287270381
AAOtol_cras 1206.61 S   -0.021666667    -0.010833333
ABMiop_tal  1248.86 S   -0.021666667    -0.010833333
ACCLep_cap  1500    S   -0.047619048    -0.109108945
ADGenet_gen 1756.17 S   -0.236842105    -0.118421053
BAThry_swin 4000    S   -0.475  -0.29930475
BDPhil_mont 6000    S   -0.944444444    -0.446325385
CALept_serv 11999.96    S   -0.590909091    -0.311003287", header=T)
M--
  • 25,431
  • 8
  • 61
  • 93