R - Omit NA when plotting only a subset of levels of x and using scale_fill_manual
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
add a comment |
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
Welcome to StackOverflow. Can you add data usingdputfunction (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, orctrl-g.
– Tom
Dec 28 '18 at 23:33
add a comment |
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
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
r ggplot2 boxplot na fill
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 usingdputfunction (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, orctrl-g.
– Tom
Dec 28 '18 at 23:33
add a comment |
Welcome to StackOverflow. Can you add data usingdputfunction (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, orctrl-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
add a comment |
1 Answer
1
active
oldest
votes
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()

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"))

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"))

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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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()

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"))

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"))

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.
add a comment |
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()

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"))

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"))

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.
add a comment |
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()

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"))

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"))

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.
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()

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"))

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"))

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.
edited Dec 31 '18 at 10:48
answered Dec 30 '18 at 11:16
ValentinValentin
1,8501129
1,8501129
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Welcome to StackOverflow. Can you add data using
dputfunction (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