Changing order in stacked bar chart












1















I'm struggling with fixing the order of my stacked bar chart despite having read through all the examples I could find. It is definitely not stacking based on the order in my dataframe, which seems to be a common answer.



I've managed to change the labels to the proper order, but the plot itself is being stubborn.



I would like to enter the order manually since it does not follow a pattern.



enter image description here



data



sdi_table_2<-read.table(text="'Over 50' 'Under 50'
'Intellectual Disability' 154814 214279
'Other Mental Health' 1387315 1012520
Injuries 240033 116167
Musculo-Skeletal 2041162 447212
'Nervous System' 530179 272756
'Circulatory, Respiratory, Endocrine, and Neoplasms' 1281261 260737
'Other and Unknown' 404973 212136", header=TRUE, check.names=F)
sdi_data_2<-as.data.frame.matrix(sdi_table_2)
sdi_data_2$type<-rownames(sdi_data_2)
sdi_data_2<-melt(sdi_data_2,id.vars = c("type"), value.name = "Beneficiaries")
sdi_data_2$variable <- as.character(sdi_data_2$variable)
colnames(sdi_data_2)[2] <- "Age"
factor(sdi_table_2$type, levels = sdi_table_2$type, ordered = TRUE)


plot



ggplot() +
geom_bar(aes(y = Beneficiaries, x = Age, fill = type),
data = sdi_data_2,
stat = "identity") +
theme_bw() +
scale_y_continuous(label = comma) +
scale_fill_manual(
name = "",
values = c(
"Injuries" = "orange2",
"Circulatory, Respiratory, Endocrine, and Neoplasms" =
"sandybrown",
"Nervous System" = "peachpuff",
"Intellectual Disability" = "skyblue2",
"Musculo-Skeletal" = "royalblue3",
[enter image description here][1] "Other and Unknown" =
"lightgoldenrod1",
"Other Mental Health" = "royalblue1"
),
breaks = c(
"Other and Unknown",
"Injuries",
"Circulatory, Respiratory, Endocrine, and Neoplasms",
"Nervous System",
"Musculo-Skeletal",
"Other Mental Health",
"Intellectual Disability"
)
)









share|improve this question

























  • There are issues in your code. Please check.

    – markus
    Nov 22 '18 at 8:43
















1















I'm struggling with fixing the order of my stacked bar chart despite having read through all the examples I could find. It is definitely not stacking based on the order in my dataframe, which seems to be a common answer.



I've managed to change the labels to the proper order, but the plot itself is being stubborn.



I would like to enter the order manually since it does not follow a pattern.



enter image description here



data



sdi_table_2<-read.table(text="'Over 50' 'Under 50'
'Intellectual Disability' 154814 214279
'Other Mental Health' 1387315 1012520
Injuries 240033 116167
Musculo-Skeletal 2041162 447212
'Nervous System' 530179 272756
'Circulatory, Respiratory, Endocrine, and Neoplasms' 1281261 260737
'Other and Unknown' 404973 212136", header=TRUE, check.names=F)
sdi_data_2<-as.data.frame.matrix(sdi_table_2)
sdi_data_2$type<-rownames(sdi_data_2)
sdi_data_2<-melt(sdi_data_2,id.vars = c("type"), value.name = "Beneficiaries")
sdi_data_2$variable <- as.character(sdi_data_2$variable)
colnames(sdi_data_2)[2] <- "Age"
factor(sdi_table_2$type, levels = sdi_table_2$type, ordered = TRUE)


plot



ggplot() +
geom_bar(aes(y = Beneficiaries, x = Age, fill = type),
data = sdi_data_2,
stat = "identity") +
theme_bw() +
scale_y_continuous(label = comma) +
scale_fill_manual(
name = "",
values = c(
"Injuries" = "orange2",
"Circulatory, Respiratory, Endocrine, and Neoplasms" =
"sandybrown",
"Nervous System" = "peachpuff",
"Intellectual Disability" = "skyblue2",
"Musculo-Skeletal" = "royalblue3",
[enter image description here][1] "Other and Unknown" =
"lightgoldenrod1",
"Other Mental Health" = "royalblue1"
),
breaks = c(
"Other and Unknown",
"Injuries",
"Circulatory, Respiratory, Endocrine, and Neoplasms",
"Nervous System",
"Musculo-Skeletal",
"Other Mental Health",
"Intellectual Disability"
)
)









share|improve this question

























  • There are issues in your code. Please check.

    – markus
    Nov 22 '18 at 8:43














1












1








1








I'm struggling with fixing the order of my stacked bar chart despite having read through all the examples I could find. It is definitely not stacking based on the order in my dataframe, which seems to be a common answer.



I've managed to change the labels to the proper order, but the plot itself is being stubborn.



I would like to enter the order manually since it does not follow a pattern.



enter image description here



data



sdi_table_2<-read.table(text="'Over 50' 'Under 50'
'Intellectual Disability' 154814 214279
'Other Mental Health' 1387315 1012520
Injuries 240033 116167
Musculo-Skeletal 2041162 447212
'Nervous System' 530179 272756
'Circulatory, Respiratory, Endocrine, and Neoplasms' 1281261 260737
'Other and Unknown' 404973 212136", header=TRUE, check.names=F)
sdi_data_2<-as.data.frame.matrix(sdi_table_2)
sdi_data_2$type<-rownames(sdi_data_2)
sdi_data_2<-melt(sdi_data_2,id.vars = c("type"), value.name = "Beneficiaries")
sdi_data_2$variable <- as.character(sdi_data_2$variable)
colnames(sdi_data_2)[2] <- "Age"
factor(sdi_table_2$type, levels = sdi_table_2$type, ordered = TRUE)


plot



ggplot() +
geom_bar(aes(y = Beneficiaries, x = Age, fill = type),
data = sdi_data_2,
stat = "identity") +
theme_bw() +
scale_y_continuous(label = comma) +
scale_fill_manual(
name = "",
values = c(
"Injuries" = "orange2",
"Circulatory, Respiratory, Endocrine, and Neoplasms" =
"sandybrown",
"Nervous System" = "peachpuff",
"Intellectual Disability" = "skyblue2",
"Musculo-Skeletal" = "royalblue3",
[enter image description here][1] "Other and Unknown" =
"lightgoldenrod1",
"Other Mental Health" = "royalblue1"
),
breaks = c(
"Other and Unknown",
"Injuries",
"Circulatory, Respiratory, Endocrine, and Neoplasms",
"Nervous System",
"Musculo-Skeletal",
"Other Mental Health",
"Intellectual Disability"
)
)









share|improve this question
















I'm struggling with fixing the order of my stacked bar chart despite having read through all the examples I could find. It is definitely not stacking based on the order in my dataframe, which seems to be a common answer.



I've managed to change the labels to the proper order, but the plot itself is being stubborn.



I would like to enter the order manually since it does not follow a pattern.



enter image description here



data



sdi_table_2<-read.table(text="'Over 50' 'Under 50'
'Intellectual Disability' 154814 214279
'Other Mental Health' 1387315 1012520
Injuries 240033 116167
Musculo-Skeletal 2041162 447212
'Nervous System' 530179 272756
'Circulatory, Respiratory, Endocrine, and Neoplasms' 1281261 260737
'Other and Unknown' 404973 212136", header=TRUE, check.names=F)
sdi_data_2<-as.data.frame.matrix(sdi_table_2)
sdi_data_2$type<-rownames(sdi_data_2)
sdi_data_2<-melt(sdi_data_2,id.vars = c("type"), value.name = "Beneficiaries")
sdi_data_2$variable <- as.character(sdi_data_2$variable)
colnames(sdi_data_2)[2] <- "Age"
factor(sdi_table_2$type, levels = sdi_table_2$type, ordered = TRUE)


plot



ggplot() +
geom_bar(aes(y = Beneficiaries, x = Age, fill = type),
data = sdi_data_2,
stat = "identity") +
theme_bw() +
scale_y_continuous(label = comma) +
scale_fill_manual(
name = "",
values = c(
"Injuries" = "orange2",
"Circulatory, Respiratory, Endocrine, and Neoplasms" =
"sandybrown",
"Nervous System" = "peachpuff",
"Intellectual Disability" = "skyblue2",
"Musculo-Skeletal" = "royalblue3",
[enter image description here][1] "Other and Unknown" =
"lightgoldenrod1",
"Other Mental Health" = "royalblue1"
),
breaks = c(
"Other and Unknown",
"Injuries",
"Circulatory, Respiratory, Endocrine, and Neoplasms",
"Nervous System",
"Musculo-Skeletal",
"Other Mental Health",
"Intellectual Disability"
)
)






r ggplot2 bar-chart stacked-chart






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 8:42









markus

11k1031




11k1031










asked Nov 22 '18 at 8:07









R BrownR Brown

61




61













  • There are issues in your code. Please check.

    – markus
    Nov 22 '18 at 8:43



















  • There are issues in your code. Please check.

    – markus
    Nov 22 '18 at 8:43

















There are issues in your code. Please check.

– markus
Nov 22 '18 at 8:43





There are issues in your code. Please check.

– markus
Nov 22 '18 at 8:43












1 Answer
1






active

oldest

votes


















0














I don't know if I understand your problem.
But you can order your type using the factor's level.



sdi_data_2$type <- factor(sdi_data_2$type, levels = c("Circulatory, Respiratory, Endocrine, and Neoplasms","Nervous System","Injuries","Other Mental Health","Other and Unknown","Musculo-Skeletal","Intellectual Disability"))
sdi_data_2$Age <- factor(sdi_data_2$Age,levels = c("Under 50","Over 50"))


and then to plot:



ggplot() + geom_bar(aes(y = Beneficiaries, x = Age, fill = type), data = sdi_data_2, stat = "identity") + theme_bw()


This is the result:
enter image description here






share|improve this answer
























  • This is exactly what I needed, thank you!!

    – R Brown
    Nov 22 '18 at 19:14











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%2f53426374%2fchanging-order-in-stacked-bar-chart%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 don't know if I understand your problem.
But you can order your type using the factor's level.



sdi_data_2$type <- factor(sdi_data_2$type, levels = c("Circulatory, Respiratory, Endocrine, and Neoplasms","Nervous System","Injuries","Other Mental Health","Other and Unknown","Musculo-Skeletal","Intellectual Disability"))
sdi_data_2$Age <- factor(sdi_data_2$Age,levels = c("Under 50","Over 50"))


and then to plot:



ggplot() + geom_bar(aes(y = Beneficiaries, x = Age, fill = type), data = sdi_data_2, stat = "identity") + theme_bw()


This is the result:
enter image description here






share|improve this answer
























  • This is exactly what I needed, thank you!!

    – R Brown
    Nov 22 '18 at 19:14
















0














I don't know if I understand your problem.
But you can order your type using the factor's level.



sdi_data_2$type <- factor(sdi_data_2$type, levels = c("Circulatory, Respiratory, Endocrine, and Neoplasms","Nervous System","Injuries","Other Mental Health","Other and Unknown","Musculo-Skeletal","Intellectual Disability"))
sdi_data_2$Age <- factor(sdi_data_2$Age,levels = c("Under 50","Over 50"))


and then to plot:



ggplot() + geom_bar(aes(y = Beneficiaries, x = Age, fill = type), data = sdi_data_2, stat = "identity") + theme_bw()


This is the result:
enter image description here






share|improve this answer
























  • This is exactly what I needed, thank you!!

    – R Brown
    Nov 22 '18 at 19:14














0












0








0







I don't know if I understand your problem.
But you can order your type using the factor's level.



sdi_data_2$type <- factor(sdi_data_2$type, levels = c("Circulatory, Respiratory, Endocrine, and Neoplasms","Nervous System","Injuries","Other Mental Health","Other and Unknown","Musculo-Skeletal","Intellectual Disability"))
sdi_data_2$Age <- factor(sdi_data_2$Age,levels = c("Under 50","Over 50"))


and then to plot:



ggplot() + geom_bar(aes(y = Beneficiaries, x = Age, fill = type), data = sdi_data_2, stat = "identity") + theme_bw()


This is the result:
enter image description here






share|improve this answer













I don't know if I understand your problem.
But you can order your type using the factor's level.



sdi_data_2$type <- factor(sdi_data_2$type, levels = c("Circulatory, Respiratory, Endocrine, and Neoplasms","Nervous System","Injuries","Other Mental Health","Other and Unknown","Musculo-Skeletal","Intellectual Disability"))
sdi_data_2$Age <- factor(sdi_data_2$Age,levels = c("Under 50","Over 50"))


and then to plot:



ggplot() + geom_bar(aes(y = Beneficiaries, x = Age, fill = type), data = sdi_data_2, stat = "identity") + theme_bw()


This is the result:
enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 9:35









TheAvengerTheAvenger

14111




14111













  • This is exactly what I needed, thank you!!

    – R Brown
    Nov 22 '18 at 19:14



















  • This is exactly what I needed, thank you!!

    – R Brown
    Nov 22 '18 at 19:14

















This is exactly what I needed, thank you!!

– R Brown
Nov 22 '18 at 19:14





This is exactly what I needed, thank you!!

– R Brown
Nov 22 '18 at 19:14


















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%2f53426374%2fchanging-order-in-stacked-bar-chart%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'