R plotly - Different colorscales for two scatter on the same plots





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







1















I'm trying to plot two different set of data on the same figure, using two àdd_trace commands. I specify a different colorscales for each, but the second one is ignored , so my second scatter plot has the same color gradient than the first one. How can I fix this ?



I tried the solution here, but it doesn't work (I got a warning saying 'scatter' objects don't have these attributes: 'colorscale').



My code (with a dataframe with random numbers for testing) :



library(plotly)
library(FactoMineR)

n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))

res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)

biplot <- plot_ly(ind) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
color=cos2[,1],
colors="OrRd",
marker=list(symbol=27, size=11)) %>%
add_trace(var,
x=var[,1],
y=var[,2],
type = 'scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
color=cos2_v[,1],
colors="BuGn",
marker=list(symbol=4, size=11))


Thanks in advance (the actual result is on the picture below).



enter image description here










share|improve this question

























  • Please help ! I'm stuck ! :)

    – Micawber
    Apr 10 '18 at 13:13


















1















I'm trying to plot two different set of data on the same figure, using two àdd_trace commands. I specify a different colorscales for each, but the second one is ignored , so my second scatter plot has the same color gradient than the first one. How can I fix this ?



I tried the solution here, but it doesn't work (I got a warning saying 'scatter' objects don't have these attributes: 'colorscale').



My code (with a dataframe with random numbers for testing) :



library(plotly)
library(FactoMineR)

n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))

res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)

biplot <- plot_ly(ind) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
color=cos2[,1],
colors="OrRd",
marker=list(symbol=27, size=11)) %>%
add_trace(var,
x=var[,1],
y=var[,2],
type = 'scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
color=cos2_v[,1],
colors="BuGn",
marker=list(symbol=4, size=11))


Thanks in advance (the actual result is on the picture below).



enter image description here










share|improve this question

























  • Please help ! I'm stuck ! :)

    – Micawber
    Apr 10 '18 at 13:13














1












1








1


1






I'm trying to plot two different set of data on the same figure, using two àdd_trace commands. I specify a different colorscales for each, but the second one is ignored , so my second scatter plot has the same color gradient than the first one. How can I fix this ?



I tried the solution here, but it doesn't work (I got a warning saying 'scatter' objects don't have these attributes: 'colorscale').



My code (with a dataframe with random numbers for testing) :



library(plotly)
library(FactoMineR)

n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))

res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)

biplot <- plot_ly(ind) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
color=cos2[,1],
colors="OrRd",
marker=list(symbol=27, size=11)) %>%
add_trace(var,
x=var[,1],
y=var[,2],
type = 'scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
color=cos2_v[,1],
colors="BuGn",
marker=list(symbol=4, size=11))


Thanks in advance (the actual result is on the picture below).



enter image description here










share|improve this question
















I'm trying to plot two different set of data on the same figure, using two àdd_trace commands. I specify a different colorscales for each, but the second one is ignored , so my second scatter plot has the same color gradient than the first one. How can I fix this ?



I tried the solution here, but it doesn't work (I got a warning saying 'scatter' objects don't have these attributes: 'colorscale').



My code (with a dataframe with random numbers for testing) :



library(plotly)
library(FactoMineR)

n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))

res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)

biplot <- plot_ly(ind) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
color=cos2[,1],
colors="OrRd",
marker=list(symbol=27, size=11)) %>%
add_trace(var,
x=var[,1],
y=var[,2],
type = 'scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
color=cos2_v[,1],
colors="BuGn",
marker=list(symbol=4, size=11))


Thanks in advance (the actual result is on the picture below).



enter image description here







r r-plotly






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 12 '18 at 8:57







Micawber

















asked Mar 12 '18 at 8:41









MicawberMicawber

14119




14119













  • Please help ! I'm stuck ! :)

    – Micawber
    Apr 10 '18 at 13:13



















  • Please help ! I'm stuck ! :)

    – Micawber
    Apr 10 '18 at 13:13

















Please help ! I'm stuck ! :)

– Micawber
Apr 10 '18 at 13:13





Please help ! I'm stuck ! :)

– Micawber
Apr 10 '18 at 13:13












1 Answer
1






active

oldest

votes


















0














This works. If you do something custom you have to format the arguments according to plotly::schema(). The colors argument in plotly is a helper to streamline the more complicated plotly.js syntax. Note how color and all the other marker arguments have to be within a list called marker, under which you have to manually format both the colorscale (to get the colors you want) and the colorbar (to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).



library(plotly)
library(FactoMineR)

n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))

res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)

biplot <- plot_ly(ind,showlegend=F) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=27, size=11
,color=cos2[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
)
,colorbar=list(yanchor='bottom',len=.5)
)) %>%
add_trace(x=var[,1],
y=var[,2],
type='scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=4, size=11
,color=cos2_v[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
)
,colorbar=list(yanchor='top',len=.5)
))
biplot


result with two colorscales






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%2f49231083%2fr-plotly-different-colorscales-for-two-scatter-on-the-same-plots%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









    0














    This works. If you do something custom you have to format the arguments according to plotly::schema(). The colors argument in plotly is a helper to streamline the more complicated plotly.js syntax. Note how color and all the other marker arguments have to be within a list called marker, under which you have to manually format both the colorscale (to get the colors you want) and the colorbar (to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).



    library(plotly)
    library(FactoMineR)

    n <- 10 ; m <- 20 ; reps <- 6
    a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
    replicate(reps, sample(c(0, 1), n, replace = TRUE))))

    res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
    ind <- as.data.frame(res.pca$ind$coord)
    cos2 <- as.data.frame(res.pca$ind$cos2)
    var <- as.data.frame(res.pca$var$coord)
    cos2_v <- as.data.frame(res.pca$var$cos2)

    biplot <- plot_ly(ind,showlegend=F) %>%
    add_trace(x=ind[,1],
    y=ind[,2],
    type='scatter',
    text=rownames(a),
    textposition='top',
    mode="markers+text",
    marker=list(symbol=27, size=11
    ,color=cos2[,1]
    ,colorscale=list(
    list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
    ,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
    )
    ,colorbar=list(yanchor='bottom',len=.5)
    )) %>%
    add_trace(x=var[,1],
    y=var[,2],
    type='scatter',
    text=colnames(a),
    textposition='top',
    mode="markers+text",
    marker=list(symbol=4, size=11
    ,color=cos2_v[,1]
    ,colorscale=list(
    list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
    ,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
    )
    ,colorbar=list(yanchor='top',len=.5)
    ))
    biplot


    result with two colorscales






    share|improve this answer






























      0














      This works. If you do something custom you have to format the arguments according to plotly::schema(). The colors argument in plotly is a helper to streamline the more complicated plotly.js syntax. Note how color and all the other marker arguments have to be within a list called marker, under which you have to manually format both the colorscale (to get the colors you want) and the colorbar (to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).



      library(plotly)
      library(FactoMineR)

      n <- 10 ; m <- 20 ; reps <- 6
      a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
      replicate(reps, sample(c(0, 1), n, replace = TRUE))))

      res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
      ind <- as.data.frame(res.pca$ind$coord)
      cos2 <- as.data.frame(res.pca$ind$cos2)
      var <- as.data.frame(res.pca$var$coord)
      cos2_v <- as.data.frame(res.pca$var$cos2)

      biplot <- plot_ly(ind,showlegend=F) %>%
      add_trace(x=ind[,1],
      y=ind[,2],
      type='scatter',
      text=rownames(a),
      textposition='top',
      mode="markers+text",
      marker=list(symbol=27, size=11
      ,color=cos2[,1]
      ,colorscale=list(
      list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
      ,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
      )
      ,colorbar=list(yanchor='bottom',len=.5)
      )) %>%
      add_trace(x=var[,1],
      y=var[,2],
      type='scatter',
      text=colnames(a),
      textposition='top',
      mode="markers+text",
      marker=list(symbol=4, size=11
      ,color=cos2_v[,1]
      ,colorscale=list(
      list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
      ,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
      )
      ,colorbar=list(yanchor='top',len=.5)
      ))
      biplot


      result with two colorscales






      share|improve this answer




























        0












        0








        0







        This works. If you do something custom you have to format the arguments according to plotly::schema(). The colors argument in plotly is a helper to streamline the more complicated plotly.js syntax. Note how color and all the other marker arguments have to be within a list called marker, under which you have to manually format both the colorscale (to get the colors you want) and the colorbar (to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).



        library(plotly)
        library(FactoMineR)

        n <- 10 ; m <- 20 ; reps <- 6
        a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
        replicate(reps, sample(c(0, 1), n, replace = TRUE))))

        res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
        ind <- as.data.frame(res.pca$ind$coord)
        cos2 <- as.data.frame(res.pca$ind$cos2)
        var <- as.data.frame(res.pca$var$coord)
        cos2_v <- as.data.frame(res.pca$var$cos2)

        biplot <- plot_ly(ind,showlegend=F) %>%
        add_trace(x=ind[,1],
        y=ind[,2],
        type='scatter',
        text=rownames(a),
        textposition='top',
        mode="markers+text",
        marker=list(symbol=27, size=11
        ,color=cos2[,1]
        ,colorscale=list(
        list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
        ,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
        )
        ,colorbar=list(yanchor='bottom',len=.5)
        )) %>%
        add_trace(x=var[,1],
        y=var[,2],
        type='scatter',
        text=colnames(a),
        textposition='top',
        mode="markers+text",
        marker=list(symbol=4, size=11
        ,color=cos2_v[,1]
        ,colorscale=list(
        list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
        ,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
        )
        ,colorbar=list(yanchor='top',len=.5)
        ))
        biplot


        result with two colorscales






        share|improve this answer















        This works. If you do something custom you have to format the arguments according to plotly::schema(). The colors argument in plotly is a helper to streamline the more complicated plotly.js syntax. Note how color and all the other marker arguments have to be within a list called marker, under which you have to manually format both the colorscale (to get the colors you want) and the colorbar (to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).



        library(plotly)
        library(FactoMineR)

        n <- 10 ; m <- 20 ; reps <- 6
        a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
        replicate(reps, sample(c(0, 1), n, replace = TRUE))))

        res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
        ind <- as.data.frame(res.pca$ind$coord)
        cos2 <- as.data.frame(res.pca$ind$cos2)
        var <- as.data.frame(res.pca$var$coord)
        cos2_v <- as.data.frame(res.pca$var$cos2)

        biplot <- plot_ly(ind,showlegend=F) %>%
        add_trace(x=ind[,1],
        y=ind[,2],
        type='scatter',
        text=rownames(a),
        textposition='top',
        mode="markers+text",
        marker=list(symbol=27, size=11
        ,color=cos2[,1]
        ,colorscale=list(
        list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
        ,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
        )
        ,colorbar=list(yanchor='bottom',len=.5)
        )) %>%
        add_trace(x=var[,1],
        y=var[,2],
        type='scatter',
        text=colnames(a),
        textposition='top',
        mode="markers+text",
        marker=list(symbol=4, size=11
        ,color=cos2_v[,1]
        ,colorscale=list(
        list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
        ,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
        )
        ,colorbar=list(yanchor='top',len=.5)
        ))
        biplot


        result with two colorscales







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 17 '18 at 18:23

























        answered Nov 17 '18 at 4:36









        Brooks AmbroseBrooks Ambrose

        584




        584
































            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%2f49231083%2fr-plotly-different-colorscales-for-two-scatter-on-the-same-plots%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

            Bressuire

            Vorschmack

            Quarantine