position_stack() changes data when used with geom_line() in ggplot2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I would like to stack several geom_line() plots one above the other. However they appeared with changed data.
Here is an example:
# make 3 data.frame with some random data
x <- seq(5, 15, length = 1000)
data1 <- data.frame(x = x, y = dnorm(x, mean = 10, sd = 3), sample = "1")
data2 <- data.frame(x = x, y = dnorm(x, mean = 7.5, sd = 3), sample = "2")
data3 <- data.frame(x = x, y = dnorm(x, mean = 12.5, sd = 1), sample = "3")
# bind data
data <- bind_rows(data1, data2, data3)
# plot data without stacking
plot.data <- data %>% ggplot(mapping = aes(x = x, y = y, color = sample)) + geom_line()
# plot data with stacking
plot.data <- data %>% ggplot(mapping = aes(x = x, y = y, color = sample)) + geom_line(position = position_stack(vjust = 1, reverse = T))
The plot without stacking looks like this:
The plot with stacking looks like this:
So it seems that position_stack sums the data, not shifts them to some constnant value, which is not expected behaviour for geom_line in my opinion. Could you suggest how to make the plots to be just shifted one above the other?
r ggplot2
add a comment |
I would like to stack several geom_line() plots one above the other. However they appeared with changed data.
Here is an example:
# make 3 data.frame with some random data
x <- seq(5, 15, length = 1000)
data1 <- data.frame(x = x, y = dnorm(x, mean = 10, sd = 3), sample = "1")
data2 <- data.frame(x = x, y = dnorm(x, mean = 7.5, sd = 3), sample = "2")
data3 <- data.frame(x = x, y = dnorm(x, mean = 12.5, sd = 1), sample = "3")
# bind data
data <- bind_rows(data1, data2, data3)
# plot data without stacking
plot.data <- data %>% ggplot(mapping = aes(x = x, y = y, color = sample)) + geom_line()
# plot data with stacking
plot.data <- data %>% ggplot(mapping = aes(x = x, y = y, color = sample)) + geom_line(position = position_stack(vjust = 1, reverse = T))
The plot without stacking looks like this:
The plot with stacking looks like this:
So it seems that position_stack sums the data, not shifts them to some constnant value, which is not expected behaviour for geom_line in my opinion. Could you suggest how to make the plots to be just shifted one above the other?
r ggplot2
Is this an acceptable alternative? :)ggplot(data, mapping = aes(x = x, y = y, color=sample)) + geom_line() + facet_wrap(~sample, ncol=1)
– Jonny Phelps
Nov 23 '18 at 16:22
Yeah that is exactly whatposition_stack
is supposed to do! It literally stacks the lines on top of each other, more obviously seen when you usegeom_area
. I agree with Johnny that facetting is probably what you want!
– Axeman
Nov 23 '18 at 18:48
@JonnyPhelps thanks for your answer, but this is not exactly what I want. Actually I want to compare several spectra and it is easier to do this when they are on the same plot but shifted by some y value, not on different plots with extra space used by ggplot's facet_wrap
– Denis
Nov 26 '18 at 15:39
@Axeman thanks for clarification. Seems that I'm spoiled by Origin software where stacking means shifting graphs by const value, not summing them up :) as far as I understood I need to do this by hand in R
– Denis
Nov 26 '18 at 15:43
You can remove a lot of the space with facet_wrap e.g. stackoverflow.com/questions/32426951/…
– Jonny Phelps
Nov 26 '18 at 16:01
add a comment |
I would like to stack several geom_line() plots one above the other. However they appeared with changed data.
Here is an example:
# make 3 data.frame with some random data
x <- seq(5, 15, length = 1000)
data1 <- data.frame(x = x, y = dnorm(x, mean = 10, sd = 3), sample = "1")
data2 <- data.frame(x = x, y = dnorm(x, mean = 7.5, sd = 3), sample = "2")
data3 <- data.frame(x = x, y = dnorm(x, mean = 12.5, sd = 1), sample = "3")
# bind data
data <- bind_rows(data1, data2, data3)
# plot data without stacking
plot.data <- data %>% ggplot(mapping = aes(x = x, y = y, color = sample)) + geom_line()
# plot data with stacking
plot.data <- data %>% ggplot(mapping = aes(x = x, y = y, color = sample)) + geom_line(position = position_stack(vjust = 1, reverse = T))
The plot without stacking looks like this:
The plot with stacking looks like this:
So it seems that position_stack sums the data, not shifts them to some constnant value, which is not expected behaviour for geom_line in my opinion. Could you suggest how to make the plots to be just shifted one above the other?
r ggplot2
I would like to stack several geom_line() plots one above the other. However they appeared with changed data.
Here is an example:
# make 3 data.frame with some random data
x <- seq(5, 15, length = 1000)
data1 <- data.frame(x = x, y = dnorm(x, mean = 10, sd = 3), sample = "1")
data2 <- data.frame(x = x, y = dnorm(x, mean = 7.5, sd = 3), sample = "2")
data3 <- data.frame(x = x, y = dnorm(x, mean = 12.5, sd = 1), sample = "3")
# bind data
data <- bind_rows(data1, data2, data3)
# plot data without stacking
plot.data <- data %>% ggplot(mapping = aes(x = x, y = y, color = sample)) + geom_line()
# plot data with stacking
plot.data <- data %>% ggplot(mapping = aes(x = x, y = y, color = sample)) + geom_line(position = position_stack(vjust = 1, reverse = T))
The plot without stacking looks like this:
The plot with stacking looks like this:
So it seems that position_stack sums the data, not shifts them to some constnant value, which is not expected behaviour for geom_line in my opinion. Could you suggest how to make the plots to be just shifted one above the other?
r ggplot2
r ggplot2
edited Nov 23 '18 at 17:03
antoine-sac
2,78321442
2,78321442
asked Nov 23 '18 at 16:04
DenisDenis
61
61
Is this an acceptable alternative? :)ggplot(data, mapping = aes(x = x, y = y, color=sample)) + geom_line() + facet_wrap(~sample, ncol=1)
– Jonny Phelps
Nov 23 '18 at 16:22
Yeah that is exactly whatposition_stack
is supposed to do! It literally stacks the lines on top of each other, more obviously seen when you usegeom_area
. I agree with Johnny that facetting is probably what you want!
– Axeman
Nov 23 '18 at 18:48
@JonnyPhelps thanks for your answer, but this is not exactly what I want. Actually I want to compare several spectra and it is easier to do this when they are on the same plot but shifted by some y value, not on different plots with extra space used by ggplot's facet_wrap
– Denis
Nov 26 '18 at 15:39
@Axeman thanks for clarification. Seems that I'm spoiled by Origin software where stacking means shifting graphs by const value, not summing them up :) as far as I understood I need to do this by hand in R
– Denis
Nov 26 '18 at 15:43
You can remove a lot of the space with facet_wrap e.g. stackoverflow.com/questions/32426951/…
– Jonny Phelps
Nov 26 '18 at 16:01
add a comment |
Is this an acceptable alternative? :)ggplot(data, mapping = aes(x = x, y = y, color=sample)) + geom_line() + facet_wrap(~sample, ncol=1)
– Jonny Phelps
Nov 23 '18 at 16:22
Yeah that is exactly whatposition_stack
is supposed to do! It literally stacks the lines on top of each other, more obviously seen when you usegeom_area
. I agree with Johnny that facetting is probably what you want!
– Axeman
Nov 23 '18 at 18:48
@JonnyPhelps thanks for your answer, but this is not exactly what I want. Actually I want to compare several spectra and it is easier to do this when they are on the same plot but shifted by some y value, not on different plots with extra space used by ggplot's facet_wrap
– Denis
Nov 26 '18 at 15:39
@Axeman thanks for clarification. Seems that I'm spoiled by Origin software where stacking means shifting graphs by const value, not summing them up :) as far as I understood I need to do this by hand in R
– Denis
Nov 26 '18 at 15:43
You can remove a lot of the space with facet_wrap e.g. stackoverflow.com/questions/32426951/…
– Jonny Phelps
Nov 26 '18 at 16:01
Is this an acceptable alternative? :)
ggplot(data, mapping = aes(x = x, y = y, color=sample)) + geom_line() + facet_wrap(~sample, ncol=1)
– Jonny Phelps
Nov 23 '18 at 16:22
Is this an acceptable alternative? :)
ggplot(data, mapping = aes(x = x, y = y, color=sample)) + geom_line() + facet_wrap(~sample, ncol=1)
– Jonny Phelps
Nov 23 '18 at 16:22
Yeah that is exactly what
position_stack
is supposed to do! It literally stacks the lines on top of each other, more obviously seen when you use geom_area
. I agree with Johnny that facetting is probably what you want!– Axeman
Nov 23 '18 at 18:48
Yeah that is exactly what
position_stack
is supposed to do! It literally stacks the lines on top of each other, more obviously seen when you use geom_area
. I agree with Johnny that facetting is probably what you want!– Axeman
Nov 23 '18 at 18:48
@JonnyPhelps thanks for your answer, but this is not exactly what I want. Actually I want to compare several spectra and it is easier to do this when they are on the same plot but shifted by some y value, not on different plots with extra space used by ggplot's facet_wrap
– Denis
Nov 26 '18 at 15:39
@JonnyPhelps thanks for your answer, but this is not exactly what I want. Actually I want to compare several spectra and it is easier to do this when they are on the same plot but shifted by some y value, not on different plots with extra space used by ggplot's facet_wrap
– Denis
Nov 26 '18 at 15:39
@Axeman thanks for clarification. Seems that I'm spoiled by Origin software where stacking means shifting graphs by const value, not summing them up :) as far as I understood I need to do this by hand in R
– Denis
Nov 26 '18 at 15:43
@Axeman thanks for clarification. Seems that I'm spoiled by Origin software where stacking means shifting graphs by const value, not summing them up :) as far as I understood I need to do this by hand in R
– Denis
Nov 26 '18 at 15:43
You can remove a lot of the space with facet_wrap e.g. stackoverflow.com/questions/32426951/…
– Jonny Phelps
Nov 26 '18 at 16:01
You can remove a lot of the space with facet_wrap e.g. stackoverflow.com/questions/32426951/…
– Jonny Phelps
Nov 26 '18 at 16:01
add a comment |
0
active
oldest
votes
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%2f53449808%2fposition-stack-changes-data-when-used-with-geom-line-in-ggplot2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53449808%2fposition-stack-changes-data-when-used-with-geom-line-in-ggplot2%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
Is this an acceptable alternative? :)
ggplot(data, mapping = aes(x = x, y = y, color=sample)) + geom_line() + facet_wrap(~sample, ncol=1)
– Jonny Phelps
Nov 23 '18 at 16:22
Yeah that is exactly what
position_stack
is supposed to do! It literally stacks the lines on top of each other, more obviously seen when you usegeom_area
. I agree with Johnny that facetting is probably what you want!– Axeman
Nov 23 '18 at 18:48
@JonnyPhelps thanks for your answer, but this is not exactly what I want. Actually I want to compare several spectra and it is easier to do this when they are on the same plot but shifted by some y value, not on different plots with extra space used by ggplot's facet_wrap
– Denis
Nov 26 '18 at 15:39
@Axeman thanks for clarification. Seems that I'm spoiled by Origin software where stacking means shifting graphs by const value, not summing them up :) as far as I understood I need to do this by hand in R
– Denis
Nov 26 '18 at 15:43
You can remove a lot of the space with facet_wrap e.g. stackoverflow.com/questions/32426951/…
– Jonny Phelps
Nov 26 '18 at 16:01