How can I place multiple unrelated graphs on the same axes in ggplot2?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to recreate an image found in a textbook in R, the original of which was built in MATLAB:



Normal distribution and a transformation with its cumulative distribution function all on the same axes



I have generated each of the graphs seperately, but what would be best practice them into an image like this in ggplot2?



Edit: Provided code used. This is just a transformation of normally distributed data.



library(ggplot2)

mean <- 6
sd <- 1

X <- rnorm(100000, mean = mean, sd = sd)
Y <- dnorm(X, mean = mean, sd = sd)
Y_p <- pnorm(X, mean = mean, sd = sd)

ch_vars <- function(X){
nu_vars <- c()
for (x in X){
nu_vars <- c(nu_vars, (1/(1 + exp(-x + 5))))
}
return(nu_vars)
}

nu_X <- ch_vars(X)
nu_Y <- ch_vars(Y)

data <- data.frame(x = X, y = Y, Y_p = Y_p, x = nu_X, y = nu_Y)

# Cumulative distribution
ggplot(data = data) +
geom_line(aes(x = X, y = Y_p))

# Distribution of initial data
ggplot(data = data_ch, aes(x = X)) +
geom_histogram(aes(y = ..density..), bins = 25, fill = "red", color = "black")

# Distribution of transformed data
ggplot(data = data, aes(x = nu_X)) +
geom_histogram(aes(y = ..density..), bins = 25, fill = "green", color = "black")









share|improve this question




















  • 3





    Can you add example data and code of what you have tried?

    – PoGibas
    Nov 22 '18 at 9:38











  • Your ch_vars() function is unnecessary and very inefficient. You can achieve the same effect by taking advantage of R's built-in vectorization, for example: nu_X <- 1/(1 + exp(-X + 5))

    – jdobres
    Nov 22 '18 at 13:27











  • Thanks and very true, but there's more to come in the function. This is just the starting point.

    – FindingIKK
    Nov 22 '18 at 14:11


















0















I am trying to recreate an image found in a textbook in R, the original of which was built in MATLAB:



Normal distribution and a transformation with its cumulative distribution function all on the same axes



I have generated each of the graphs seperately, but what would be best practice them into an image like this in ggplot2?



Edit: Provided code used. This is just a transformation of normally distributed data.



library(ggplot2)

mean <- 6
sd <- 1

X <- rnorm(100000, mean = mean, sd = sd)
Y <- dnorm(X, mean = mean, sd = sd)
Y_p <- pnorm(X, mean = mean, sd = sd)

ch_vars <- function(X){
nu_vars <- c()
for (x in X){
nu_vars <- c(nu_vars, (1/(1 + exp(-x + 5))))
}
return(nu_vars)
}

nu_X <- ch_vars(X)
nu_Y <- ch_vars(Y)

data <- data.frame(x = X, y = Y, Y_p = Y_p, x = nu_X, y = nu_Y)

# Cumulative distribution
ggplot(data = data) +
geom_line(aes(x = X, y = Y_p))

# Distribution of initial data
ggplot(data = data_ch, aes(x = X)) +
geom_histogram(aes(y = ..density..), bins = 25, fill = "red", color = "black")

# Distribution of transformed data
ggplot(data = data, aes(x = nu_X)) +
geom_histogram(aes(y = ..density..), bins = 25, fill = "green", color = "black")









share|improve this question




















  • 3





    Can you add example data and code of what you have tried?

    – PoGibas
    Nov 22 '18 at 9:38











  • Your ch_vars() function is unnecessary and very inefficient. You can achieve the same effect by taking advantage of R's built-in vectorization, for example: nu_X <- 1/(1 + exp(-X + 5))

    – jdobres
    Nov 22 '18 at 13:27











  • Thanks and very true, but there's more to come in the function. This is just the starting point.

    – FindingIKK
    Nov 22 '18 at 14:11














0












0








0








I am trying to recreate an image found in a textbook in R, the original of which was built in MATLAB:



Normal distribution and a transformation with its cumulative distribution function all on the same axes



I have generated each of the graphs seperately, but what would be best practice them into an image like this in ggplot2?



Edit: Provided code used. This is just a transformation of normally distributed data.



library(ggplot2)

mean <- 6
sd <- 1

X <- rnorm(100000, mean = mean, sd = sd)
Y <- dnorm(X, mean = mean, sd = sd)
Y_p <- pnorm(X, mean = mean, sd = sd)

ch_vars <- function(X){
nu_vars <- c()
for (x in X){
nu_vars <- c(nu_vars, (1/(1 + exp(-x + 5))))
}
return(nu_vars)
}

nu_X <- ch_vars(X)
nu_Y <- ch_vars(Y)

data <- data.frame(x = X, y = Y, Y_p = Y_p, x = nu_X, y = nu_Y)

# Cumulative distribution
ggplot(data = data) +
geom_line(aes(x = X, y = Y_p))

# Distribution of initial data
ggplot(data = data_ch, aes(x = X)) +
geom_histogram(aes(y = ..density..), bins = 25, fill = "red", color = "black")

# Distribution of transformed data
ggplot(data = data, aes(x = nu_X)) +
geom_histogram(aes(y = ..density..), bins = 25, fill = "green", color = "black")









share|improve this question
















I am trying to recreate an image found in a textbook in R, the original of which was built in MATLAB:



Normal distribution and a transformation with its cumulative distribution function all on the same axes



I have generated each of the graphs seperately, but what would be best practice them into an image like this in ggplot2?



Edit: Provided code used. This is just a transformation of normally distributed data.



library(ggplot2)

mean <- 6
sd <- 1

X <- rnorm(100000, mean = mean, sd = sd)
Y <- dnorm(X, mean = mean, sd = sd)
Y_p <- pnorm(X, mean = mean, sd = sd)

ch_vars <- function(X){
nu_vars <- c()
for (x in X){
nu_vars <- c(nu_vars, (1/(1 + exp(-x + 5))))
}
return(nu_vars)
}

nu_X <- ch_vars(X)
nu_Y <- ch_vars(Y)

data <- data.frame(x = X, y = Y, Y_p = Y_p, x = nu_X, y = nu_Y)

# Cumulative distribution
ggplot(data = data) +
geom_line(aes(x = X, y = Y_p))

# Distribution of initial data
ggplot(data = data_ch, aes(x = X)) +
geom_histogram(aes(y = ..density..), bins = 25, fill = "red", color = "black")

# Distribution of transformed data
ggplot(data = data, aes(x = nu_X)) +
geom_histogram(aes(y = ..density..), bins = 25, fill = "green", color = "black")






r ggplot2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 13:03







FindingIKK

















asked Nov 22 '18 at 9:26









FindingIKKFindingIKK

4217




4217








  • 3





    Can you add example data and code of what you have tried?

    – PoGibas
    Nov 22 '18 at 9:38











  • Your ch_vars() function is unnecessary and very inefficient. You can achieve the same effect by taking advantage of R's built-in vectorization, for example: nu_X <- 1/(1 + exp(-X + 5))

    – jdobres
    Nov 22 '18 at 13:27











  • Thanks and very true, but there's more to come in the function. This is just the starting point.

    – FindingIKK
    Nov 22 '18 at 14:11














  • 3





    Can you add example data and code of what you have tried?

    – PoGibas
    Nov 22 '18 at 9:38











  • Your ch_vars() function is unnecessary and very inefficient. You can achieve the same effect by taking advantage of R's built-in vectorization, for example: nu_X <- 1/(1 + exp(-X + 5))

    – jdobres
    Nov 22 '18 at 13:27











  • Thanks and very true, but there's more to come in the function. This is just the starting point.

    – FindingIKK
    Nov 22 '18 at 14:11








3




3





Can you add example data and code of what you have tried?

– PoGibas
Nov 22 '18 at 9:38





Can you add example data and code of what you have tried?

– PoGibas
Nov 22 '18 at 9:38













Your ch_vars() function is unnecessary and very inefficient. You can achieve the same effect by taking advantage of R's built-in vectorization, for example: nu_X <- 1/(1 + exp(-X + 5))

– jdobres
Nov 22 '18 at 13:27





Your ch_vars() function is unnecessary and very inefficient. You can achieve the same effect by taking advantage of R's built-in vectorization, for example: nu_X <- 1/(1 + exp(-X + 5))

– jdobres
Nov 22 '18 at 13:27













Thanks and very true, but there's more to come in the function. This is just the starting point.

– FindingIKK
Nov 22 '18 at 14:11





Thanks and very true, but there's more to come in the function. This is just the starting point.

– FindingIKK
Nov 22 '18 at 14:11












1 Answer
1






active

oldest

votes


















1














In short, you can't, or rather, you shouldn't.



ggplot is a high-level plotting packaging. More than a system for drawing shapes and lines, it's fairly "opinionated" about how data should be represented, and one of its opinions is that a plot should express a clear relationship between its axes and marks (points, bars, lines, etc.). The axes essentially define a coordinate space, and the marks are then plotted onto the space in a straightforward and easily interpretable manner.



The plot you show breaks that relationship -- it's a set of essentially arbitrary histograms all drawn onto the same box, where the axis values become ambiguous. The x-axis represents the values of 1 histogram and the y-axis represents another (and thus neither axis represents the histograms' heights).



It is of course technically possible to force ggplot to render something like your example, but it would require pre-computing the histograms, normalizing their values and bin heights to a common coordinate space, converting these into suitable coordinates for use with geom_rect, and then re-labeling the plot axes. It would be a very large amount of manual effort and ultimately defeats the point of using a high-level plotting grammar like ggplot.






share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427619%2fhow-can-i-place-multiple-unrelated-graphs-on-the-same-axes-in-ggplot2%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    In short, you can't, or rather, you shouldn't.



    ggplot is a high-level plotting packaging. More than a system for drawing shapes and lines, it's fairly "opinionated" about how data should be represented, and one of its opinions is that a plot should express a clear relationship between its axes and marks (points, bars, lines, etc.). The axes essentially define a coordinate space, and the marks are then plotted onto the space in a straightforward and easily interpretable manner.



    The plot you show breaks that relationship -- it's a set of essentially arbitrary histograms all drawn onto the same box, where the axis values become ambiguous. The x-axis represents the values of 1 histogram and the y-axis represents another (and thus neither axis represents the histograms' heights).



    It is of course technically possible to force ggplot to render something like your example, but it would require pre-computing the histograms, normalizing their values and bin heights to a common coordinate space, converting these into suitable coordinates for use with geom_rect, and then re-labeling the plot axes. It would be a very large amount of manual effort and ultimately defeats the point of using a high-level plotting grammar like ggplot.






    share|improve this answer






























      1














      In short, you can't, or rather, you shouldn't.



      ggplot is a high-level plotting packaging. More than a system for drawing shapes and lines, it's fairly "opinionated" about how data should be represented, and one of its opinions is that a plot should express a clear relationship between its axes and marks (points, bars, lines, etc.). The axes essentially define a coordinate space, and the marks are then plotted onto the space in a straightforward and easily interpretable manner.



      The plot you show breaks that relationship -- it's a set of essentially arbitrary histograms all drawn onto the same box, where the axis values become ambiguous. The x-axis represents the values of 1 histogram and the y-axis represents another (and thus neither axis represents the histograms' heights).



      It is of course technically possible to force ggplot to render something like your example, but it would require pre-computing the histograms, normalizing their values and bin heights to a common coordinate space, converting these into suitable coordinates for use with geom_rect, and then re-labeling the plot axes. It would be a very large amount of manual effort and ultimately defeats the point of using a high-level plotting grammar like ggplot.






      share|improve this answer




























        1












        1








        1







        In short, you can't, or rather, you shouldn't.



        ggplot is a high-level plotting packaging. More than a system for drawing shapes and lines, it's fairly "opinionated" about how data should be represented, and one of its opinions is that a plot should express a clear relationship between its axes and marks (points, bars, lines, etc.). The axes essentially define a coordinate space, and the marks are then plotted onto the space in a straightforward and easily interpretable manner.



        The plot you show breaks that relationship -- it's a set of essentially arbitrary histograms all drawn onto the same box, where the axis values become ambiguous. The x-axis represents the values of 1 histogram and the y-axis represents another (and thus neither axis represents the histograms' heights).



        It is of course technically possible to force ggplot to render something like your example, but it would require pre-computing the histograms, normalizing their values and bin heights to a common coordinate space, converting these into suitable coordinates for use with geom_rect, and then re-labeling the plot axes. It would be a very large amount of manual effort and ultimately defeats the point of using a high-level plotting grammar like ggplot.






        share|improve this answer















        In short, you can't, or rather, you shouldn't.



        ggplot is a high-level plotting packaging. More than a system for drawing shapes and lines, it's fairly "opinionated" about how data should be represented, and one of its opinions is that a plot should express a clear relationship between its axes and marks (points, bars, lines, etc.). The axes essentially define a coordinate space, and the marks are then plotted onto the space in a straightforward and easily interpretable manner.



        The plot you show breaks that relationship -- it's a set of essentially arbitrary histograms all drawn onto the same box, where the axis values become ambiguous. The x-axis represents the values of 1 histogram and the y-axis represents another (and thus neither axis represents the histograms' heights).



        It is of course technically possible to force ggplot to render something like your example, but it would require pre-computing the histograms, normalizing their values and bin heights to a common coordinate space, converting these into suitable coordinates for use with geom_rect, and then re-labeling the plot axes. It would be a very large amount of manual effort and ultimately defeats the point of using a high-level plotting grammar like ggplot.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 18:17

























        answered Nov 22 '18 at 14:10









        jdobresjdobres

        5,1031623




        5,1031623
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427619%2fhow-can-i-place-multiple-unrelated-graphs-on-the-same-axes-in-ggplot2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word