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

  • Home
  • SEARCH
  • 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 7720847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:46:21+00:00 2026-06-01T03:46:21+00:00

I am making a graph in ggplot2 consisting of a set of datapoints plotted

  • 0

I am making a graph in ggplot2 consisting of a set of datapoints plotted as points, with the lines predicted by a fitted model overlaid. The general idea of the graph looks something like this:

names <- c(1,1,1,2,2,2,3,3,3)
xvals <- c(1:9)
yvals <- c(1,2,3,10,11,12,15,16,17)
pvals <- c(1.1,2.1,3.1,11,12,13,14,15,16)
ex_data <- data.frame(names,xvals,yvals,pvals)
ex_data$names <- factor(ex_data$names)

graph <- ggplot(data=ex_data, aes(x=xvals, y=yvals, color=names))
print(graph + geom_point() + geom_line(aes(x=xvals, y=pvals)))

As you can see, both the lines and the points are colored by a categorical variable (‘names’ in this case). I would like the legend to contain 2 entries: a dot labeled ‘Data’, and a line labeled ‘Fitted’ (to denote that the dots are real data and the lines are fits). However, I cannot seem to get this to work. The (awesome) guide here is great for formatting, but doesn’t deal with the actual entries, while I have tried the technique here to no avail, i.e.

print(graph + scale_colour_manual("", values=c("green", "blue", "red")) 
+ scale_shape_manual("", values=c(19,NA,NA)) 
+ scale_linetype_manual("",values=c(0,1,1))) 

The main trouble is that, in my actual data, there are >200 different categories for ‘names,’ while I only want the 2 entries I mentioned above in the legend. Doing this with my actual data just produces a meaningless legend that runs off the page, because the legend is trying to be a key for the colors (of which I have way too many).

I’d appreciate any help!

  • 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-01T03:46:23+00:00Added an answer on June 1, 2026 at 3:46 am

    I think this is close to what you want:

    ggplot(ex_data, aes(x=xvals, group=names)) + 
      geom_point(aes(y=yvals, shape='data', linetype='data')) + 
      geom_line(aes(y=pvals, shape='fitted', linetype='fitted')) + 
      scale_shape_manual('', values=c(19, NA)) + 
      scale_linetype_manual('', values=c(0, 1))
    

    The idea is that you specify two aesthetics (linetype and shape) for both lines and points, even though it makes no sense, say, for a point to have a linetype aesthetic. Then you manually map these “nonsense” aesthetics to “null” values (NA and 0 in this case), using a manual scale.

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

Sidebar

Related Questions

In a graph I'm making with gnuplot I draw some grey lines ( set
I am making a pyplot graph with a set of points using: plt.plot([range(0,10)], [dictionary[key]],'bo')
I'm thinking of making an application where at some points a graph is displayed
I am making an interactive graph using reportlab. Each bar in the graph is
Making a word document of our network set-up. We have about 7 servers and
I'm making a line graph on the iphone using core graphics and instead of
How can i draw a line graph in Java Swing . I'm making a
I'm trouble with making graph by networkx. I'm trying to add edges but my
What I do I am making graph of fictitious stock options. The price is
I'm making a graph using PEAR Image_Graph and the bottom coordinates labels are dates.

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.