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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:00:51+00:00 2026-06-08T21:00:51+00:00

Hi can I add an additional legend to a ggplot. Like the following code

  • 0

Hi can I add an additional legend to a ggplot.
Like
the following code

d <- melt(as.matrix(data.frame(y1=1/(1:10),y2=1/(10:1))))
ggplot(d, aes(x=Var1, y=value,fill=Var2)) + geom_bar(stat="identity",position='dodge')

This generates a nice legend containing the name of my dataframe.
But is it possible to put in an extralegend, that contains some extra information generated from the data.

In the standard R, I would add the additional legend like

d<-data.frame(y1=1/(1:10),y2=2*1/(10:1))
barplot(t(d),beside=T)
legend("top",paste("sums:",apply(d,2,sum)))

Thanks

  • 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-08T21:00:53+00:00Added an answer on June 8, 2026 at 9:00 pm

    This seems to work for me.

        plot.new()
        d <- melt(as.matrix(data.frame(y1=1/(1:10),y2=1/(10:1))))
        ggplot(d, aes(x=Var1, y=value,fill=Var2)) + 
        geom_bar(stat="identity",position='dodge')
    

    then the exciting stuff.

      legend('top',paste("sums:",tapply(d$value,d$Var2,sum)))
    

    I changed the apply statement to work on the molten data.

    I am not aware of a ggplot solution, but I would love to see one.

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

Sidebar

Related Questions

When compiling an erlang file with erlc I can add additional include directories like
How can I add additional attributes to my select menu option tags? Like this:
How can I add additional, custom information to my objective-c methods and properties? I
How can I add an additional icon to the Name/Lable of app, just the
Is there a way I can add additional buttons with click event handlers to
How I can add an additional condition for a certain keyboard key, to a
I'm after a means by which I can add additional commands to a text
With ZedGraph, I can add multiple YAxis by doing the following: YAxis yAxis3 =
I have 4 columns called ID, EQUIPID, VERSION, DESCRIPTION. Users can add additional columns
I want to add additional stuff to the parent class. I can't modify the

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.