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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:22:47+00:00 2026-06-17T18:22:47+00:00

Summary : I want to choose the colors for a ggplot2() density distribution plot

  • 0

Summary: I want to choose the colors for a ggplot2() density distribution plot without losing the automatically generated legend.

Details: I have a dataframe created with the following code (I realize it is not elegant but I am only learning R):

cands<-scan("human.i.cands.degnums")
non<-scan("human.i.non.degnums")
df<-data.frame(grp=factor(c(rep("1. Candidates", each=length(cands)),
           rep("2. NonCands",each=length(non)))), val=c(cands,non))

I then plot their density distribution like so:

library(ggplot2)
ggplot(df, aes(x=val,color=grp)) + geom_density() 

This produces the following output:

enter image description here

I would like to choose the colors the lines appear in and cannot for the life of me figure out how. I have read various other posts on the site but to no avail. The most relevant are:

  • Changing color of density plots in ggplot2
  • Overlapped density plots in ggplot2

After searching around for a while I have tried:

## This one gives an error
ggplot(df, aes(x=val,colour=c("red","blue"))) + geom_density() 
Error: Aesthetics must either be length one, or the same length as the dataProblems:c("red", "blue")

## This one produces a single, black line
ggplot(df, aes(x=val),colour=c("red","green")) + geom_density() 

The best I’ve come up with is this:

ggplot() + geom_density(aes(x=cands),colour="blue")  + geom_density(aes(x=non),colour="red")

enter image description here
As you can see in the image above, that last command correctly changes the colors of the lines but it removes the legend. I like ggplot2’s legend system. It is nice and simple, I don’t want to have to fiddle about with recreating something that ggplot is clearly capable of doing. On top of which, the syntax is very very ugly. My actual data frame consists of 7 different groups of data. I cannot believe that writing + geom_density(aes(x=FOO),colour="BAR") 7 times is the most elegant way of coding this.

So, if all else fails I will accept with an answer that tells me how to get the legend back on to the 2nd plot. However, if someone can tell me how to do it properly I will be very happy.

  • 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-17T18:22:49+00:00Added an answer on June 17, 2026 at 6:22 pm
    set.seed(45)
    df <- data.frame(x=c(rnorm(100), rnorm(100, mean=2, sd=2)), grp=rep(1:2, each=100))
    ggplot(data = df, aes(x=x, color=factor(grp))) + geom_density() + 
                    scale_color_brewer(palette = "Set1")
    
    ggplot(data = df, aes(x=x, color=factor(grp))) + geom_density() + 
                    scale_color_brewer(palette = "Set3")
    

    gives me same plots with different sets of colors.

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

Sidebar

Related Questions

I have a data frame and I want to learn how the summary generates
Summary I want to write a .png file as quickly as possible, without a
Now I have a game summary table like this: And I want to split
Summary: I want a horizontal row of ImageButtons to scale down evenly to fit
Summary: I want to display a value (in a text box) stored in another
Summary I want to be able to call a JavaScript function from a Flex
Summary: I want the prettiness of Silverlight/WPF in part of my current Winforms application.
This is what i want to do: I want /summary.php to include 5 latest
I want to generate a summary of the files that are in one tree
We are two students who want to use one-class svm for dectection of summary

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.