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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:48:04+00:00 2026-05-31T10:48:04+00:00

I have a melted data frame with your standard id, variable and value columns.

  • 0

I have a melted data frame with your standard id, variable and value columns.
variable has 4 levels.

I want to use ggplot to plot a scatter plot using the values in value from each of the factors

to illustrate

data.frame(id= gl(4,1,labels=paste("id",1:4,sep="")), variable=gl(4,4,labels=LETTERS[1:4]),value=rnorm(16))

        id variable        value
1  id1        A -0.494270766
2  id2        A  0.189400188
3  id3        A -0.550961030
4  id4        A -1.046945450
5  id1        B -0.525552660
6  id2        B -0.293601677
7  id3        B  0.009664513
8  id4        B -0.214687215
9  id1        C  1.253551926
10 id2        C -1.241847326
11 id3        C -0.307036508
12 id4        C -0.228632605
13 id1        D -1.683798512
14 id2        D -0.419295267
15 id3        D -0.154469178
16 id4        D -0.763460558

I want to produce ggplot scatter plots for each pair of variable A vs B, A vs C, A vs D, B vs C, and so on, and then ass smoothers to them afterwards.

Cheers,
Davy

  • 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-31T10:48:06+00:00Added an answer on May 31, 2026 at 10:48 am

    Here’s a slightly modified version of plotmatrix in ggplot2 that does this:

    dat <- data.frame(id= gl(4,1,labels=paste("id",1:4,sep="")), variable=gl(4,4,labels=LETTERS[1:4]),value=rnorm(16))
    
    require(reshape2)
    dat <- dcast(dat,id~variable)
    
    plotmatrix <- function (data, mapping = aes(), colour = "black") 
    {
        grid <- expand.grid(x = 1:ncol(data), y = 1:ncol(data))
        grid <- subset(grid, x != y)
        all <- do.call("rbind", lapply(1:nrow(grid), function(i) {
            xcol <- grid[i, "x"]
            ycol <- grid[i, "y"]
            data.frame(xvar = names(data)[ycol], yvar = names(data)[xcol], 
                x = data[, xcol], y = data[, ycol], data)
        }))
        all$xvar <- factor(all$xvar, levels = names(data))
        all$yvar <- factor(all$yvar, levels = names(data))
        densities <- do.call("rbind", lapply(1:ncol(data), function(i) {
            data.frame(xvar = names(data)[i], yvar = names(data)[i], 
                x = data[, i])
        }))
        densities$xvar <- factor(densities$xvar, levels = names(data))
        densities$yvar <- factor(densities$yvar, levels = names(data))
        mapping <- defaults(mapping, aes_string(x = "x", y = "y"))
        class(mapping) <- "uneval"
        ggplot(all, mapping) + 
            facet_grid(xvar ~ yvar, scales = "free") + 
            geom_point(colour = colour, na.rm = TRUE) + 
            stat_density(aes(x = x,y = ..scaled.. * diff(range(x)) + min(x)), 
                data = densities,position = "identity", colour = "grey20", geom = "line") + 
            geom_smooth(se = FALSE,method = "lm",colour = "blue")
    }
    
    plotmatrix(dat[,-1])
    

    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 have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
I have a melted data.frame, dput(x), below: ## dput(x) x <- structure(list(variable = structure(c(1L,
I often run into an issue where I have a data frame that has
I have a data.frame (which I melted using the melt function), from which I
I have $('#hidden-data').find('#' + $(this).attr('id')).html(currdata); var hiddata = $('#hidden-data').find('#' + $(this).attr('id')).html(); I want to
I have a melted dataset containing a column value which represent an absolute number
My brain has melted due to several weeks of 14-hour days. I have a
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have finally got a responsive site working (of a fashion). What I want to
Have a linking (or ref) table which has a dual primary key. Need to

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.