ggplot stacked bar chart issue
I have the following data
library(ggplot2)
d <- data.frame(
Type= c("t1", "t2", "t3", "t4"),
value= c(14000, 2500, 145, 900))
I want to create a horizontal stacked bar chart. I have the following code:
ggplot(data = d, aes(x=Type, fill=value) ) + geom_bar()
However this doesn't give me what I want. I just need one bar with the values stacked on top of one another. Any help would be really appreciated.
Thanks
r ggplot2
|
show 2 more comments
I have the following data
library(ggplot2)
d <- data.frame(
Type= c("t1", "t2", "t3", "t4"),
value= c(14000, 2500, 145, 900))
I want to create a horizontal stacked bar chart. I have the following code:
ggplot(data = d, aes(x=Type, fill=value) ) + geom_bar()
However this doesn't give me what I want. I just need one bar with the values stacked on top of one another. Any help would be really appreciated.
Thanks
r ggplot2
It'slibrary(ggplot2).
– Rui Barradas
Nov 20 '18 at 11:42
apologies, have edited to add more detail
– Mrmoleje
Nov 20 '18 at 11:44
1
What do you want to stack? The different types? Different values? Can you clarify what you expect on the x and y-axes? You could also draw the expected result in paint or whatever, or give a different example.
– kath
Nov 20 '18 at 11:46
1
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value.
– Rui Barradas
Nov 20 '18 at 11:50
2
Try thisd %>% mutate(Type2 = "type") %>% ggplot(aes(Type2, value, fill=Type)) + geom_bar(stat="identity") + coord_flip()
– AntoniosK
Nov 20 '18 at 11:53
|
show 2 more comments
I have the following data
library(ggplot2)
d <- data.frame(
Type= c("t1", "t2", "t3", "t4"),
value= c(14000, 2500, 145, 900))
I want to create a horizontal stacked bar chart. I have the following code:
ggplot(data = d, aes(x=Type, fill=value) ) + geom_bar()
However this doesn't give me what I want. I just need one bar with the values stacked on top of one another. Any help would be really appreciated.
Thanks
r ggplot2
I have the following data
library(ggplot2)
d <- data.frame(
Type= c("t1", "t2", "t3", "t4"),
value= c(14000, 2500, 145, 900))
I want to create a horizontal stacked bar chart. I have the following code:
ggplot(data = d, aes(x=Type, fill=value) ) + geom_bar()
However this doesn't give me what I want. I just need one bar with the values stacked on top of one another. Any help would be really appreciated.
Thanks
r ggplot2
r ggplot2
edited Nov 20 '18 at 11:44
Mrmoleje
asked Nov 20 '18 at 11:38
MrmolejeMrmoleje
11019
11019
It'slibrary(ggplot2).
– Rui Barradas
Nov 20 '18 at 11:42
apologies, have edited to add more detail
– Mrmoleje
Nov 20 '18 at 11:44
1
What do you want to stack? The different types? Different values? Can you clarify what you expect on the x and y-axes? You could also draw the expected result in paint or whatever, or give a different example.
– kath
Nov 20 '18 at 11:46
1
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value.
– Rui Barradas
Nov 20 '18 at 11:50
2
Try thisd %>% mutate(Type2 = "type") %>% ggplot(aes(Type2, value, fill=Type)) + geom_bar(stat="identity") + coord_flip()
– AntoniosK
Nov 20 '18 at 11:53
|
show 2 more comments
It'slibrary(ggplot2).
– Rui Barradas
Nov 20 '18 at 11:42
apologies, have edited to add more detail
– Mrmoleje
Nov 20 '18 at 11:44
1
What do you want to stack? The different types? Different values? Can you clarify what you expect on the x and y-axes? You could also draw the expected result in paint or whatever, or give a different example.
– kath
Nov 20 '18 at 11:46
1
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value.
– Rui Barradas
Nov 20 '18 at 11:50
2
Try thisd %>% mutate(Type2 = "type") %>% ggplot(aes(Type2, value, fill=Type)) + geom_bar(stat="identity") + coord_flip()
– AntoniosK
Nov 20 '18 at 11:53
It's
library(ggplot2).– Rui Barradas
Nov 20 '18 at 11:42
It's
library(ggplot2).– Rui Barradas
Nov 20 '18 at 11:42
apologies, have edited to add more detail
– Mrmoleje
Nov 20 '18 at 11:44
apologies, have edited to add more detail
– Mrmoleje
Nov 20 '18 at 11:44
1
1
What do you want to stack? The different types? Different values? Can you clarify what you expect on the x and y-axes? You could also draw the expected result in paint or whatever, or give a different example.
– kath
Nov 20 '18 at 11:46
What do you want to stack? The different types? Different values? Can you clarify what you expect on the x and y-axes? You could also draw the expected result in paint or whatever, or give a different example.
– kath
Nov 20 '18 at 11:46
1
1
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value.
– Rui Barradas
Nov 20 '18 at 11:50
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value.
– Rui Barradas
Nov 20 '18 at 11:50
2
2
Try this
d %>% mutate(Type2 = "type") %>% ggplot(aes(Type2, value, fill=Type)) + geom_bar(stat="identity") + coord_flip()– AntoniosK
Nov 20 '18 at 11:53
Try this
d %>% mutate(Type2 = "type") %>% ggplot(aes(Type2, value, fill=Type)) + geom_bar(stat="identity") + coord_flip()– AntoniosK
Nov 20 '18 at 11:53
|
show 2 more comments
1 Answer
1
active
oldest
votes
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value. That is solved with the dplyr pipe, that creates a temporary variable X.
Thanks to AntoniosK's comment for calling my atention to the OP's request for a horizontal bar.
library(ggplot2)
library(dplyr)
d %>%
mutate(X = factor(1)) %>%
ggplot(aes(x = X, y = value, fill = value) ) +
geom_bar(stat = "identity", width = 0.2) +
coord_flip()

3
Actually, there is no need to create a new variable. This also works:ggplot(data = d, aes(x = "1", y = value, fill = value) ) + geom_col(position = position_stack()) + coord_flip()
– kath
Nov 20 '18 at 12:04
@kath Right, thanks.
– Rui Barradas
Nov 20 '18 at 13:07
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%2f53392209%2fggplot-stacked-bar-chart-issue%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
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value. That is solved with the dplyr pipe, that creates a temporary variable X.
Thanks to AntoniosK's comment for calling my atention to the OP's request for a horizontal bar.
library(ggplot2)
library(dplyr)
d %>%
mutate(X = factor(1)) %>%
ggplot(aes(x = X, y = value, fill = value) ) +
geom_bar(stat = "identity", width = 0.2) +
coord_flip()

3
Actually, there is no need to create a new variable. This also works:ggplot(data = d, aes(x = "1", y = value, fill = value) ) + geom_col(position = position_stack()) + coord_flip()
– kath
Nov 20 '18 at 12:04
@kath Right, thanks.
– Rui Barradas
Nov 20 '18 at 13:07
add a comment |
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value. That is solved with the dplyr pipe, that creates a temporary variable X.
Thanks to AntoniosK's comment for calling my atention to the OP's request for a horizontal bar.
library(ggplot2)
library(dplyr)
d %>%
mutate(X = factor(1)) %>%
ggplot(aes(x = X, y = value, fill = value) ) +
geom_bar(stat = "identity", width = 0.2) +
coord_flip()

3
Actually, there is no need to create a new variable. This also works:ggplot(data = d, aes(x = "1", y = value, fill = value) ) + geom_col(position = position_stack()) + coord_flip()
– kath
Nov 20 '18 at 12:04
@kath Right, thanks.
– Rui Barradas
Nov 20 '18 at 13:07
add a comment |
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value. That is solved with the dplyr pipe, that creates a temporary variable X.
Thanks to AntoniosK's comment for calling my atention to the OP's request for a horizontal bar.
library(ggplot2)
library(dplyr)
d %>%
mutate(X = factor(1)) %>%
ggplot(aes(x = X, y = value, fill = value) ) +
geom_bar(stat = "identity", width = 0.2) +
coord_flip()

If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value. That is solved with the dplyr pipe, that creates a temporary variable X.
Thanks to AntoniosK's comment for calling my atention to the OP's request for a horizontal bar.
library(ggplot2)
library(dplyr)
d %>%
mutate(X = factor(1)) %>%
ggplot(aes(x = X, y = value, fill = value) ) +
geom_bar(stat = "identity", width = 0.2) +
coord_flip()

answered Nov 20 '18 at 11:54
Rui BarradasRui Barradas
17.2k51731
17.2k51731
3
Actually, there is no need to create a new variable. This also works:ggplot(data = d, aes(x = "1", y = value, fill = value) ) + geom_col(position = position_stack()) + coord_flip()
– kath
Nov 20 '18 at 12:04
@kath Right, thanks.
– Rui Barradas
Nov 20 '18 at 13:07
add a comment |
3
Actually, there is no need to create a new variable. This also works:ggplot(data = d, aes(x = "1", y = value, fill = value) ) + geom_col(position = position_stack()) + coord_flip()
– kath
Nov 20 '18 at 12:04
@kath Right, thanks.
– Rui Barradas
Nov 20 '18 at 13:07
3
3
Actually, there is no need to create a new variable. This also works:
ggplot(data = d, aes(x = "1", y = value, fill = value) ) + geom_col(position = position_stack()) + coord_flip()– kath
Nov 20 '18 at 12:04
Actually, there is no need to create a new variable. This also works:
ggplot(data = d, aes(x = "1", y = value, fill = value) ) + geom_col(position = position_stack()) + coord_flip()– kath
Nov 20 '18 at 12:04
@kath Right, thanks.
– Rui Barradas
Nov 20 '18 at 13:07
@kath Right, thanks.
– Rui Barradas
Nov 20 '18 at 13:07
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%2f53392209%2fggplot-stacked-bar-chart-issue%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
It's
library(ggplot2).– Rui Barradas
Nov 20 '18 at 11:42
apologies, have edited to add more detail
– Mrmoleje
Nov 20 '18 at 11:44
1
What do you want to stack? The different types? Different values? Can you clarify what you expect on the x and y-axes? You could also draw the expected result in paint or whatever, or give a different example.
– kath
Nov 20 '18 at 11:46
1
If you have several x axis values ("t1", "t2", etc) the bars will not be stacked. They will be only if they share the same x value.
– Rui Barradas
Nov 20 '18 at 11:50
2
Try this
d %>% mutate(Type2 = "type") %>% ggplot(aes(Type2, value, fill=Type)) + geom_bar(stat="identity") + coord_flip()– AntoniosK
Nov 20 '18 at 11:53