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;
}







1















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:



plot without stacking



The plot with stacking looks like this:



plot with stacking



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?










share|improve this question

























  • 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











  • @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


















1















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:



plot without stacking



The plot with stacking looks like this:



plot with stacking



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?










share|improve this question

























  • 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











  • @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














1












1








1








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:



plot without stacking



The plot with stacking looks like this:



plot with stacking



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?










share|improve this question
















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:



plot without stacking



The plot with stacking looks like this:



plot with stacking



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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











  • @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













  • 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











  • @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












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
});


}
});














draft saved

draft discarded


















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
















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%2f53449808%2fposition-stack-changes-data-when-used-with-geom-line-in-ggplot2%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini