Changing order in stacked bar chart
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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.
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
add a comment |
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.
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
There are issues in your code. Please check.
– markus
Nov 22 '18 at 8:43
add a comment |
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.
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
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.
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
r ggplot2 bar-chart stacked-chart
edited Nov 22 '18 at 8:42
markus
15.6k11336
15.6k11336
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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:
This is exactly what I needed, thank you!!
– R Brown
Nov 22 '18 at 19:14
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%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
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:
This is exactly what I needed, thank you!!
– R Brown
Nov 22 '18 at 19:14
add a comment |
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:
This is exactly what I needed, thank you!!
– R Brown
Nov 22 '18 at 19:14
add a comment |
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:
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:
answered Nov 22 '18 at 9:35
TheAvengerTheAvenger
174112
174112
This is exactly what I needed, thank you!!
– R Brown
Nov 22 '18 at 19:14
add a comment |
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
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%2f53426374%2fchanging-order-in-stacked-bar-chart%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
There are issues in your code. Please check.
– markus
Nov 22 '18 at 8:43