scatter plot with random factor in mixed model

76 views
Skip to first unread message

anvar sour

unread,
Jun 26, 2017, 7:28:32 AM6/26/17
to ggplot2

Dear all,

Hi,

I used mixed model regression (LMM) with some random factors (Sites, Disturbances, Plant community types) to evaluate how aboveground biomass (AGB) changes is determined by biotic (Plant coverage, species richness and evenness) and some abiotic factors across 35 sites, 5 disturbance intensities and 18 Plant community types. now I want to show bivariate relationship between aboveground biomass (AGB) with plant richness (S), Plant coverage (C), Evenness (E), and some environmental factors. as mentioned I have 35 sites (as a random factor), that in bivariate relationships (for example between AGB and C, or AGB with C) I want to show significant sites (among 35 site) with continuous lines and non-significant sites with dashed lines in single graph or if be in different color types. But I do not know how I can do that by ggplot2.

I attached two sample graphs that are same with my idea.  

 Thanks in advance for any help.

Best Regards,

Anvar,

example1.bmp
example2.jpg

Crump, Ron

unread,
Jun 26, 2017, 7:44:46 AM6/26/17
to anvar sour, ggplot2
Dear Anvar,
You could really do with providing a minimal example for us to work with,
most of what you have written is not really relevant to the plotting
problem. Of more help would be some example data and the ggplot code you
have got so far.

Look at ?geom_line, particularly the linetype aesthetic (set the linotype
to be controlled by a TRUE/FALSE is/isn't significant) and maybe
?scale_linetype_discrete or ?scale_linetype_manual to set specific line
types for the signif/non-signif lines.

Ron.



anvar sour

unread,
Jun 26, 2017, 8:47:58 AM6/26/17
to ggplot2
Dear Ron,
thanks for your attention.
I used following R codes and I get attached graphs. 
p4 <- ggplot(Dataset,aes(x=C,y=B,colour=site, lty=site))+geom_point()+geom_smooth(method="lm", se=FALSE, color="black")+theme_classic()  #### for site effect
p5 <- ggplot(Dataset,aes(x=C,y=B,colour=D, lty=D))+geom_point()+ geom_smooth(method="lm", se=FALSE, color="black")+theme_classic() #### for Disturbance effect

but I have two problems:
1) I have 35 sites, but in site effect graph (attached graph) I do not see 35 fitted regression lines.
2) I know that bivariate relationships between biomass (B) and plant cover (C) in which sites was significant or non-significant, but I do not know how to write that in above R codes. sites (1,2,4,9,10,12,13,16,19,20,24,26,28,30,31,34,35) are significant and sites (3,5,6,7,8,11,15,17,18,21,22,23,25,27,29,32,33) are non-significant.
Thanks in advance for your help.
Best Regards,
Anvar,
site effect2.jpeg
disturbance effect2.jpeg

Hadley Wickham

unread,
Jun 26, 2017, 9:21:34 AM6/26/17
to anvar sour, ggplot2
Instead of using geom_smooth(), plot the predictions from your model.

Hadley
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://hadley.nz

anvar sour

unread,
Jun 26, 2017, 12:12:36 PM6/26/17
to ggplot2, anvar...@gmail.com
Dear Hadley,
thanks.
I used following codes for disturbance intensity and received good results:
p4 <- ggplot(Dataset, aes(x=C,y=B,colour=D, lty=D))+geom_point()+theme_classic()+ geom_smooth(method="lm", se=FALSE)+scale_linetype_manual(values=c(A="solid",B="dashed", C="dashed", D="solid",E="solid"))+scale_color_manual(values=c(A="black",
B="grey",C="grey", D="black",E="black")) ###### for disturbance effect
I have 5 disturbance levels, 
but I have 35 sites, I can not repeat same codes for sites effects, R software has a limitation for more number classifications.
how to write above R codes for analysis site effects?
Best Regrads,
Anvar,
disturbance effect3.jpeg

anvar sour

unread,
Jun 26, 2017, 3:34:28 PM6/26/17
to ggplot2, anvar...@gmail.com
Dear Hadley,
thanks.
I used following codes for disturbance intensity and received good results (attached graph disturbance effect 3):
p4 <- ggplot(Dataset, aes(x=C,y=B,colour=D, lty=D))+geom_point()+theme_classic()+ geom_smooth(method="lm", se=FALSE)+scale_linetype_manual(values=c(A="solid",B="dashed", C="dashed", D="solid",E="solid"))+scale_color_manual(values=c(A="black",
B="grey",C="grey", D="black",E="black")) ###### for disturbance effect
I have 5 disturbance levels.
 
but I have 35 sites, I can not repeat same codes for sites effects, R software has a limitation for more number classifications.
 following you can see R codes for site effect. how to write following R codes for analysis site effects? I want to classify sites (35 sites) into 2 category and I want to show all regression lines based on significant and non-significant (2 line types) not based on 35 line types. I attached site effects analysis graph from following codes (graph site effects3)
p5 <- ggplot(Dataset,aes(x=C,y=B,colour=site))+theme_classic()+geom_point()+ 
  geom_smooth(method="lm", span=6, se=F)+scale_linetype()+ xlab("ln (Plant Coverage, %)") +
  ylab("ln (AGB g m -2)") ##### for site effect

thanks in advance for your help.
Best Regards,
Anvar,

On Monday, June 26, 2017 at 5:51:34 PM UTC+4:30, Hadley Wickham wrote:
disturbance effect3.jpeg
site effect3.jpeg

Brandon Hurr

unread,
Jun 26, 2017, 3:36:58 PM6/26/17
to anvar sour, ggplot2
You should really give us a reproducible example. It's really hard to help. Also, from what I can tell you did not follow Hadley's advice and plot from the model object.

anvar sour

unread,
Jun 27, 2017, 2:51:36 AM6/27/17
to ggplot2, anvar...@gmail.com
Dear Brandon Hurr,
for example bivariate relationships between biomass (B) and plant cover (C) in  sites (1,2,4,9,10,12,13,16,19,20,24,26,28,30,31,34,35) are significant and sites (3,5,6,7,8,11,15,17,18,21,22,23,25,27,29,32,33) are non-significant. I want to show significant sites with solid and non-significant sites with dashed line type.
I used following codes but I can not show sites based on two category (significant  and non-significant).
p5 <- ggplot(Dataset,aes(x=C,y=B,colour=site))+theme_classic()+geom_point()+ 
  geom_smooth(method="lm", span=6, se=F)+scale_linetype()+ xlab("ln (Plant Coverage, %)") +
  ylab("ln (AGB g m -2)") #### for site effect as a random factor
I attached my Dataset, please see attached file.
Thanks for any help.
Best Regards,
Anvar,
Dataset.xls

anvar sour

unread,
Jun 29, 2017, 3:16:02 PM6/29/17
to ggplot2, anvar...@gmail.com
Dear all,
Hi everybody,
I am trying to scatter plot between biomass (B) and Coverage (C) across 35 sites, I want to fit regression lines in each site separately. 
I want to show regression lines in two categories (significant and non significant, significant with solid and non significant in dashed type) but points be the same for all 35 sites.
here is my codes: in following codes grey color related to the non  significant regression and black color for significant regression.

p22 <- ggplot(Dataset,aes(x=C,y=B,colour=site))+theme_classic()+geom_point(aes(size=1.6))+xlab("ln (Plant Coverage, %)") +
  ylab("ln (AGB g m -2)")+geom_smooth(method="lm", se=FALSE, size=1.8)+ scale_color_manual(values=c(S1="black",S2="grey", S3="black", S4="black",
S5="black", S6="black", S7="grey", S8="black", S9="black", S10="black", S11="black", S12="grey",
S13="black", S14="black", S15="black", S16="black", S17="black", S18="black", S19="black", S20="black", 
S21="black", S22="black", S23="black", S24="black",S25="black", S26="black", S27="black", S28="black", S29="black",
S30="black", S31="black", S32="black", S33="black", S34="black", S35="black"))+ theme(axis.text=element_text(size=32),axis.title=element_text(size=32))+
  theme(axis.text =  element_text(size=32, colour = "black"), (axis.ticks=element_line(colour = "black", size=2)),
        axis.text.x  = element_text(size=32))+guides(fill=guide_legend(nrow=3))+guides(fill=guide_legend(nrow=3))+theme(plot.title = element_text(size =18) , legend.title=element_text(size=32) , legend.text=element_text(size=26))+ theme(axis.line.x = element_line(color="black", size = 1.5), axis.line.y = element_line(color="black", size = 1.5))

Thanks for any help.
Best Regards,
Anva

scatter plot.jpeg

Crump, Ron

unread,
Jun 30, 2017, 3:34:11 AM6/30/17
to anvar sour, ggplot2
Dear Anvar,

>I am trying to scatter plot between biomass (B) and Coverage (C) across
>35 sites, I want to fit regression lines in each site separately.
>I want to show regression lines in two categories (significant and non
>significant, significant with solid and non significant in dashed type)
>but points be the same for all 35 sites.
>here is my codes: in following codes grey color related to the non
>significant regression and black color for significant regression.

What was wrong with Hadley's previous suggestion of "Instead of using
geom_smooth(), plot the predictions from your model"?

If you do this you wouldn't need to specify those 35 colours (although
I'm not sure that you need to anyway, as I think was also pointed out
elsewhere).

But, and this is a big but, if the model you are using outside of this
ggplot call does have a random factor in it (I presume site is the factor
fitted as a random effect), then this geom_smooth with lm is not a plot of
this model (as site would be fixed).

However, to stop the colour being applied to the geom_point as well as the
geom_line, take the colour=site out of the ggplot aesthetic and put it in
the geom_smooth aesthetic only. And as you are specifying a single point
size rather than mapping a variable to size, the size=1.6 should not be
inside an aes call for geom_point.

Ron.

Reply all
Reply to author
Forward
0 new messages