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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:09:58+00:00 2026-05-31T20:09:58+00:00

Another ggplot legend question! I have a dataset of the form test <- data.frame(

  • 0

Another ggplot legend question!

I have a dataset of the form

test <- data.frame(
  cond = factor(rep(c("A", "B"), each=200)), 
  value = c(rnorm(200), rnorm(200, mean=0.8))
)

So two groups and some values and I want to plot the density. I also want to add a line indicating the mean for each group to the plot so I:

test.cdf <- ddply(test, .(cond), summarise, value.mean=mean(value))

Then in ggplot call:

ggplot(test, aes(value, fill=cond)) + 
  geom_density(alpha=0.5) + 
  labs(x='Energy', y='Density', fill='Group') + 
  opts(
    panel.background=theme_blank(), 
    panel.grid.major=theme_blank(), 
    panel.grid.minor=theme_blank(), 
    panel.border=theme_blank(), 
    axis.line=theme_segment()
  ) + 
  geom_vline(data=test.cdf, aes(xintercept=value.mean, colour=cond), 
    linetype='dashed', size=1)

If you run the above code, you get a legend indicating each group, but also one for the mean indicator vline. My question is how can I get rid of the legend for the geom_vline()?

  • 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-05-31T20:09:59+00:00Added an answer on May 31, 2026 at 8:09 pm

    Depending on the version of ggplot2 you are using you get this problem. Using ggplot2 vs 0.9.0 on R2.14.1 I get this graph:

    enter image description here

    which does not include the legend for the vline. In this version of ggplot2 you can tweak the occurence of the legend using show_guide:

    ggplot(test, aes(value, fill=cond)) + 
      geom_density(alpha=0.5) + 
      labs(x='Energy', y='Density', fill='Group') + 
      opts(
        panel.background=theme_blank(), 
        panel.grid.major=theme_blank(), 
        panel.grid.minor=theme_blank(), 
        panel.border=theme_blank(), 
        axis.line=theme_segment()
      ) + 
      geom_vline(data=test.cdf, aes(xintercept=value.mean, colour=cond), 
        linetype='dashed', size=1, show_guide = TRUE)
    

    enter image description here

    which reproduces your problem. Default, show_guide = FALSE. In older versions, you can add legend = FALSE to geom_vline in order to omit the legend. Adding legend = FALSE still works still works in the current version, but it throws a warning:

    Warning message:
    In get(x, envir = this, inherits = inh)(this, ...) :
      "legend" argument in geom_XXX and stat_XXX is deprecated. Use show_guide = TRUE or show_guide = FALSE for display or suppress the guide display.
    

    I would recommend upgrading ggplot2.

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

Sidebar

Related Questions

Another basic Rails question: I have a database table that needs to contain references
I have simple histogram, but can't adjust the binwidth: qplot(factor(size_class),data=mydf,geom=histogram,binwidth = 0.01) size_class is
How can I order the levels of a factor in one data.frame column according
Another question in large scale programming: I have a job queue with time stamps
Another jQuery noob question - what am I doing wrong?? I have some HTML
another list-dictionary question. I have a dict as follows with unit names and testnames
Another question by a newbie. I have a php variable that queries the database
Another question by a newbie. I have a php variable that queries the database
another question of mine. I have created a screen that consists of a Spinner
Another newbie question here, I have the following Select: SELECT DATEADD(dd, 0, DATEDIFF(dd, 0,

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.