specify color scheme for n number of nodes in networkD3 in R












0















I know that in networkD3 you can specify the color of different nodes by putting them in groups.
Eg.
nodes = data.frame(id = c(0,1,2), name = c("Node 1", "Node 2", "Node 3"), group = c("1","2","3"))



JS('d3.scaleOrdinal().domain(["1", "2", "3"]).range(["#000000", "#111111", "#222222"])')



I wanted to specify a select number of nodes (e.g. just node 1) but then rest of the nodes (e.g. nodes 2 and 3) can be arbitrary colors.



With doing something like this, all the nodes would be the same color:
JS('d3.scaleOrdinal().domain(["1"]).range(["#000000"])')



Would it be possible to specify the node color for select nodes and then other nodes can be arbitrary colors? I'm trying to accomplish this so that if my data set is large I don't need to know how many groups there are to define the colors of the groups whose color can be arbitrary.










share|improve this question





























    0















    I know that in networkD3 you can specify the color of different nodes by putting them in groups.
    Eg.
    nodes = data.frame(id = c(0,1,2), name = c("Node 1", "Node 2", "Node 3"), group = c("1","2","3"))



    JS('d3.scaleOrdinal().domain(["1", "2", "3"]).range(["#000000", "#111111", "#222222"])')



    I wanted to specify a select number of nodes (e.g. just node 1) but then rest of the nodes (e.g. nodes 2 and 3) can be arbitrary colors.



    With doing something like this, all the nodes would be the same color:
    JS('d3.scaleOrdinal().domain(["1"]).range(["#000000"])')



    Would it be possible to specify the node color for select nodes and then other nodes can be arbitrary colors? I'm trying to accomplish this so that if my data set is large I don't need to know how many groups there are to define the colors of the groups whose color can be arbitrary.










    share|improve this question



























      0












      0








      0


      1






      I know that in networkD3 you can specify the color of different nodes by putting them in groups.
      Eg.
      nodes = data.frame(id = c(0,1,2), name = c("Node 1", "Node 2", "Node 3"), group = c("1","2","3"))



      JS('d3.scaleOrdinal().domain(["1", "2", "3"]).range(["#000000", "#111111", "#222222"])')



      I wanted to specify a select number of nodes (e.g. just node 1) but then rest of the nodes (e.g. nodes 2 and 3) can be arbitrary colors.



      With doing something like this, all the nodes would be the same color:
      JS('d3.scaleOrdinal().domain(["1"]).range(["#000000"])')



      Would it be possible to specify the node color for select nodes and then other nodes can be arbitrary colors? I'm trying to accomplish this so that if my data set is large I don't need to know how many groups there are to define the colors of the groups whose color can be arbitrary.










      share|improve this question
















      I know that in networkD3 you can specify the color of different nodes by putting them in groups.
      Eg.
      nodes = data.frame(id = c(0,1,2), name = c("Node 1", "Node 2", "Node 3"), group = c("1","2","3"))



      JS('d3.scaleOrdinal().domain(["1", "2", "3"]).range(["#000000", "#111111", "#222222"])')



      I wanted to specify a select number of nodes (e.g. just node 1) but then rest of the nodes (e.g. nodes 2 and 3) can be arbitrary colors.



      With doing something like this, all the nodes would be the same color:
      JS('d3.scaleOrdinal().domain(["1"]).range(["#000000"])')



      Would it be possible to specify the node color for select nodes and then other nodes can be arbitrary colors? I'm trying to accomplish this so that if my data set is large I don't need to know how many groups there are to define the colors of the groups whose color can be arbitrary.







      javascript r d3.js htmlwidgets networkd3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 21:05









      CJ Yetman

      4,2302632




      4,2302632










      asked Jan 2 at 18:15









      TillieTillie

      184




      184
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Here's an example of how you could set all nodes of group "1" to black, while all other nodes are given a random color from d3.schemeCategory20...



          library(networkD3)

          colorfunc <- JS('colorfunc = function(i) { return i == 1 ? "#000" : d3.schemeCategory20[Math.floor(Math.random()*20)] };')

          forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
          Target = "target", Value = "value", NodeID = "name",
          Group = "group", colourScale = colorfunc, opacity = 1)


          enter image description here






          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%2f54011220%2fspecify-color-scheme-for-n-number-of-nodes-in-networkd3-in-r%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














            Here's an example of how you could set all nodes of group "1" to black, while all other nodes are given a random color from d3.schemeCategory20...



            library(networkD3)

            colorfunc <- JS('colorfunc = function(i) { return i == 1 ? "#000" : d3.schemeCategory20[Math.floor(Math.random()*20)] };')

            forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
            Target = "target", Value = "value", NodeID = "name",
            Group = "group", colourScale = colorfunc, opacity = 1)


            enter image description here






            share|improve this answer




























              0














              Here's an example of how you could set all nodes of group "1" to black, while all other nodes are given a random color from d3.schemeCategory20...



              library(networkD3)

              colorfunc <- JS('colorfunc = function(i) { return i == 1 ? "#000" : d3.schemeCategory20[Math.floor(Math.random()*20)] };')

              forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",
              Group = "group", colourScale = colorfunc, opacity = 1)


              enter image description here






              share|improve this answer


























                0












                0








                0







                Here's an example of how you could set all nodes of group "1" to black, while all other nodes are given a random color from d3.schemeCategory20...



                library(networkD3)

                colorfunc <- JS('colorfunc = function(i) { return i == 1 ? "#000" : d3.schemeCategory20[Math.floor(Math.random()*20)] };')

                forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
                Target = "target", Value = "value", NodeID = "name",
                Group = "group", colourScale = colorfunc, opacity = 1)


                enter image description here






                share|improve this answer













                Here's an example of how you could set all nodes of group "1" to black, while all other nodes are given a random color from d3.schemeCategory20...



                library(networkD3)

                colorfunc <- JS('colorfunc = function(i) { return i == 1 ? "#000" : d3.schemeCategory20[Math.floor(Math.random()*20)] };')

                forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
                Target = "target", Value = "value", NodeID = "name",
                Group = "group", colourScale = colorfunc, opacity = 1)


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 21:03









                CJ YetmanCJ Yetman

                4,2302632




                4,2302632
































                    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%2f54011220%2fspecify-color-scheme-for-n-number-of-nodes-in-networkd3-in-r%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

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas

                    Can't read property showImagePicker of undefined in react native iOS