R - Omit NA when plotting only a subset of levels of x and using scale_fill_manual












-1















I'm trying to make a boxplot with X being detections of 3 types of a certain bird genus(Vermivora): two species and hybrids of the two. There were also occasions when none of the 3 were detected, yielding a 4th level of X, "none", in addition to "gwwa" "bwwa" and "hybrid".



I only want to plot "gwwa" and "bwwa", and I want "gwwa" to be yellow and "bwwa" to be lightblue.



I can't generate a boxplot without either a third box "NA", and when I try to omit NA then there is still a third box "FALSE".



The recurring error message I receive when making attempts to avoid those two outcomes is this:



Error: Aesthetics must be either length 1 or the same as the data (3): fill



Here is what I've written:





library(tidyverse)
VERM <- read.csv("C:/1Zack/GWWA Map Stuff/GWWA_bySpecies.csv")
Vermivora <- factor(VERM$Species,levels(VERM$Species)[c(2,1)])
Road_Proximity <- VERM$RoadCount_2km

ggplot(data=VERM, mapping = aes(Vermivora, y=Road_Proximity, fill=Vermivora))+
geom_boxplot()+
scale_fill_manual(values=c("yellow", "lightblue"))




Sorry, this is my first post, and I can't figure out how to include a screenshot and I can't figure out how to put each line of my code on its own line without a full empty line between them.










share|improve this question

























  • Welcome to StackOverflow. Can you add data using dput function (dput(VERM))?

    – PoGibas
    Dec 28 '18 at 23:25











  • You can add blocks of code with single spacing by indenting each line 4 spaces. You can then use normal indentation after that. There is a button on the toolbar to add images, or ctrl-g.

    – Tom
    Dec 28 '18 at 23:33
















-1















I'm trying to make a boxplot with X being detections of 3 types of a certain bird genus(Vermivora): two species and hybrids of the two. There were also occasions when none of the 3 were detected, yielding a 4th level of X, "none", in addition to "gwwa" "bwwa" and "hybrid".



I only want to plot "gwwa" and "bwwa", and I want "gwwa" to be yellow and "bwwa" to be lightblue.



I can't generate a boxplot without either a third box "NA", and when I try to omit NA then there is still a third box "FALSE".



The recurring error message I receive when making attempts to avoid those two outcomes is this:



Error: Aesthetics must be either length 1 or the same as the data (3): fill



Here is what I've written:





library(tidyverse)
VERM <- read.csv("C:/1Zack/GWWA Map Stuff/GWWA_bySpecies.csv")
Vermivora <- factor(VERM$Species,levels(VERM$Species)[c(2,1)])
Road_Proximity <- VERM$RoadCount_2km

ggplot(data=VERM, mapping = aes(Vermivora, y=Road_Proximity, fill=Vermivora))+
geom_boxplot()+
scale_fill_manual(values=c("yellow", "lightblue"))




Sorry, this is my first post, and I can't figure out how to include a screenshot and I can't figure out how to put each line of my code on its own line without a full empty line between them.










share|improve this question

























  • Welcome to StackOverflow. Can you add data using dput function (dput(VERM))?

    – PoGibas
    Dec 28 '18 at 23:25











  • You can add blocks of code with single spacing by indenting each line 4 spaces. You can then use normal indentation after that. There is a button on the toolbar to add images, or ctrl-g.

    – Tom
    Dec 28 '18 at 23:33














-1












-1








-1








I'm trying to make a boxplot with X being detections of 3 types of a certain bird genus(Vermivora): two species and hybrids of the two. There were also occasions when none of the 3 were detected, yielding a 4th level of X, "none", in addition to "gwwa" "bwwa" and "hybrid".



I only want to plot "gwwa" and "bwwa", and I want "gwwa" to be yellow and "bwwa" to be lightblue.



I can't generate a boxplot without either a third box "NA", and when I try to omit NA then there is still a third box "FALSE".



The recurring error message I receive when making attempts to avoid those two outcomes is this:



Error: Aesthetics must be either length 1 or the same as the data (3): fill



Here is what I've written:





library(tidyverse)
VERM <- read.csv("C:/1Zack/GWWA Map Stuff/GWWA_bySpecies.csv")
Vermivora <- factor(VERM$Species,levels(VERM$Species)[c(2,1)])
Road_Proximity <- VERM$RoadCount_2km

ggplot(data=VERM, mapping = aes(Vermivora, y=Road_Proximity, fill=Vermivora))+
geom_boxplot()+
scale_fill_manual(values=c("yellow", "lightblue"))




Sorry, this is my first post, and I can't figure out how to include a screenshot and I can't figure out how to put each line of my code on its own line without a full empty line between them.










share|improve this question
















I'm trying to make a boxplot with X being detections of 3 types of a certain bird genus(Vermivora): two species and hybrids of the two. There were also occasions when none of the 3 were detected, yielding a 4th level of X, "none", in addition to "gwwa" "bwwa" and "hybrid".



I only want to plot "gwwa" and "bwwa", and I want "gwwa" to be yellow and "bwwa" to be lightblue.



I can't generate a boxplot without either a third box "NA", and when I try to omit NA then there is still a third box "FALSE".



The recurring error message I receive when making attempts to avoid those two outcomes is this:



Error: Aesthetics must be either length 1 or the same as the data (3): fill



Here is what I've written:





library(tidyverse)
VERM <- read.csv("C:/1Zack/GWWA Map Stuff/GWWA_bySpecies.csv")
Vermivora <- factor(VERM$Species,levels(VERM$Species)[c(2,1)])
Road_Proximity <- VERM$RoadCount_2km

ggplot(data=VERM, mapping = aes(Vermivora, y=Road_Proximity, fill=Vermivora))+
geom_boxplot()+
scale_fill_manual(values=c("yellow", "lightblue"))




Sorry, this is my first post, and I can't figure out how to include a screenshot and I can't figure out how to put each line of my code on its own line without a full empty line between them.







r ggplot2 boxplot na fill






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 1:03









Tom

918416




918416










asked Dec 28 '18 at 23:20









Zachary GrassoZachary Grasso

1




1













  • Welcome to StackOverflow. Can you add data using dput function (dput(VERM))?

    – PoGibas
    Dec 28 '18 at 23:25











  • You can add blocks of code with single spacing by indenting each line 4 spaces. You can then use normal indentation after that. There is a button on the toolbar to add images, or ctrl-g.

    – Tom
    Dec 28 '18 at 23:33



















  • Welcome to StackOverflow. Can you add data using dput function (dput(VERM))?

    – PoGibas
    Dec 28 '18 at 23:25











  • You can add blocks of code with single spacing by indenting each line 4 spaces. You can then use normal indentation after that. There is a button on the toolbar to add images, or ctrl-g.

    – Tom
    Dec 28 '18 at 23:33

















Welcome to StackOverflow. Can you add data using dput function (dput(VERM))?

– PoGibas
Dec 28 '18 at 23:25





Welcome to StackOverflow. Can you add data using dput function (dput(VERM))?

– PoGibas
Dec 28 '18 at 23:25













You can add blocks of code with single spacing by indenting each line 4 spaces. You can then use normal indentation after that. There is a button on the toolbar to add images, or ctrl-g.

– Tom
Dec 28 '18 at 23:33





You can add blocks of code with single spacing by indenting each line 4 spaces. You can then use normal indentation after that. There is a button on the toolbar to add images, or ctrl-g.

– Tom
Dec 28 '18 at 23:33












1 Answer
1






active

oldest

votes


















0














I am just guessing how your data looks like (since you didn't provide it) so I made up some VERM data.frame in the example below. The main idea is that you should filter for what values you need in Vermivora variable (column).



library(tidyverse)
# Some made up data
VERM <- data.frame(Vermivora = c("gwwa", "bwwa", "hybrid", NA),
Road_Proximity = sample(4*10))


I presume this is similar to what you got based on the description of your problem:



ggplot(VERM, aes(Vermivora, Road_Proximity, fill = Vermivora)) + 
geom_boxplot()


enter image description here





Solution with dyplr



Filter for the values that you need in the variable Vermivora.



VERM %>%
filter(Vermivora %in% c("gwwa", "bwwa")) %>%
ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
geom_boxplot() +
scale_fill_manual(values = c("gwwa" = "yellow",
"bwwa" = "lightblue"))


enter image description here



If you need a certain order on the OX axis, then order the factor Vermivora.



VERM %>%
filter(Vermivora %in% c("gwwa", "bwwa")) %>%
mutate(Vermivora = ordered(x = Vermivora,
levels = c("gwwa", "bwwa"))) %>%
ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
geom_boxplot() +
scale_fill_manual(values = c("gwwa" = "yellow",
"bwwa" = "lightblue"))


enter image description here





Solution with data.table



Using the more concise syntax of data.table:



library(data.table)
library(magrittr) # for piping with %>% (not only dyplr use it;
# pipeline is a Unix trait not a dyplr trait)

setDT(VERM) # converts to data.table from data.frame

VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
geom_boxplot() +
scale_fill_manual(values = c("gwwa" = "yellow",
"bwwa" = "lightblue"))

# If you need to order the factor Vermivora:
VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
.[, Vermivora := ordered(x = Vermivora, levels = c("gwwa", "bwwa"))] %>%
ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
geom_boxplot() +
scale_fill_manual(values = c("gwwa" = "yellow",
"bwwa" = "lightblue"))




Note that when addressing a question, is almost always needed to see some data related to your problem. You can try that by posting the output of one of the following lines:



dput(VERM) # the entire data
dput(head(VERM, 20)) # the first 20 rows
dput(VERM[sample(x = nrow(VERM), size = 20),]) # a sample of 20 rows
# Or make up some data like I did in the above example.





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%2f53965330%2fr-omit-na-when-plotting-only-a-subset-of-levels-of-x-and-using-scale-fill-manu%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














    I am just guessing how your data looks like (since you didn't provide it) so I made up some VERM data.frame in the example below. The main idea is that you should filter for what values you need in Vermivora variable (column).



    library(tidyverse)
    # Some made up data
    VERM <- data.frame(Vermivora = c("gwwa", "bwwa", "hybrid", NA),
    Road_Proximity = sample(4*10))


    I presume this is similar to what you got based on the description of your problem:



    ggplot(VERM, aes(Vermivora, Road_Proximity, fill = Vermivora)) + 
    geom_boxplot()


    enter image description here





    Solution with dyplr



    Filter for the values that you need in the variable Vermivora.



    VERM %>%
    filter(Vermivora %in% c("gwwa", "bwwa")) %>%
    ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
    geom_boxplot() +
    scale_fill_manual(values = c("gwwa" = "yellow",
    "bwwa" = "lightblue"))


    enter image description here



    If you need a certain order on the OX axis, then order the factor Vermivora.



    VERM %>%
    filter(Vermivora %in% c("gwwa", "bwwa")) %>%
    mutate(Vermivora = ordered(x = Vermivora,
    levels = c("gwwa", "bwwa"))) %>%
    ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
    geom_boxplot() +
    scale_fill_manual(values = c("gwwa" = "yellow",
    "bwwa" = "lightblue"))


    enter image description here





    Solution with data.table



    Using the more concise syntax of data.table:



    library(data.table)
    library(magrittr) # for piping with %>% (not only dyplr use it;
    # pipeline is a Unix trait not a dyplr trait)

    setDT(VERM) # converts to data.table from data.frame

    VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
    ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
    geom_boxplot() +
    scale_fill_manual(values = c("gwwa" = "yellow",
    "bwwa" = "lightblue"))

    # If you need to order the factor Vermivora:
    VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
    .[, Vermivora := ordered(x = Vermivora, levels = c("gwwa", "bwwa"))] %>%
    ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
    geom_boxplot() +
    scale_fill_manual(values = c("gwwa" = "yellow",
    "bwwa" = "lightblue"))




    Note that when addressing a question, is almost always needed to see some data related to your problem. You can try that by posting the output of one of the following lines:



    dput(VERM) # the entire data
    dput(head(VERM, 20)) # the first 20 rows
    dput(VERM[sample(x = nrow(VERM), size = 20),]) # a sample of 20 rows
    # Or make up some data like I did in the above example.





    share|improve this answer






























      0














      I am just guessing how your data looks like (since you didn't provide it) so I made up some VERM data.frame in the example below. The main idea is that you should filter for what values you need in Vermivora variable (column).



      library(tidyverse)
      # Some made up data
      VERM <- data.frame(Vermivora = c("gwwa", "bwwa", "hybrid", NA),
      Road_Proximity = sample(4*10))


      I presume this is similar to what you got based on the description of your problem:



      ggplot(VERM, aes(Vermivora, Road_Proximity, fill = Vermivora)) + 
      geom_boxplot()


      enter image description here





      Solution with dyplr



      Filter for the values that you need in the variable Vermivora.



      VERM %>%
      filter(Vermivora %in% c("gwwa", "bwwa")) %>%
      ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
      geom_boxplot() +
      scale_fill_manual(values = c("gwwa" = "yellow",
      "bwwa" = "lightblue"))


      enter image description here



      If you need a certain order on the OX axis, then order the factor Vermivora.



      VERM %>%
      filter(Vermivora %in% c("gwwa", "bwwa")) %>%
      mutate(Vermivora = ordered(x = Vermivora,
      levels = c("gwwa", "bwwa"))) %>%
      ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
      geom_boxplot() +
      scale_fill_manual(values = c("gwwa" = "yellow",
      "bwwa" = "lightblue"))


      enter image description here





      Solution with data.table



      Using the more concise syntax of data.table:



      library(data.table)
      library(magrittr) # for piping with %>% (not only dyplr use it;
      # pipeline is a Unix trait not a dyplr trait)

      setDT(VERM) # converts to data.table from data.frame

      VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
      ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
      geom_boxplot() +
      scale_fill_manual(values = c("gwwa" = "yellow",
      "bwwa" = "lightblue"))

      # If you need to order the factor Vermivora:
      VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
      .[, Vermivora := ordered(x = Vermivora, levels = c("gwwa", "bwwa"))] %>%
      ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
      geom_boxplot() +
      scale_fill_manual(values = c("gwwa" = "yellow",
      "bwwa" = "lightblue"))




      Note that when addressing a question, is almost always needed to see some data related to your problem. You can try that by posting the output of one of the following lines:



      dput(VERM) # the entire data
      dput(head(VERM, 20)) # the first 20 rows
      dput(VERM[sample(x = nrow(VERM), size = 20),]) # a sample of 20 rows
      # Or make up some data like I did in the above example.





      share|improve this answer




























        0












        0








        0







        I am just guessing how your data looks like (since you didn't provide it) so I made up some VERM data.frame in the example below. The main idea is that you should filter for what values you need in Vermivora variable (column).



        library(tidyverse)
        # Some made up data
        VERM <- data.frame(Vermivora = c("gwwa", "bwwa", "hybrid", NA),
        Road_Proximity = sample(4*10))


        I presume this is similar to what you got based on the description of your problem:



        ggplot(VERM, aes(Vermivora, Road_Proximity, fill = Vermivora)) + 
        geom_boxplot()


        enter image description here





        Solution with dyplr



        Filter for the values that you need in the variable Vermivora.



        VERM %>%
        filter(Vermivora %in% c("gwwa", "bwwa")) %>%
        ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
        geom_boxplot() +
        scale_fill_manual(values = c("gwwa" = "yellow",
        "bwwa" = "lightblue"))


        enter image description here



        If you need a certain order on the OX axis, then order the factor Vermivora.



        VERM %>%
        filter(Vermivora %in% c("gwwa", "bwwa")) %>%
        mutate(Vermivora = ordered(x = Vermivora,
        levels = c("gwwa", "bwwa"))) %>%
        ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
        geom_boxplot() +
        scale_fill_manual(values = c("gwwa" = "yellow",
        "bwwa" = "lightblue"))


        enter image description here





        Solution with data.table



        Using the more concise syntax of data.table:



        library(data.table)
        library(magrittr) # for piping with %>% (not only dyplr use it;
        # pipeline is a Unix trait not a dyplr trait)

        setDT(VERM) # converts to data.table from data.frame

        VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
        ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
        geom_boxplot() +
        scale_fill_manual(values = c("gwwa" = "yellow",
        "bwwa" = "lightblue"))

        # If you need to order the factor Vermivora:
        VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
        .[, Vermivora := ordered(x = Vermivora, levels = c("gwwa", "bwwa"))] %>%
        ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
        geom_boxplot() +
        scale_fill_manual(values = c("gwwa" = "yellow",
        "bwwa" = "lightblue"))




        Note that when addressing a question, is almost always needed to see some data related to your problem. You can try that by posting the output of one of the following lines:



        dput(VERM) # the entire data
        dput(head(VERM, 20)) # the first 20 rows
        dput(VERM[sample(x = nrow(VERM), size = 20),]) # a sample of 20 rows
        # Or make up some data like I did in the above example.





        share|improve this answer















        I am just guessing how your data looks like (since you didn't provide it) so I made up some VERM data.frame in the example below. The main idea is that you should filter for what values you need in Vermivora variable (column).



        library(tidyverse)
        # Some made up data
        VERM <- data.frame(Vermivora = c("gwwa", "bwwa", "hybrid", NA),
        Road_Proximity = sample(4*10))


        I presume this is similar to what you got based on the description of your problem:



        ggplot(VERM, aes(Vermivora, Road_Proximity, fill = Vermivora)) + 
        geom_boxplot()


        enter image description here





        Solution with dyplr



        Filter for the values that you need in the variable Vermivora.



        VERM %>%
        filter(Vermivora %in% c("gwwa", "bwwa")) %>%
        ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
        geom_boxplot() +
        scale_fill_manual(values = c("gwwa" = "yellow",
        "bwwa" = "lightblue"))


        enter image description here



        If you need a certain order on the OX axis, then order the factor Vermivora.



        VERM %>%
        filter(Vermivora %in% c("gwwa", "bwwa")) %>%
        mutate(Vermivora = ordered(x = Vermivora,
        levels = c("gwwa", "bwwa"))) %>%
        ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
        geom_boxplot() +
        scale_fill_manual(values = c("gwwa" = "yellow",
        "bwwa" = "lightblue"))


        enter image description here





        Solution with data.table



        Using the more concise syntax of data.table:



        library(data.table)
        library(magrittr) # for piping with %>% (not only dyplr use it;
        # pipeline is a Unix trait not a dyplr trait)

        setDT(VERM) # converts to data.table from data.frame

        VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
        ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
        geom_boxplot() +
        scale_fill_manual(values = c("gwwa" = "yellow",
        "bwwa" = "lightblue"))

        # If you need to order the factor Vermivora:
        VERM[Vermivora %in% c("gwwa", "bwwa")] %>%
        .[, Vermivora := ordered(x = Vermivora, levels = c("gwwa", "bwwa"))] %>%
        ggplot(aes(x = Vermivora, y = Road_Proximity, fill = Vermivora)) +
        geom_boxplot() +
        scale_fill_manual(values = c("gwwa" = "yellow",
        "bwwa" = "lightblue"))




        Note that when addressing a question, is almost always needed to see some data related to your problem. You can try that by posting the output of one of the following lines:



        dput(VERM) # the entire data
        dput(head(VERM, 20)) # the first 20 rows
        dput(VERM[sample(x = nrow(VERM), size = 20),]) # a sample of 20 rows
        # Or make up some data like I did in the above example.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 31 '18 at 10:48

























        answered Dec 30 '18 at 11:16









        ValentinValentin

        1,8501129




        1,8501129






























            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%2f53965330%2fr-omit-na-when-plotting-only-a-subset-of-levels-of-x-and-using-scale-fill-manu%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

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'