Issues changing color on ggplot using scale_fill_brewer
I have a dataframe that I want to plot, where one column is a category and the other is counts.
I used scale_fill_brewer
, but it's not changing any colors. The color is also off -- the bars are all neon colored.
How can I fix this?
The dataframe:
> dput(broad_cat)
structure(list(numbs = c(629, 526, 246, 86), labs = structure(c(4L,
2L, 3L, 1L), .Label = c("Propaganda", "Public Service", "Public Welfare",
"Social Welfare"), class = "factor")), .Names = c("numbs", "labs"
), row.names = c(NA, -4L), class = "data.frame")
plot code:
p <- ggplot(broad_cat,
aes(reorder(broad_cat$labs, -(broad_cat$numbs), sum),
broad_cat$numbs))
p +
geom_bar(stat = "identity", fill = broad_cat$numbs) +
labs(x = "Categories", y = "Counts") +
ggtitle("Title[![enter image description here][1]][1]") +
theme(axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
plot.title = element_text(size = 10, hjust = 0.5)) +
scale_fill_brewer(palette = "Blues")
Fixed:
I included fill
in aes()
, but the graph is still black and white.
aes(reorder(broad_cat$labs, -(broad_cat$numbs), sum),
broad_cat$numbs), fill = broad_cat$numbs)
r ggplot2
add a comment |
I have a dataframe that I want to plot, where one column is a category and the other is counts.
I used scale_fill_brewer
, but it's not changing any colors. The color is also off -- the bars are all neon colored.
How can I fix this?
The dataframe:
> dput(broad_cat)
structure(list(numbs = c(629, 526, 246, 86), labs = structure(c(4L,
2L, 3L, 1L), .Label = c("Propaganda", "Public Service", "Public Welfare",
"Social Welfare"), class = "factor")), .Names = c("numbs", "labs"
), row.names = c(NA, -4L), class = "data.frame")
plot code:
p <- ggplot(broad_cat,
aes(reorder(broad_cat$labs, -(broad_cat$numbs), sum),
broad_cat$numbs))
p +
geom_bar(stat = "identity", fill = broad_cat$numbs) +
labs(x = "Categories", y = "Counts") +
ggtitle("Title[![enter image description here][1]][1]") +
theme(axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
plot.title = element_text(size = 10, hjust = 0.5)) +
scale_fill_brewer(palette = "Blues")
Fixed:
I included fill
in aes()
, but the graph is still black and white.
aes(reorder(broad_cat$labs, -(broad_cat$numbs), sum),
broad_cat$numbs), fill = broad_cat$numbs)
r ggplot2
you havefill
outside ofaes
– Tjebo
Jan 1 at 23:08
I fixed the code toaes(reorder(broad_cat$labs,-(broad_cat$numbs),sum),broad_cat$numbs),fill=broad_cat$numbs)
but it's not changing color, it's still b&w
– song0089
Jan 1 at 23:11
1
I have edited your question in order to improve the code readability. would suggest following my suggestions
– Tjebo
Jan 1 at 23:13
Never use$
insideaes
.
– Axeman
Jan 2 at 1:21
add a comment |
I have a dataframe that I want to plot, where one column is a category and the other is counts.
I used scale_fill_brewer
, but it's not changing any colors. The color is also off -- the bars are all neon colored.
How can I fix this?
The dataframe:
> dput(broad_cat)
structure(list(numbs = c(629, 526, 246, 86), labs = structure(c(4L,
2L, 3L, 1L), .Label = c("Propaganda", "Public Service", "Public Welfare",
"Social Welfare"), class = "factor")), .Names = c("numbs", "labs"
), row.names = c(NA, -4L), class = "data.frame")
plot code:
p <- ggplot(broad_cat,
aes(reorder(broad_cat$labs, -(broad_cat$numbs), sum),
broad_cat$numbs))
p +
geom_bar(stat = "identity", fill = broad_cat$numbs) +
labs(x = "Categories", y = "Counts") +
ggtitle("Title[![enter image description here][1]][1]") +
theme(axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
plot.title = element_text(size = 10, hjust = 0.5)) +
scale_fill_brewer(palette = "Blues")
Fixed:
I included fill
in aes()
, but the graph is still black and white.
aes(reorder(broad_cat$labs, -(broad_cat$numbs), sum),
broad_cat$numbs), fill = broad_cat$numbs)
r ggplot2
I have a dataframe that I want to plot, where one column is a category and the other is counts.
I used scale_fill_brewer
, but it's not changing any colors. The color is also off -- the bars are all neon colored.
How can I fix this?
The dataframe:
> dput(broad_cat)
structure(list(numbs = c(629, 526, 246, 86), labs = structure(c(4L,
2L, 3L, 1L), .Label = c("Propaganda", "Public Service", "Public Welfare",
"Social Welfare"), class = "factor")), .Names = c("numbs", "labs"
), row.names = c(NA, -4L), class = "data.frame")
plot code:
p <- ggplot(broad_cat,
aes(reorder(broad_cat$labs, -(broad_cat$numbs), sum),
broad_cat$numbs))
p +
geom_bar(stat = "identity", fill = broad_cat$numbs) +
labs(x = "Categories", y = "Counts") +
ggtitle("Title[![enter image description here][1]][1]") +
theme(axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
plot.title = element_text(size = 10, hjust = 0.5)) +
scale_fill_brewer(palette = "Blues")
Fixed:
I included fill
in aes()
, but the graph is still black and white.
aes(reorder(broad_cat$labs, -(broad_cat$numbs), sum),
broad_cat$numbs), fill = broad_cat$numbs)
r ggplot2
r ggplot2
edited Jan 2 at 5:38
Z.Lin
12.8k22238
12.8k22238
asked Jan 1 at 23:05
song0089song0089
88031237
88031237
you havefill
outside ofaes
– Tjebo
Jan 1 at 23:08
I fixed the code toaes(reorder(broad_cat$labs,-(broad_cat$numbs),sum),broad_cat$numbs),fill=broad_cat$numbs)
but it's not changing color, it's still b&w
– song0089
Jan 1 at 23:11
1
I have edited your question in order to improve the code readability. would suggest following my suggestions
– Tjebo
Jan 1 at 23:13
Never use$
insideaes
.
– Axeman
Jan 2 at 1:21
add a comment |
you havefill
outside ofaes
– Tjebo
Jan 1 at 23:08
I fixed the code toaes(reorder(broad_cat$labs,-(broad_cat$numbs),sum),broad_cat$numbs),fill=broad_cat$numbs)
but it's not changing color, it's still b&w
– song0089
Jan 1 at 23:11
1
I have edited your question in order to improve the code readability. would suggest following my suggestions
– Tjebo
Jan 1 at 23:13
Never use$
insideaes
.
– Axeman
Jan 2 at 1:21
you have
fill
outside of aes
– Tjebo
Jan 1 at 23:08
you have
fill
outside of aes
– Tjebo
Jan 1 at 23:08
I fixed the code to
aes(reorder(broad_cat$labs,-(broad_cat$numbs),sum),broad_cat$numbs),fill=broad_cat$numbs)
but it's not changing color, it's still b&w– song0089
Jan 1 at 23:11
I fixed the code to
aes(reorder(broad_cat$labs,-(broad_cat$numbs),sum),broad_cat$numbs),fill=broad_cat$numbs)
but it's not changing color, it's still b&w– song0089
Jan 1 at 23:11
1
1
I have edited your question in order to improve the code readability. would suggest following my suggestions
– Tjebo
Jan 1 at 23:13
I have edited your question in order to improve the code readability. would suggest following my suggestions
– Tjebo
Jan 1 at 23:13
Never use
$
inside aes
.– Axeman
Jan 2 at 1:21
Never use
$
inside aes
.– Axeman
Jan 2 at 1:21
add a comment |
1 Answer
1
active
oldest
votes
Since numbs
is numeric with no repeating values, I understand that you use it for fill
just to get some nice colors. Otherwise, as the error suggests, it is not going to work as numbs
is continuous and your fill scale is discrete. Perhaps the following is what you want:
p <- ggplot(broad_cat, aes(reorder(labs, -numbs, sum), numbs))
p + geom_bar(stat = "identity", aes(fill = as.factor(numbs)), show.legend = FALSE) +
labs(x = "Categories", y = "Counts") +
ggtitle("Title[![enter image description here][1]][1]") +
theme(axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
plot.title = element_text(size = 10, hjust = 0.5)) +
scale_fill_brewer(palette = "Blues")
Where the main things were to use fill
inside aes
, convert numbs
to a factor as to use the discrete scale, and also I removed all broad_cat$...
as those will lead to problems and variables should be referred to directly by name.
It worked!! Thank you so much!!
– song0089
Jan 1 at 23:19
@song0089, no problem, I'm glad it helped. And indeed you shouldn't never use$
insideaes
and, typically (though not always), there is no need for that elsewhere in theggplot
call either.
– Julius Vainora
Jan 2 at 18:30
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%2f53999622%2fissues-changing-color-on-ggplot-using-scale-fill-brewer%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
Since numbs
is numeric with no repeating values, I understand that you use it for fill
just to get some nice colors. Otherwise, as the error suggests, it is not going to work as numbs
is continuous and your fill scale is discrete. Perhaps the following is what you want:
p <- ggplot(broad_cat, aes(reorder(labs, -numbs, sum), numbs))
p + geom_bar(stat = "identity", aes(fill = as.factor(numbs)), show.legend = FALSE) +
labs(x = "Categories", y = "Counts") +
ggtitle("Title[![enter image description here][1]][1]") +
theme(axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
plot.title = element_text(size = 10, hjust = 0.5)) +
scale_fill_brewer(palette = "Blues")
Where the main things were to use fill
inside aes
, convert numbs
to a factor as to use the discrete scale, and also I removed all broad_cat$...
as those will lead to problems and variables should be referred to directly by name.
It worked!! Thank you so much!!
– song0089
Jan 1 at 23:19
@song0089, no problem, I'm glad it helped. And indeed you shouldn't never use$
insideaes
and, typically (though not always), there is no need for that elsewhere in theggplot
call either.
– Julius Vainora
Jan 2 at 18:30
add a comment |
Since numbs
is numeric with no repeating values, I understand that you use it for fill
just to get some nice colors. Otherwise, as the error suggests, it is not going to work as numbs
is continuous and your fill scale is discrete. Perhaps the following is what you want:
p <- ggplot(broad_cat, aes(reorder(labs, -numbs, sum), numbs))
p + geom_bar(stat = "identity", aes(fill = as.factor(numbs)), show.legend = FALSE) +
labs(x = "Categories", y = "Counts") +
ggtitle("Title[![enter image description here][1]][1]") +
theme(axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
plot.title = element_text(size = 10, hjust = 0.5)) +
scale_fill_brewer(palette = "Blues")
Where the main things were to use fill
inside aes
, convert numbs
to a factor as to use the discrete scale, and also I removed all broad_cat$...
as those will lead to problems and variables should be referred to directly by name.
It worked!! Thank you so much!!
– song0089
Jan 1 at 23:19
@song0089, no problem, I'm glad it helped. And indeed you shouldn't never use$
insideaes
and, typically (though not always), there is no need for that elsewhere in theggplot
call either.
– Julius Vainora
Jan 2 at 18:30
add a comment |
Since numbs
is numeric with no repeating values, I understand that you use it for fill
just to get some nice colors. Otherwise, as the error suggests, it is not going to work as numbs
is continuous and your fill scale is discrete. Perhaps the following is what you want:
p <- ggplot(broad_cat, aes(reorder(labs, -numbs, sum), numbs))
p + geom_bar(stat = "identity", aes(fill = as.factor(numbs)), show.legend = FALSE) +
labs(x = "Categories", y = "Counts") +
ggtitle("Title[![enter image description here][1]][1]") +
theme(axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
plot.title = element_text(size = 10, hjust = 0.5)) +
scale_fill_brewer(palette = "Blues")
Where the main things were to use fill
inside aes
, convert numbs
to a factor as to use the discrete scale, and also I removed all broad_cat$...
as those will lead to problems and variables should be referred to directly by name.
Since numbs
is numeric with no repeating values, I understand that you use it for fill
just to get some nice colors. Otherwise, as the error suggests, it is not going to work as numbs
is continuous and your fill scale is discrete. Perhaps the following is what you want:
p <- ggplot(broad_cat, aes(reorder(labs, -numbs, sum), numbs))
p + geom_bar(stat = "identity", aes(fill = as.factor(numbs)), show.legend = FALSE) +
labs(x = "Categories", y = "Counts") +
ggtitle("Title[![enter image description here][1]][1]") +
theme(axis.title.x = element_text(size = 9),
axis.title.y = element_text(size = 9),
plot.title = element_text(size = 10, hjust = 0.5)) +
scale_fill_brewer(palette = "Blues")
Where the main things were to use fill
inside aes
, convert numbs
to a factor as to use the discrete scale, and also I removed all broad_cat$...
as those will lead to problems and variables should be referred to directly by name.
edited Jan 2 at 1:20
Axeman
19.1k54559
19.1k54559
answered Jan 1 at 23:15
Julius VainoraJulius Vainora
38.1k76685
38.1k76685
It worked!! Thank you so much!!
– song0089
Jan 1 at 23:19
@song0089, no problem, I'm glad it helped. And indeed you shouldn't never use$
insideaes
and, typically (though not always), there is no need for that elsewhere in theggplot
call either.
– Julius Vainora
Jan 2 at 18:30
add a comment |
It worked!! Thank you so much!!
– song0089
Jan 1 at 23:19
@song0089, no problem, I'm glad it helped. And indeed you shouldn't never use$
insideaes
and, typically (though not always), there is no need for that elsewhere in theggplot
call either.
– Julius Vainora
Jan 2 at 18:30
It worked!! Thank you so much!!
– song0089
Jan 1 at 23:19
It worked!! Thank you so much!!
– song0089
Jan 1 at 23:19
@song0089, no problem, I'm glad it helped. And indeed you shouldn't never use
$
inside aes
and, typically (though not always), there is no need for that elsewhere in the ggplot
call either.– Julius Vainora
Jan 2 at 18:30
@song0089, no problem, I'm glad it helped. And indeed you shouldn't never use
$
inside aes
and, typically (though not always), there is no need for that elsewhere in the ggplot
call either.– Julius Vainora
Jan 2 at 18:30
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%2f53999622%2fissues-changing-color-on-ggplot-using-scale-fill-brewer%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
you have
fill
outside ofaes
– Tjebo
Jan 1 at 23:08
I fixed the code to
aes(reorder(broad_cat$labs,-(broad_cat$numbs),sum),broad_cat$numbs),fill=broad_cat$numbs)
but it's not changing color, it's still b&w– song0089
Jan 1 at 23:11
1
I have edited your question in order to improve the code readability. would suggest following my suggestions
– Tjebo
Jan 1 at 23:13
Never use
$
insideaes
.– Axeman
Jan 2 at 1:21