ggplot2 How to align graphs with equal y scale as percentage





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I'm trying to obtain 2 aligned bar plots with percentage scales of 2 different factors. The y scales calculated as percent are different. I would like to have the same y scales for both plots, for example from 0 to 40% in both. I've tried ylim() which doesn't work on the percentage scale. Example below



library(ggplot2)
library(scales)
data("diamonds")


First bar-plot for cut



  p<- ggplot(diamonds, aes(x = cut)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Cut") +
ylab("Percent") +
theme(legend.position="bottom")


Second bar-plot for clarity



p1<- ggplot(diamonds, aes(x = clarity)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=clarity)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Clarity") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Clarity") +
ylab("Percent") +
theme(legend.position="bottom")


Arranging bar-plot with different scales



  grid.arrange(p,p1, ncol = 2)  


different scales but I would like for example both at 40% top



enter image description here



If scales weren't percentages I would do this:



p<- ggplot(diamonds, aes(x = cut)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
xlab("Cut") +
ylab("Percent") +
ylim(0, 40)
theme(legend.position="bottom")


But here, of course, it doesn't work and returns this:



enter image description here










share|improve this question

























  • use ylim(0, 0.4)

    – Richard Telford
    Nov 16 '18 at 12:47











  • Thanks! definitely better but I lose the scale as integer and % which I kind of liked. Any idea how to preserve that?

    – Mia
    Nov 16 '18 at 13:06











  • The ylim replaced the existing scale - your solution fixed that

    – Richard Telford
    Nov 16 '18 at 15:32


















1















I'm trying to obtain 2 aligned bar plots with percentage scales of 2 different factors. The y scales calculated as percent are different. I would like to have the same y scales for both plots, for example from 0 to 40% in both. I've tried ylim() which doesn't work on the percentage scale. Example below



library(ggplot2)
library(scales)
data("diamonds")


First bar-plot for cut



  p<- ggplot(diamonds, aes(x = cut)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Cut") +
ylab("Percent") +
theme(legend.position="bottom")


Second bar-plot for clarity



p1<- ggplot(diamonds, aes(x = clarity)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=clarity)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Clarity") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Clarity") +
ylab("Percent") +
theme(legend.position="bottom")


Arranging bar-plot with different scales



  grid.arrange(p,p1, ncol = 2)  


different scales but I would like for example both at 40% top



enter image description here



If scales weren't percentages I would do this:



p<- ggplot(diamonds, aes(x = cut)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
xlab("Cut") +
ylab("Percent") +
ylim(0, 40)
theme(legend.position="bottom")


But here, of course, it doesn't work and returns this:



enter image description here










share|improve this question

























  • use ylim(0, 0.4)

    – Richard Telford
    Nov 16 '18 at 12:47











  • Thanks! definitely better but I lose the scale as integer and % which I kind of liked. Any idea how to preserve that?

    – Mia
    Nov 16 '18 at 13:06











  • The ylim replaced the existing scale - your solution fixed that

    – Richard Telford
    Nov 16 '18 at 15:32














1












1








1








I'm trying to obtain 2 aligned bar plots with percentage scales of 2 different factors. The y scales calculated as percent are different. I would like to have the same y scales for both plots, for example from 0 to 40% in both. I've tried ylim() which doesn't work on the percentage scale. Example below



library(ggplot2)
library(scales)
data("diamonds")


First bar-plot for cut



  p<- ggplot(diamonds, aes(x = cut)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Cut") +
ylab("Percent") +
theme(legend.position="bottom")


Second bar-plot for clarity



p1<- ggplot(diamonds, aes(x = clarity)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=clarity)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Clarity") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Clarity") +
ylab("Percent") +
theme(legend.position="bottom")


Arranging bar-plot with different scales



  grid.arrange(p,p1, ncol = 2)  


different scales but I would like for example both at 40% top



enter image description here



If scales weren't percentages I would do this:



p<- ggplot(diamonds, aes(x = cut)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
xlab("Cut") +
ylab("Percent") +
ylim(0, 40)
theme(legend.position="bottom")


But here, of course, it doesn't work and returns this:



enter image description here










share|improve this question
















I'm trying to obtain 2 aligned bar plots with percentage scales of 2 different factors. The y scales calculated as percent are different. I would like to have the same y scales for both plots, for example from 0 to 40% in both. I've tried ylim() which doesn't work on the percentage scale. Example below



library(ggplot2)
library(scales)
data("diamonds")


First bar-plot for cut



  p<- ggplot(diamonds, aes(x = cut)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Cut") +
ylab("Percent") +
theme(legend.position="bottom")


Second bar-plot for clarity



p1<- ggplot(diamonds, aes(x = clarity)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=clarity)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label =
scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Clarity") + theme(plot.title = element_text(hjust = 0.5, size=14,
face="bold")) +
xlab("Clarity") +
ylab("Percent") +
theme(legend.position="bottom")


Arranging bar-plot with different scales



  grid.arrange(p,p1, ncol = 2)  


different scales but I would like for example both at 40% top



enter image description here



If scales weren't percentages I would do this:



p<- ggplot(diamonds, aes(x = cut)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
scale_y_continuous(labels = percent) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
xlab("Cut") +
ylab("Percent") +
ylim(0, 40)
theme(legend.position="bottom")


But here, of course, it doesn't work and returns this:



enter image description here







r ggplot2 bar-chart percentage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 22:07









aosmith

21.7k44671




21.7k44671










asked Nov 16 '18 at 12:24









MiaMia

634




634













  • use ylim(0, 0.4)

    – Richard Telford
    Nov 16 '18 at 12:47











  • Thanks! definitely better but I lose the scale as integer and % which I kind of liked. Any idea how to preserve that?

    – Mia
    Nov 16 '18 at 13:06











  • The ylim replaced the existing scale - your solution fixed that

    – Richard Telford
    Nov 16 '18 at 15:32



















  • use ylim(0, 0.4)

    – Richard Telford
    Nov 16 '18 at 12:47











  • Thanks! definitely better but I lose the scale as integer and % which I kind of liked. Any idea how to preserve that?

    – Mia
    Nov 16 '18 at 13:06











  • The ylim replaced the existing scale - your solution fixed that

    – Richard Telford
    Nov 16 '18 at 15:32

















use ylim(0, 0.4)

– Richard Telford
Nov 16 '18 at 12:47





use ylim(0, 0.4)

– Richard Telford
Nov 16 '18 at 12:47













Thanks! definitely better but I lose the scale as integer and % which I kind of liked. Any idea how to preserve that?

– Mia
Nov 16 '18 at 13:06





Thanks! definitely better but I lose the scale as integer and % which I kind of liked. Any idea how to preserve that?

– Mia
Nov 16 '18 at 13:06













The ylim replaced the existing scale - your solution fixed that

– Richard Telford
Nov 16 '18 at 15:32





The ylim replaced the existing scale - your solution fixed that

– Richard Telford
Nov 16 '18 at 15:32












1 Answer
1






active

oldest

votes


















2














Ok I found a way, here the code for Cut for a % scale limited to 60%



p<- ggplot(diamonds, aes(x = cut)) +  
geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
stat = "count", vjust = -0.25) +
ggtitle("Diamonds Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
scale_y_continuous(labels = scales::percent, limits=c(0,0.6)) + labs(y="Percent")
xlab("Cut") +
theme(legend.position="bottom")
p


enter image description here






share|improve this answer
























    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%2f53337884%2fggplot2-how-to-align-graphs-with-equal-y-scale-as-percentage%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









    2














    Ok I found a way, here the code for Cut for a % scale limited to 60%



    p<- ggplot(diamonds, aes(x = cut)) +  
    geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
    geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
    stat = "count", vjust = -0.25) +
    ggtitle("Diamonds Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
    scale_y_continuous(labels = scales::percent, limits=c(0,0.6)) + labs(y="Percent")
    xlab("Cut") +
    theme(legend.position="bottom")
    p


    enter image description here






    share|improve this answer




























      2














      Ok I found a way, here the code for Cut for a % scale limited to 60%



      p<- ggplot(diamonds, aes(x = cut)) +  
      geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
      geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
      stat = "count", vjust = -0.25) +
      ggtitle("Diamonds Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
      scale_y_continuous(labels = scales::percent, limits=c(0,0.6)) + labs(y="Percent")
      xlab("Cut") +
      theme(legend.position="bottom")
      p


      enter image description here






      share|improve this answer


























        2












        2








        2







        Ok I found a way, here the code for Cut for a % scale limited to 60%



        p<- ggplot(diamonds, aes(x = cut)) +  
        geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
        geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
        stat = "count", vjust = -0.25) +
        ggtitle("Diamonds Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
        scale_y_continuous(labels = scales::percent, limits=c(0,0.6)) + labs(y="Percent")
        xlab("Cut") +
        theme(legend.position="bottom")
        p


        enter image description here






        share|improve this answer













        Ok I found a way, here the code for Cut for a % scale limited to 60%



        p<- ggplot(diamonds, aes(x = cut)) +  
        geom_bar(aes(y = (..count..)/sum(..count..), fill=cut)) +
        geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))),
        stat = "count", vjust = -0.25) +
        ggtitle("Diamonds Cut") + theme(plot.title = element_text(hjust = 0.5, size=14, face="bold")) +
        scale_y_continuous(labels = scales::percent, limits=c(0,0.6)) + labs(y="Percent")
        xlab("Cut") +
        theme(legend.position="bottom")
        p


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 13:41









        MiaMia

        634




        634
































            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%2f53337884%2fggplot2-how-to-align-graphs-with-equal-y-scale-as-percentage%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

            Bressuire

            Vorschmack

            Quarantine