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 9024237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:57:22+00:00 2026-06-16T05:57:22+00:00

I’m trying to do a plot that has most data points drawn normally, but

  • 0

I’m trying to do a plot that has most data points drawn normally, but one set of data points with a different sized symbol. I want the legend to show the same: most points shown normally, but the exception drawn with a different sized symbol. Here is a short bit of code:

library(ggplot2)
x = c(1,2,1,2,3)
y = c(1,2,3,4,3)
vendor = c("x", "x", "y", "y", "z")
df = data.frame(x,y,vendor)

p <- ggplot(df) +
     aes_string(x="x", y="y", color="vendor") +
     geom_point(size=3, data=subset(df, vendor!="z")) +
     geom_point(size=5, data=subset(df, vendor=="z"))
ggsave("foo.pdf")

The problem is that in the resulting legend, all points are drawn with the larger (size=5) symbol, not just those with vendor z. I want vendor z drawn with the larger point in the legend, and the others drawn with size=3.

(Bonus question: What I really want is a larger thick outlined symbol: instead of a circle, I want a donut. I realize that shape=2 will draw an outlined circle, but it is very thin. I’d rather have a thicker outlined circle. I want to do the same with a triangle. Any easy way to do this?)

Maybe I applied it wrong, but following this advice:

ggplot2: Making changes to symbols in the legend

with the addition of the “guides” line did not help:

guides(size = guide_legend(override.aes = list(shape = 1)))

i.e. same output, with size=5 symbols for all three vendors in the legend.

EDITED: Fantastic answer, which I quickly implemented. Now I’ve added lines:

library(ggplot2)
x = c(1,2,1,2,3)
y = c(1,2,3,4,3)
vendor = c("x", "x", "y", "y", "z")
df = data.frame(x,y,vendor)

df$vendor_z <- df$vendor=="z"     # create a new column 

ggplot(df) +
  aes_string(x = "x", y = "y", color = "vendor", size = "vendor_z") +
  geom_point() +
  geom_line(size=1.5) +   # this is the only difference
  scale_size_manual(values = c(3, 5), guide = FALSE) 
  guides(colour = guide_legend(override.aes = list(size = c(3, 3, 5))))

ggsave("foo.pdf")

and now the size of the legend is back down to 3 again for all dots, including the ones with vendor z. Any ideas on how to fix this?

  • 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-16T05:57:23+00:00Added an answer on June 16, 2026 at 5:57 am

    The size is not applied to the legend since size is outside aes_string. Furtermore, the work with ggplot will be much easier if you create an additional column indicating whether vendor == "z".

    Here’s a solution for part 1:

    df$vendor_z <- df$vendor=="z"     # create a new column 
    
    ggplot(df) +
      aes_string(x = "x", y = "y", color = "vendor", size = "vendor_z") +
      geom_point() +
      scale_size_manual(values = c(3, 5), guide = FALSE) + 
      guides(colour = guide_legend(override.aes = list(size = c(3, 3, 5))))
    

    Note that vendor_z is as argument of aes_string. This will tell ggplot to create a legend for the size characteristic. In the function scale_size_manual, the values for size are set. Furthermore, guide = FALSE avoids a second legend for size only. Finally, the size values are applied to the color legend.

    enter image description here

    Part2: a “donut” symbol

    The size of the lines for circles cannot be modified in ggplot. Here is a workaround:

    ggplot(df) +
      aes_string(x = "x", y = "y", color = "vendor", size = "vendor_z") +
      geom_point() +
      geom_point(data = df[df$vendor_z, ], aes(x = x, y = y),
                 size = 3, shape = 21, fill = "white", show_guide = FALSE) +
      scale_size_manual(values = c(3, 5), guide = FALSE) + 
      guides(colour = guide_legend(override.aes = list(size = c(3, 3, 5))))
    

    Here, a single point is drawn using geom_point and a subset of the data (df[df$vendor_z, ]). I chose a size of 3 since this is the value of the smaller circles. The shape 21 is a circle for which a fill colour could be specified. Finally, show_guide = FALSE avoids that the legend characteristics are overwritten by the new shape.

    enter image description here

    Edit: part 3: Add lines

    You could suppress the legend for geom_line with the argument show_guide = FALSE:

    ggplot(df) +
      aes_string(x = "x", y = "y", color = "vendor", size = "vendor_z") +
      geom_point() +
      geom_line(size=1.5, show_guide = FALSE) +   # this is the only difference
      scale_size_manual(values = c(3, 5), guide = FALSE) +
      guides(colour = guide_legend(override.aes = list(size = c(3, 3, 5))))
    

    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'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is

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.