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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:45:15+00:00 2026-05-26T18:45:15+00:00

I am trying to output multiple density plot from a function, by dividing the

  • 0

I am trying to output multiple density plot from a function, by dividing the dataframe into pieces such that separate density for each level of a factor for corresponding yvar.

set.seed(1234)
Aa = c(rnorm(40000, 50, 10))
Bb = c(rnorm(4000, 70, 10))
Cc = c(rnorm(400, 75, 10))
Dd = c(rnorm(40, 80, 10))
yvar = c(Aa, Bb, Cc, Dd)
    gen <- c(rep("Aa", length(Aa)),rep("Bb", length(Bb)), rep("Cc", length(Cc)),
  rep("Dd", length(Dd)))
mydf <- data.frame(gen, yvar)

minyvar <- min(yvar)
maxyvar <- max(yvar)

par(mfrow = c(length(levels(mydf$gen)),1))

plotdensity <- function (xf, minyvar, maxyvar){
plot(density(xf), xlim=c(minyvar, maxyvar), main = paste (names(xf), 
  "distribution", sep = ""))
dens <- density(xf)
x1 <- min(which(dens$x >= quantile(xf, .80)))
x2 <- max(which(dens$x <  max(dens$x)))
with(dens, polygon(x=c(x[c(x1,x1:x2,x2)]), y= c(0, y[x1:x2], 0), col="blu4"))
abline(v= mean(xf),  col = "black", lty = 1, lwd =2)
}

require(plyr)
ddply(mydf, .(mydf$gen), plotdensity, yvar, minyvar, maxyvar)

Error in .fun(piece, ...) : unused argument(s) (111.544494112914)

My specific expectation are each plot is named by name of level for example Aa, Bb, Cc, Dd
Arrangement of the graphs see the parameter set, so that we compare density changes and means. compact – Low space between the graphs.

Help appreciated.

Edits:
The following graphs are individually produced, although I want to develop a function that can be applicable to x level for a factor.

enter image description here

  • 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-26T18:45:16+00:00Added an answer on May 26, 2026 at 6:45 pm

    I see that @Andrie just beat me to most of this. I’m still going to post my answer, since filling only certain quantiles of the distribution requires a slightly different approach.

    set.seed(1234)
    Aa = c(rnorm(40000, 50, 10))
    Bb = c(rnorm(4000, 70, 10))
    Cc = c(rnorm(400, 75, 10))
    Dd = c(rnorm(40, 80, 10))
    yvar = c(Aa, Bb, Cc, Dd)
    gen <- c(rep("Aa", length(Aa)),rep("Bb", length(Bb)), rep("Cc", length(Cc)),
                rep("Dd", length(Dd)))
    mydf <- data.frame(grp = gen,x = c(Aa,Bb,Cc,Dd))
    
    #Calculate the densities and an indicator for the desire quantile
    # for later use in subsetting
    mydf <- ddply(mydf,.(grp),.fun = function(x){
        tmp <- density(x$x)
        x1 <- tmp$x
        y1 <- tmp$y
        q80 <- x1 >= quantile(x$x,0.8)
        data.frame(x=x1,y=y1,q80=q80)
    })
    
    #Separate data frame for the means
    mydfMean <- ddply(mydf,.(grp),summarise,mn = mean(x))
    
    ggplot(mydf,aes(x = x)) + 
        facet_wrap(~grp) +
        geom_line(aes(y = y)) + 
        geom_ribbon(data = subset(mydf,q80),aes(ymax = y),ymin = 0, fill = "black") +
        geom_vline(data = mydfMean,aes(xintercept = mn),colour = "black")
    

    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 trying to pass multiple values from PHP into my javascript function. The function
Trying to write such function. It must divide text into multiple columns and the
I am trying to output values of each rows from a DataSet : for
I'm trying to output unicode string into RTF format. (using c# and winforms) From
I'm trying to create a loop that will output multiple where statements for a
I am trying to combine multiple commands into a single output. #!/bin/bash x=$(date +%Y)
I have multiple preg match expressions, and I'm trying to use each to output
I am trying to select multiple rows from the database and then separate the
I am trying to output all the products from our Magento shop - the
I'm trying to output each line in a csv file, and it seems like

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.