library(car)
library(performance)
library(sjPlot)
library(ggplot2)


### DATA IMPORT #################################################
data0=read.table("Home_range_RMR.csv",header=TRUE,sep=",",dec=".", stringsAsFactors=T) 


##model 1 -  RMR corrected by body mass
a1 = glm(area ~
           RMR+
           body_mass,
         data = data0)

Anova(a1, type = "II")
summary(a1)
check_model(a1)
AIC(a1)
plot11=plot_model(a1, terms=c("RMR"), type="pred", show.scatter=T, dot.size = 5, remove.estimates = T, show.data = T)
plot11



#Fig 1
plot_1a=plot_model(a1, terms=c("RMR[all]"), type="pred", 
                   dot.size = 2.5, alpha=0.35, remove.estimates = F,  show.data = T,
                   title="(a)")+
  theme_bw()+                                                                          #remove grid
  theme(plot.margin = margin(5, 5, 5, 5))+    # margin clockwise, starting at top
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),           #remove grid
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+   #remove grid
  theme(plot.title = element_text(size=20))+
  theme(plot.title = element_text(vjust = - 6))+
  theme(plot.title = element_text(hjust = + 0.02))+ 
  theme(axis.title = element_text(size = 15))+
  theme(axis.title.y = element_text(vjust = +2.5))+
  theme(axis.title.x = element_text(vjust = -0.25))+
  theme(legend.position="none")+
  theme(legend.title = element_text(size = 14))+
  theme(axis.text = element_text(size = 13)) +                                           
  theme(axis.title.y = ggtext::element_markdown())+
  scale_x_continuous(name="RMR  [ml/h/g]", breaks=seq(0.4,2.2,0.2), limits=c(0.4,2.2))+
  scale_y_continuous(name="Area [m2]", breaks=seq(0,1000,100), limits=c(0,1000))+
  geom_line(key_glyph = "rect", alpha=0.5)   #connecting line
plot_1a


plot_1b=plot_model(a1, terms=c("body_mass[all]"), type="pred", 
                   dot.size = 2.5, alpha=0.35, remove.estimates = F,  show.data = T,
                   title="(b)")+
  theme_bw()+                                                                          #remove grid
  theme(plot.margin = margin(5, 5, 5, 5))+    # margin clockwise, starting at top
  theme(panel.border = element_blank(), panel.grid.major = element_blank(),           #remove grid
        panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+   #remove grid
  theme(plot.title = element_text(size=20))+
  theme(plot.title = element_text(vjust = - 6))+
  theme(plot.title = element_text(hjust = + 0.02))+ 
  theme(axis.title = element_text(size = 15))+
  theme(axis.title.y = element_text(vjust = +2.5))+
  theme(axis.title.x = element_text(vjust = -0.25))+
  theme(legend.position="none")+
  theme(legend.title = element_text(size = 14))+
  theme(axis.text = element_text(size = 13)) +                                           
  theme(axis.title.y = ggtext::element_markdown())+
  scale_x_continuous(name="body mass [g]", breaks=seq(60,150,10), limits=c(60,150))+
  scale_y_continuous(name="Area [m2]", breaks=seq(0,1000,100), limits=c(0,1000))+
  geom_line(key_glyph = "rect", alpha=0.5)   #connecting line

plot_1b



## combine to one figure 

library(ggpubr)


Figure.1=ggarrange(plot_1a, plot_1b, ncol = 2, nrow = 1)

ggsave(plot = last_plot(), filename="Fig.1.png", width=10,height=5, device='png', dpi=600)

Figure.1
