Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:17:30+00:00 2026-06-17T09:17:30+00:00

I would like to produce a scatter plot with ggplot2, which contains both a

  • 0

I would like to produce a scatter plot with ggplot2, which contains both a regression line through all data points (regardless which group they are from), but at the same time varies the shape of the markers by the grouping variable. The code below produces the group markers, but comes up with TWO regression lines, one for each group.

#model=lm(df, ParamY~ParamX)
p1<-ggplot(df,aes(x=ParamX,y=ParamY,shape=group)) + geom_point() + stat_smooth(method=lm)

How can I program that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:17:31+00:00Added an answer on June 17, 2026 at 9:17 am

    you shouldn’t have to redo your full aes in the geom_point and add another layer, just move the shape aes to the geom_point call:

    df <- data.frame(x=1:10,y=1:100+5,grouping = c(rep("a",10),rep("b",10)))
    ggplot(df,aes(x=x,y=y)) + 
    geom_point(aes(shape=grouping)) + 
    stat_smooth(method=lm)
    

    enter image description here

    EDIT:

    To help with your comment:

    because annotate can end up, for me anyway, with the same labels on each facet. I like to make a mini data.frame that has my variable for faceting and the facet levels with another column representing the labels I want to use. In this case the label data frame is called dfalbs.

    Then use this to label data frame to label the facets individually e.g.

    df <- data.frame(x=1:10,y=1:10,grouping =   
                      c(rep("a",5),rep("b",5)),faceting=c(rep(c("oneR2","twoR2"),5)))
    
    
    dflabs <- data.frame(faceting=c("oneR2","twoR2"),posx=c(7.5,7.5),posy=c(2.5,2.5))
    
    ggplot(df,aes(x=x,y=y,group=faceting)) + 
           geom_point(aes(shape=grouping),size=5) + 
           stat_smooth(method=lm) +
           facet_wrap( ~ faceting) +
           geom_text(data=dflabs,aes(x=posx,y=posy,label=faceting))
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to produce a character list of all of the first letters
I would like to produce a spider (aka radar/star) plot using Gnuplot where different
This code produces a POST request: urllib2.urlopen(http://somedomain.com/, data) I would like to produce a
I would like to produce a text file which includes a mixture of strings
Possible Duplicate: Streamgraphs in R? I would like to produce a plot like this
I would like to produce 2 builds of my Android app: one build which
I would like to plot a simple scatter graph in MATLAB, with marker colours
I would like to produce an android user interface which allows the user to
I would like to produce C# helper files from the KML2.2 xml schema using
I would like to produce a binary zip distribution of my project that would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.