Issues changing color on ggplot using scale_fill_brewer












1















enter image description here



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)









share|improve this question

























  • 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






  • 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 $ inside aes.

    – Axeman
    Jan 2 at 1:21
















1















enter image description here



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)









share|improve this question

























  • 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






  • 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 $ inside aes.

    – Axeman
    Jan 2 at 1:21














1












1








1








enter image description here



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)









share|improve this question
















enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 5:38









Z.Lin

12.8k22238




12.8k22238










asked Jan 1 at 23:05









song0089song0089

88031237




88031237













  • 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






  • 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 $ inside aes.

    – Axeman
    Jan 2 at 1:21



















  • 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






  • 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 $ inside aes.

    – 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












1 Answer
1






active

oldest

votes


















2














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


enter image description here



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.






share|improve this answer


























  • 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











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%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









2














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


enter image description here



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.






share|improve this answer


























  • 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
















2














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


enter image description here



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.






share|improve this answer


























  • 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














2












2








2







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


enter image description here



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.






share|improve this answer















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


enter image description here



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.







share|improve this answer














share|improve this answer



share|improve this answer








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 $ 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



















  • 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

















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




















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%2f53999622%2fissues-changing-color-on-ggplot-using-scale-fill-brewer%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

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]