R ggplot how to use shades of the same color in grouped barplots?












-2















I am trying to graph a grouped barplot, where the x-axis contains different states, and within each state, I have values for winter, spring, summer, fall. Basically, on the x-axis, I have 5 states, and each stage is a cluster of bars, and within each cluster I have 4 bars, one for each of winter, spring, summer, fall.



I want to vary the color of the 4 vars in each cluster so that they are different shades of the same color. Say I pick the color blue, I want winter to be the darkest shade of blue and fall to the be lightest shade of blue. I also, want the code to be flexible so it can accomodate more than 4 values within each cluster.



Here is the code I'm working with



# fill = different categories within a group
# x = grouping vars
# y = value (height of bar)
xvar = rlang::sym('states') #grouping var
yvar = rlang::sym('pct_aware')
xlabel = ('state')
ylabel = ('% of respondents')
graphtitle = 'awareness by segmentation'

ylim_max = 100

withingroupvar = rlang::sym('seasons')
legendtitle = "seasons"

p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
fill = factor(!!withingroupvar, levels =segorder_text)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
labs(x = xlabel, y = ylabel) +
ggtitle(graphtitle) +
geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
position = position_dodge(0.9), size=4) +
scale_fill_manual(name = legendtitle,
values = c('winter'="pink", 'spring' = "aquamarine", 'summer' = "lightblue", 'fall' = "salmon1"),
breaks=segorder_text) +
theme(text = element_text(size = 15)) + ylim(0,ylim_max)


Right now I'm manually assigning color values to each of the 4 seasons. I would like this to be automatic--I only have to pick the base color and R would assign different shades of the same color to each of the seasons.



How should I modify my code to achieve this?










share|improve this question


















  • 3





    We can't do much with your code if you don't provide any data.

    – jay.sf
    Nov 25 '18 at 9:14
















-2















I am trying to graph a grouped barplot, where the x-axis contains different states, and within each state, I have values for winter, spring, summer, fall. Basically, on the x-axis, I have 5 states, and each stage is a cluster of bars, and within each cluster I have 4 bars, one for each of winter, spring, summer, fall.



I want to vary the color of the 4 vars in each cluster so that they are different shades of the same color. Say I pick the color blue, I want winter to be the darkest shade of blue and fall to the be lightest shade of blue. I also, want the code to be flexible so it can accomodate more than 4 values within each cluster.



Here is the code I'm working with



# fill = different categories within a group
# x = grouping vars
# y = value (height of bar)
xvar = rlang::sym('states') #grouping var
yvar = rlang::sym('pct_aware')
xlabel = ('state')
ylabel = ('% of respondents')
graphtitle = 'awareness by segmentation'

ylim_max = 100

withingroupvar = rlang::sym('seasons')
legendtitle = "seasons"

p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
fill = factor(!!withingroupvar, levels =segorder_text)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
labs(x = xlabel, y = ylabel) +
ggtitle(graphtitle) +
geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
position = position_dodge(0.9), size=4) +
scale_fill_manual(name = legendtitle,
values = c('winter'="pink", 'spring' = "aquamarine", 'summer' = "lightblue", 'fall' = "salmon1"),
breaks=segorder_text) +
theme(text = element_text(size = 15)) + ylim(0,ylim_max)


Right now I'm manually assigning color values to each of the 4 seasons. I would like this to be automatic--I only have to pick the base color and R would assign different shades of the same color to each of the seasons.



How should I modify my code to achieve this?










share|improve this question


















  • 3





    We can't do much with your code if you don't provide any data.

    – jay.sf
    Nov 25 '18 at 9:14














-2












-2








-2








I am trying to graph a grouped barplot, where the x-axis contains different states, and within each state, I have values for winter, spring, summer, fall. Basically, on the x-axis, I have 5 states, and each stage is a cluster of bars, and within each cluster I have 4 bars, one for each of winter, spring, summer, fall.



I want to vary the color of the 4 vars in each cluster so that they are different shades of the same color. Say I pick the color blue, I want winter to be the darkest shade of blue and fall to the be lightest shade of blue. I also, want the code to be flexible so it can accomodate more than 4 values within each cluster.



Here is the code I'm working with



# fill = different categories within a group
# x = grouping vars
# y = value (height of bar)
xvar = rlang::sym('states') #grouping var
yvar = rlang::sym('pct_aware')
xlabel = ('state')
ylabel = ('% of respondents')
graphtitle = 'awareness by segmentation'

ylim_max = 100

withingroupvar = rlang::sym('seasons')
legendtitle = "seasons"

p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
fill = factor(!!withingroupvar, levels =segorder_text)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
labs(x = xlabel, y = ylabel) +
ggtitle(graphtitle) +
geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
position = position_dodge(0.9), size=4) +
scale_fill_manual(name = legendtitle,
values = c('winter'="pink", 'spring' = "aquamarine", 'summer' = "lightblue", 'fall' = "salmon1"),
breaks=segorder_text) +
theme(text = element_text(size = 15)) + ylim(0,ylim_max)


Right now I'm manually assigning color values to each of the 4 seasons. I would like this to be automatic--I only have to pick the base color and R would assign different shades of the same color to each of the seasons.



How should I modify my code to achieve this?










share|improve this question














I am trying to graph a grouped barplot, where the x-axis contains different states, and within each state, I have values for winter, spring, summer, fall. Basically, on the x-axis, I have 5 states, and each stage is a cluster of bars, and within each cluster I have 4 bars, one for each of winter, spring, summer, fall.



I want to vary the color of the 4 vars in each cluster so that they are different shades of the same color. Say I pick the color blue, I want winter to be the darkest shade of blue and fall to the be lightest shade of blue. I also, want the code to be flexible so it can accomodate more than 4 values within each cluster.



Here is the code I'm working with



# fill = different categories within a group
# x = grouping vars
# y = value (height of bar)
xvar = rlang::sym('states') #grouping var
yvar = rlang::sym('pct_aware')
xlabel = ('state')
ylabel = ('% of respondents')
graphtitle = 'awareness by segmentation'

ylim_max = 100

withingroupvar = rlang::sym('seasons')
legendtitle = "seasons"

p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
fill = factor(!!withingroupvar, levels =segorder_text)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
labs(x = xlabel, y = ylabel) +
ggtitle(graphtitle) +
geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
position = position_dodge(0.9), size=4) +
scale_fill_manual(name = legendtitle,
values = c('winter'="pink", 'spring' = "aquamarine", 'summer' = "lightblue", 'fall' = "salmon1"),
breaks=segorder_text) +
theme(text = element_text(size = 15)) + ylim(0,ylim_max)


Right now I'm manually assigning color values to each of the 4 seasons. I would like this to be automatic--I only have to pick the base color and R would assign different shades of the same color to each of the seasons.



How should I modify my code to achieve this?







r ggplot2 colors grouping bar-chart






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 9:07









AmazonianAmazonian

22329




22329








  • 3





    We can't do much with your code if you don't provide any data.

    – jay.sf
    Nov 25 '18 at 9:14














  • 3





    We can't do much with your code if you don't provide any data.

    – jay.sf
    Nov 25 '18 at 9:14








3




3





We can't do much with your code if you don't provide any data.

– jay.sf
Nov 25 '18 at 9:14





We can't do much with your code if you don't provide any data.

– jay.sf
Nov 25 '18 at 9:14












1 Answer
1






active

oldest

votes


















2














I would define a color palette



#define color palette
color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));


and I would refer to this color palette in scale_fill_manual.



... scale_fill_manual(name = legendtitle, values = color_palette) + ...


Then your code would look like this:



# fill = different categories within a group
# x = grouping vars
# y = value (height of bar)
xvar = rlang::sym('states') #grouping var
yvar = rlang::sym('pct_aware')
xlabel = ('state')
ylabel = ('% of respondents')
graphtitle = 'awareness by segmentation'

ylim_max = 100

withingroupvar = rlang::sym('seasons')
legendtitle = "seasons"

#define color palette
color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));

p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
fill = factor(!!withingroupvar, levels =segorder_text)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
labs(x = xlabel, y = ylabel) +
ggtitle(graphtitle) +
geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
position = position_dodge(0.9), size=4) +
scale_fill_manual(name = legendtitle, values = color_palette) +
theme(text = element_text(size = 15)) + ylim(0,ylim_max)





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%2f53466060%2fr-ggplot-how-to-use-shades-of-the-same-color-in-grouped-barplots%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














    I would define a color palette



    #define color palette
    color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));


    and I would refer to this color palette in scale_fill_manual.



    ... scale_fill_manual(name = legendtitle, values = color_palette) + ...


    Then your code would look like this:



    # fill = different categories within a group
    # x = grouping vars
    # y = value (height of bar)
    xvar = rlang::sym('states') #grouping var
    yvar = rlang::sym('pct_aware')
    xlabel = ('state')
    ylabel = ('% of respondents')
    graphtitle = 'awareness by segmentation'

    ylim_max = 100

    withingroupvar = rlang::sym('seasons')
    legendtitle = "seasons"

    #define color palette
    color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));

    p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
    fill = factor(!!withingroupvar, levels =segorder_text)))
    p + geom_bar(stat = "identity",
    position = position_dodge(0.9)) +
    labs(x = xlabel, y = ylabel) +
    ggtitle(graphtitle) +
    geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
    position = position_dodge(0.9), size=4) +
    scale_fill_manual(name = legendtitle, values = color_palette) +
    theme(text = element_text(size = 15)) + ylim(0,ylim_max)





    share|improve this answer




























      2














      I would define a color palette



      #define color palette
      color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));


      and I would refer to this color palette in scale_fill_manual.



      ... scale_fill_manual(name = legendtitle, values = color_palette) + ...


      Then your code would look like this:



      # fill = different categories within a group
      # x = grouping vars
      # y = value (height of bar)
      xvar = rlang::sym('states') #grouping var
      yvar = rlang::sym('pct_aware')
      xlabel = ('state')
      ylabel = ('% of respondents')
      graphtitle = 'awareness by segmentation'

      ylim_max = 100

      withingroupvar = rlang::sym('seasons')
      legendtitle = "seasons"

      #define color palette
      color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));

      p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
      fill = factor(!!withingroupvar, levels =segorder_text)))
      p + geom_bar(stat = "identity",
      position = position_dodge(0.9)) +
      labs(x = xlabel, y = ylabel) +
      ggtitle(graphtitle) +
      geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
      position = position_dodge(0.9), size=4) +
      scale_fill_manual(name = legendtitle, values = color_palette) +
      theme(text = element_text(size = 15)) + ylim(0,ylim_max)





      share|improve this answer


























        2












        2








        2







        I would define a color palette



        #define color palette
        color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));


        and I would refer to this color palette in scale_fill_manual.



        ... scale_fill_manual(name = legendtitle, values = color_palette) + ...


        Then your code would look like this:



        # fill = different categories within a group
        # x = grouping vars
        # y = value (height of bar)
        xvar = rlang::sym('states') #grouping var
        yvar = rlang::sym('pct_aware')
        xlabel = ('state')
        ylabel = ('% of respondents')
        graphtitle = 'awareness by segmentation'

        ylim_max = 100

        withingroupvar = rlang::sym('seasons')
        legendtitle = "seasons"

        #define color palette
        color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));

        p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
        fill = factor(!!withingroupvar, levels =segorder_text)))
        p + geom_bar(stat = "identity",
        position = position_dodge(0.9)) +
        labs(x = xlabel, y = ylabel) +
        ggtitle(graphtitle) +
        geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
        position = position_dodge(0.9), size=4) +
        scale_fill_manual(name = legendtitle, values = color_palette) +
        theme(text = element_text(size = 15)) + ylim(0,ylim_max)





        share|improve this answer













        I would define a color palette



        #define color palette
        color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));


        and I would refer to this color palette in scale_fill_manual.



        ... scale_fill_manual(name = legendtitle, values = color_palette) + ...


        Then your code would look like this:



        # fill = different categories within a group
        # x = grouping vars
        # y = value (height of bar)
        xvar = rlang::sym('states') #grouping var
        yvar = rlang::sym('pct_aware')
        xlabel = ('state')
        ylabel = ('% of respondents')
        graphtitle = 'awareness by segmentation'

        ylim_max = 100

        withingroupvar = rlang::sym('seasons')
        legendtitle = "seasons"

        #define color palette
        color_palette <- colorRampPalette(colors = c("steelblue", "darkblue"))(length(segorder_text));

        p <- ggplot(data = graphinput, aes(x = factor(!!xvar, levels = jobboards), y = !!yvar,
        fill = factor(!!withingroupvar, levels =segorder_text)))
        p + geom_bar(stat = "identity",
        position = position_dodge(0.9)) +
        labs(x = xlabel, y = ylabel) +
        ggtitle(graphtitle) +
        geom_text(aes(label=round(!!yvar,0)), vjust=-0.6, color="black",
        position = position_dodge(0.9), size=4) +
        scale_fill_manual(name = legendtitle, values = color_palette) +
        theme(text = element_text(size = 15)) + ylim(0,ylim_max)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 12:02









        Franziska W.Franziska W.

        14114




        14114






























            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%2f53466060%2fr-ggplot-how-to-use-shades-of-the-same-color-in-grouped-barplots%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

            Lallio

            Unable to find Lightning Node

            Futebolista