geom_rect and alpha - does this work with hard coded values?Unexpected Behavior of transparency parameter in geom_rectConditionally change panel background with facet_grid?Customize background to highlight ranges of data in ggplotHow do you control the translucence of geom_rect() rectanglesalpha in geom_segment not workingChange background color panel based on year in ggplot Rshade regions in R plotGgplot2 different alpha behaviourMark range in plot with transparent colorfaceting based on geom_point when ggplot() empty and multiple shaded geom_rect existAlpha transparency not working in ggplot2?geom_rect( ) not working over Dateshow do I use geom_rect with discrete axis valuesalpha does not change transparency but adds to ggplot2 legend with geom_rectgeom_rect not working with scale_y_log10() (ggplot2)alpha not working on facetted line graph with x-varying geom_rect()R - ggplot2: coord_radar not working with geom_rect or annotate('rect')Add a geom_rect to the plot background (not panel) in ggplot2ggplot alpha = 0 not workingggplot background in three different colours with geom_rect not working - with data and all code

affect vs effect

Why would guns not work in the dungeon?

How might the United Kingdom become a republic?

What's the point of this scene involving Flash Thompson at the airport?

Why did the Japanese attack the Aleutians at the same time as Midway?

What's the minimum number of sensors for a hobby GPS waypoint-following UAV?

Robbers: The Hidden OEIS Substring

Why does the autopilot disengage even when it does not receive pilot input?

Are neural networks prone to catastrophic forgetting?

How do Windows version numbers work?

Filtering fine silt/mud from water (not necessarily bacteria etc.)

Crowbar circuit causes unexpected behavior for op amp circuit

A DVR algebra with weird automorphisms

How do I determine whether a permit is required for a new gas line?

Can I play a first turn Simic Growth Chamber to have 3 mana available in the second turn?

Are there any double stars that I can actually see orbit each other?

How can I deal with a player trying to insert real-world mythology into my homebrew setting?

Where is the USB2 OTG port on the RPi 4 Model B located?

What's a moment that's more impactful on a reread called?

How do I take a fraction to a negative power?

Why can't supermassive black holes merge? (or can they?)

The monorail explodes before I can get on it

What is the difference between logical consistency and logical entailment in deductive logic?

What would be the ideal melee weapon made of "Phase Metal"?



geom_rect and alpha - does this work with hard coded values?


Unexpected Behavior of transparency parameter in geom_rectConditionally change panel background with facet_grid?Customize background to highlight ranges of data in ggplotHow do you control the translucence of geom_rect() rectanglesalpha in geom_segment not workingChange background color panel based on year in ggplot Rshade regions in R plotGgplot2 different alpha behaviourMark range in plot with transparent colorfaceting based on geom_point when ggplot() empty and multiple shaded geom_rect existAlpha transparency not working in ggplot2?geom_rect( ) not working over Dateshow do I use geom_rect with discrete axis valuesalpha does not change transparency but adds to ggplot2 legend with geom_rectgeom_rect not working with scale_y_log10() (ggplot2)alpha not working on facetted line graph with x-varying geom_rect()R - ggplot2: coord_radar not working with geom_rect or annotate('rect')Add a geom_rect to the plot background (not panel) in ggplot2ggplot alpha = 0 not workingggplot background in three different colours with geom_rect not working - with data and all code






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








46















Same title, completely reworded the question though.



Why does the alpha work in the first plot but not the second? I'm struggling to see why with hardcoded values the rect is drawn in the right place but not made transparent but when in a data.frame it works as expected?



mtcars$cyl <- factor(mtcars$cyl)
mtcars$am <- factor(mtcars$am)

ggplot(mtcars) +
geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
geom_rect(data=data.frame(xmin=100, xmax=200, ymin=0, ymax=Inf), aes(xmin=xmin, xmax=xmax, ymin=ymin,ymax=ymax), fill="red", alpha=0.2)

ggplot(mtcars) +
geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)









share|improve this question






























    46















    Same title, completely reworded the question though.



    Why does the alpha work in the first plot but not the second? I'm struggling to see why with hardcoded values the rect is drawn in the right place but not made transparent but when in a data.frame it works as expected?



    mtcars$cyl <- factor(mtcars$cyl)
    mtcars$am <- factor(mtcars$am)

    ggplot(mtcars) +
    geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
    geom_rect(data=data.frame(xmin=100, xmax=200, ymin=0, ymax=Inf), aes(xmin=xmin, xmax=xmax, ymin=ymin,ymax=ymax), fill="red", alpha=0.2)

    ggplot(mtcars) +
    geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
    geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)









    share|improve this question


























      46












      46








      46


      15






      Same title, completely reworded the question though.



      Why does the alpha work in the first plot but not the second? I'm struggling to see why with hardcoded values the rect is drawn in the right place but not made transparent but when in a data.frame it works as expected?



      mtcars$cyl <- factor(mtcars$cyl)
      mtcars$am <- factor(mtcars$am)

      ggplot(mtcars) +
      geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
      geom_rect(data=data.frame(xmin=100, xmax=200, ymin=0, ymax=Inf), aes(xmin=xmin, xmax=xmax, ymin=ymin,ymax=ymax), fill="red", alpha=0.2)

      ggplot(mtcars) +
      geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
      geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)









      share|improve this question
















      Same title, completely reworded the question though.



      Why does the alpha work in the first plot but not the second? I'm struggling to see why with hardcoded values the rect is drawn in the right place but not made transparent but when in a data.frame it works as expected?



      mtcars$cyl <- factor(mtcars$cyl)
      mtcars$am <- factor(mtcars$am)

      ggplot(mtcars) +
      geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
      geom_rect(data=data.frame(xmin=100, xmax=200, ymin=0, ymax=Inf), aes(xmin=xmin, xmax=xmax, ymin=ymin,ymax=ymax), fill="red", alpha=0.2)

      ggplot(mtcars) +
      geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
      geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)






      r ggplot2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 16 '13 at 2:12







      nzcoops

















      asked Jul 8 '13 at 7:48









      nzcoopsnzcoops

      5,3897 gold badges34 silver badges49 bronze badges




      5,3897 gold badges34 silver badges49 bronze badges






















          4 Answers
          4






          active

          oldest

          votes


















          93














          Thanks for clarifying your question. This was puzzling to me, so I went to google, and ended up learning something new (after working around some vagaries in their examples). Apparently what you are doing is drawing many rectangles on top of each other, effectively nullifying the semi-transparency you want. So, the only ways to overcome this are to hard-code the rectangle coordinates in a separate df, or...



          ggplot() + 
          geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
          geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), alpha=0.2, fill="red")


          ... just don't assign your data.frame globally to the plot. Instead, only use it in the layer(s) you want (in this example, geom_density), and leave the other layers df-free! Or, even better yet, Use annotate to modify your plot out from under the default df:



          ggplot(mtcars) + 
          geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
          annotate("rect", xmin=100, xmax=200, ymin=0, ymax=Inf, alpha=0.2, fill="red")


          The latter method enables you to use a single data.frame for the entire plot, so you don't have to specify the same df for each layer.



          Both methods return identical plots:



          enter image description here






          share|improve this answer


















          • 2





            Just an add on to this. If you are using this in conjunction with scale_y_continuous (say limits=c(0.005,0.015), so specify the break points exactly, you can't have (ymin=0, ymax=Inf), your min/max values have to fall within the scale limits.

            – nzcoops
            Sep 6 '13 at 5:10











          • +1 This explains all the alpha specification problems I've ever had with ggplot!

            – geotheory
            Dec 21 '13 at 15:26







          • 2





            This is great! But I still don't understand why geom_rect(..., alpha=.1) doesn't work but annotate("rect", ... alpha=.1) does.

            – Stuart
            Jul 22 '14 at 22:15







          • 1





            @Stuart, the geom_rect() call will not produce the desired alpha value because, as @sc_evans explained, it creates one layer for each row of the data frame. In contrast annotate() is a special layer that is not mapped to the data frame so only one rectangle will be drawn (see the help file for annotate())

            – Stefan Avey
            Dec 7 '16 at 16:37











          • The alpha=0.2 doesn't work for me...no matter what the alpha is (between 0,1) there is no transparency. Why?

            – Hercules Apergis
            May 14 '18 at 15:56


















          15














          Another workaround is to give geom_rect a single row data object to ensure only one rectangle is drawn:



          ggplot(mtcars) +
          geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
          geom_rect(data=mtcars[1,], aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)


          enter image description here






          share|improve this answer























          • This is the solution you are looking for if you are faceting. Thanks!

            – Nova
            Apr 7 '17 at 13:59


















          2














          ggplot(df, aes(xmin = x, xmax = x + 1, ymin = y, ymax = y + 2)) +
          geom_rect(alpha=.2) +
          geom_rect(data=data.frame(xmin=3, xmax=6, ymin=3, ymax=5),
          aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),
          fill="green", alpha=.2)





          share|improve this answer























          • Thanks @user2559998. I did toy around with that. Still, I don't see how that is any different to hard coding the values :/

            – nzcoops
            Jul 9 '13 at 0:10











          • Creating a new data.frame (even using the data, not hard-coded) seems to be better. Note that "color" can't be used when alpha is used.

            – igorjrr
            May 3 '18 at 19:42


















          0














          For those attempting to specify fill and alpha values in geom_rect while faceting, I found I had to specify the a row in the dataframe that accorded with each facet to make the rectangle appear in that facet. For a three faceted point plot and two rectangles to extend across the 3 facets:



          plotpnts = ggplot(SHDates, aes(x=Order, y=NewMean))
          Fig2 = plotpnts +

          # Rectangles for time periods
          geom_rect(data=SHDates[1,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
          geom_rect(data=SHDates[11,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
          geom_rect(data=SHDates[22,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
          geom_rect(data=SHDates[1,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
          geom_rect(data=SHDates[11,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
          geom_rect(data=SHDates[22,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +





          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%2f17521438%2fgeom-rect-and-alpha-does-this-work-with-hard-coded-values%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            93














            Thanks for clarifying your question. This was puzzling to me, so I went to google, and ended up learning something new (after working around some vagaries in their examples). Apparently what you are doing is drawing many rectangles on top of each other, effectively nullifying the semi-transparency you want. So, the only ways to overcome this are to hard-code the rectangle coordinates in a separate df, or...



            ggplot() + 
            geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), alpha=0.2, fill="red")


            ... just don't assign your data.frame globally to the plot. Instead, only use it in the layer(s) you want (in this example, geom_density), and leave the other layers df-free! Or, even better yet, Use annotate to modify your plot out from under the default df:



            ggplot(mtcars) + 
            geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            annotate("rect", xmin=100, xmax=200, ymin=0, ymax=Inf, alpha=0.2, fill="red")


            The latter method enables you to use a single data.frame for the entire plot, so you don't have to specify the same df for each layer.



            Both methods return identical plots:



            enter image description here






            share|improve this answer


















            • 2





              Just an add on to this. If you are using this in conjunction with scale_y_continuous (say limits=c(0.005,0.015), so specify the break points exactly, you can't have (ymin=0, ymax=Inf), your min/max values have to fall within the scale limits.

              – nzcoops
              Sep 6 '13 at 5:10











            • +1 This explains all the alpha specification problems I've ever had with ggplot!

              – geotheory
              Dec 21 '13 at 15:26







            • 2





              This is great! But I still don't understand why geom_rect(..., alpha=.1) doesn't work but annotate("rect", ... alpha=.1) does.

              – Stuart
              Jul 22 '14 at 22:15







            • 1





              @Stuart, the geom_rect() call will not produce the desired alpha value because, as @sc_evans explained, it creates one layer for each row of the data frame. In contrast annotate() is a special layer that is not mapped to the data frame so only one rectangle will be drawn (see the help file for annotate())

              – Stefan Avey
              Dec 7 '16 at 16:37











            • The alpha=0.2 doesn't work for me...no matter what the alpha is (between 0,1) there is no transparency. Why?

              – Hercules Apergis
              May 14 '18 at 15:56















            93














            Thanks for clarifying your question. This was puzzling to me, so I went to google, and ended up learning something new (after working around some vagaries in their examples). Apparently what you are doing is drawing many rectangles on top of each other, effectively nullifying the semi-transparency you want. So, the only ways to overcome this are to hard-code the rectangle coordinates in a separate df, or...



            ggplot() + 
            geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), alpha=0.2, fill="red")


            ... just don't assign your data.frame globally to the plot. Instead, only use it in the layer(s) you want (in this example, geom_density), and leave the other layers df-free! Or, even better yet, Use annotate to modify your plot out from under the default df:



            ggplot(mtcars) + 
            geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            annotate("rect", xmin=100, xmax=200, ymin=0, ymax=Inf, alpha=0.2, fill="red")


            The latter method enables you to use a single data.frame for the entire plot, so you don't have to specify the same df for each layer.



            Both methods return identical plots:



            enter image description here






            share|improve this answer


















            • 2





              Just an add on to this. If you are using this in conjunction with scale_y_continuous (say limits=c(0.005,0.015), so specify the break points exactly, you can't have (ymin=0, ymax=Inf), your min/max values have to fall within the scale limits.

              – nzcoops
              Sep 6 '13 at 5:10











            • +1 This explains all the alpha specification problems I've ever had with ggplot!

              – geotheory
              Dec 21 '13 at 15:26







            • 2





              This is great! But I still don't understand why geom_rect(..., alpha=.1) doesn't work but annotate("rect", ... alpha=.1) does.

              – Stuart
              Jul 22 '14 at 22:15







            • 1





              @Stuart, the geom_rect() call will not produce the desired alpha value because, as @sc_evans explained, it creates one layer for each row of the data frame. In contrast annotate() is a special layer that is not mapped to the data frame so only one rectangle will be drawn (see the help file for annotate())

              – Stefan Avey
              Dec 7 '16 at 16:37











            • The alpha=0.2 doesn't work for me...no matter what the alpha is (between 0,1) there is no transparency. Why?

              – Hercules Apergis
              May 14 '18 at 15:56













            93












            93








            93







            Thanks for clarifying your question. This was puzzling to me, so I went to google, and ended up learning something new (after working around some vagaries in their examples). Apparently what you are doing is drawing many rectangles on top of each other, effectively nullifying the semi-transparency you want. So, the only ways to overcome this are to hard-code the rectangle coordinates in a separate df, or...



            ggplot() + 
            geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), alpha=0.2, fill="red")


            ... just don't assign your data.frame globally to the plot. Instead, only use it in the layer(s) you want (in this example, geom_density), and leave the other layers df-free! Or, even better yet, Use annotate to modify your plot out from under the default df:



            ggplot(mtcars) + 
            geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            annotate("rect", xmin=100, xmax=200, ymin=0, ymax=Inf, alpha=0.2, fill="red")


            The latter method enables you to use a single data.frame for the entire plot, so you don't have to specify the same df for each layer.



            Both methods return identical plots:



            enter image description here






            share|improve this answer













            Thanks for clarifying your question. This was puzzling to me, so I went to google, and ended up learning something new (after working around some vagaries in their examples). Apparently what you are doing is drawing many rectangles on top of each other, effectively nullifying the semi-transparency you want. So, the only ways to overcome this are to hard-code the rectangle coordinates in a separate df, or...



            ggplot() + 
            geom_density(data=mtcars, aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            geom_rect(aes(xmin=100, xmax=200, ymin=0,ymax=Inf), alpha=0.2, fill="red")


            ... just don't assign your data.frame globally to the plot. Instead, only use it in the layer(s) you want (in this example, geom_density), and leave the other layers df-free! Or, even better yet, Use annotate to modify your plot out from under the default df:



            ggplot(mtcars) + 
            geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            annotate("rect", xmin=100, xmax=200, ymin=0, ymax=Inf, alpha=0.2, fill="red")


            The latter method enables you to use a single data.frame for the entire plot, so you don't have to specify the same df for each layer.



            Both methods return identical plots:



            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 11 '13 at 3:51









            sc_evanssc_evans

            1,75711 silver badges13 bronze badges




            1,75711 silver badges13 bronze badges







            • 2





              Just an add on to this. If you are using this in conjunction with scale_y_continuous (say limits=c(0.005,0.015), so specify the break points exactly, you can't have (ymin=0, ymax=Inf), your min/max values have to fall within the scale limits.

              – nzcoops
              Sep 6 '13 at 5:10











            • +1 This explains all the alpha specification problems I've ever had with ggplot!

              – geotheory
              Dec 21 '13 at 15:26







            • 2





              This is great! But I still don't understand why geom_rect(..., alpha=.1) doesn't work but annotate("rect", ... alpha=.1) does.

              – Stuart
              Jul 22 '14 at 22:15







            • 1





              @Stuart, the geom_rect() call will not produce the desired alpha value because, as @sc_evans explained, it creates one layer for each row of the data frame. In contrast annotate() is a special layer that is not mapped to the data frame so only one rectangle will be drawn (see the help file for annotate())

              – Stefan Avey
              Dec 7 '16 at 16:37











            • The alpha=0.2 doesn't work for me...no matter what the alpha is (between 0,1) there is no transparency. Why?

              – Hercules Apergis
              May 14 '18 at 15:56












            • 2





              Just an add on to this. If you are using this in conjunction with scale_y_continuous (say limits=c(0.005,0.015), so specify the break points exactly, you can't have (ymin=0, ymax=Inf), your min/max values have to fall within the scale limits.

              – nzcoops
              Sep 6 '13 at 5:10











            • +1 This explains all the alpha specification problems I've ever had with ggplot!

              – geotheory
              Dec 21 '13 at 15:26







            • 2





              This is great! But I still don't understand why geom_rect(..., alpha=.1) doesn't work but annotate("rect", ... alpha=.1) does.

              – Stuart
              Jul 22 '14 at 22:15







            • 1





              @Stuart, the geom_rect() call will not produce the desired alpha value because, as @sc_evans explained, it creates one layer for each row of the data frame. In contrast annotate() is a special layer that is not mapped to the data frame so only one rectangle will be drawn (see the help file for annotate())

              – Stefan Avey
              Dec 7 '16 at 16:37











            • The alpha=0.2 doesn't work for me...no matter what the alpha is (between 0,1) there is no transparency. Why?

              – Hercules Apergis
              May 14 '18 at 15:56







            2




            2





            Just an add on to this. If you are using this in conjunction with scale_y_continuous (say limits=c(0.005,0.015), so specify the break points exactly, you can't have (ymin=0, ymax=Inf), your min/max values have to fall within the scale limits.

            – nzcoops
            Sep 6 '13 at 5:10





            Just an add on to this. If you are using this in conjunction with scale_y_continuous (say limits=c(0.005,0.015), so specify the break points exactly, you can't have (ymin=0, ymax=Inf), your min/max values have to fall within the scale limits.

            – nzcoops
            Sep 6 '13 at 5:10













            +1 This explains all the alpha specification problems I've ever had with ggplot!

            – geotheory
            Dec 21 '13 at 15:26






            +1 This explains all the alpha specification problems I've ever had with ggplot!

            – geotheory
            Dec 21 '13 at 15:26





            2




            2





            This is great! But I still don't understand why geom_rect(..., alpha=.1) doesn't work but annotate("rect", ... alpha=.1) does.

            – Stuart
            Jul 22 '14 at 22:15






            This is great! But I still don't understand why geom_rect(..., alpha=.1) doesn't work but annotate("rect", ... alpha=.1) does.

            – Stuart
            Jul 22 '14 at 22:15





            1




            1





            @Stuart, the geom_rect() call will not produce the desired alpha value because, as @sc_evans explained, it creates one layer for each row of the data frame. In contrast annotate() is a special layer that is not mapped to the data frame so only one rectangle will be drawn (see the help file for annotate())

            – Stefan Avey
            Dec 7 '16 at 16:37





            @Stuart, the geom_rect() call will not produce the desired alpha value because, as @sc_evans explained, it creates one layer for each row of the data frame. In contrast annotate() is a special layer that is not mapped to the data frame so only one rectangle will be drawn (see the help file for annotate())

            – Stefan Avey
            Dec 7 '16 at 16:37













            The alpha=0.2 doesn't work for me...no matter what the alpha is (between 0,1) there is no transparency. Why?

            – Hercules Apergis
            May 14 '18 at 15:56





            The alpha=0.2 doesn't work for me...no matter what the alpha is (between 0,1) there is no transparency. Why?

            – Hercules Apergis
            May 14 '18 at 15:56













            15














            Another workaround is to give geom_rect a single row data object to ensure only one rectangle is drawn:



            ggplot(mtcars) +
            geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            geom_rect(data=mtcars[1,], aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)


            enter image description here






            share|improve this answer























            • This is the solution you are looking for if you are faceting. Thanks!

              – Nova
              Apr 7 '17 at 13:59















            15














            Another workaround is to give geom_rect a single row data object to ensure only one rectangle is drawn:



            ggplot(mtcars) +
            geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            geom_rect(data=mtcars[1,], aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)


            enter image description here






            share|improve this answer























            • This is the solution you are looking for if you are faceting. Thanks!

              – Nova
              Apr 7 '17 at 13:59













            15












            15








            15







            Another workaround is to give geom_rect a single row data object to ensure only one rectangle is drawn:



            ggplot(mtcars) +
            geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            geom_rect(data=mtcars[1,], aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)


            enter image description here






            share|improve this answer













            Another workaround is to give geom_rect a single row data object to ensure only one rectangle is drawn:



            ggplot(mtcars) +
            geom_density(aes(x=disp, group=cyl, fill=cyl), alpha=0.6, adjust=0.75) +
            geom_rect(data=mtcars[1,], aes(xmin=100, xmax=200, ymin=0,ymax=Inf), fill="red", alpha=0.2)


            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 21 '13 at 15:34









            geotheorygeotheory

            9,63016 gold badges71 silver badges136 bronze badges




            9,63016 gold badges71 silver badges136 bronze badges












            • This is the solution you are looking for if you are faceting. Thanks!

              – Nova
              Apr 7 '17 at 13:59

















            • This is the solution you are looking for if you are faceting. Thanks!

              – Nova
              Apr 7 '17 at 13:59
















            This is the solution you are looking for if you are faceting. Thanks!

            – Nova
            Apr 7 '17 at 13:59





            This is the solution you are looking for if you are faceting. Thanks!

            – Nova
            Apr 7 '17 at 13:59











            2














            ggplot(df, aes(xmin = x, xmax = x + 1, ymin = y, ymax = y + 2)) +
            geom_rect(alpha=.2) +
            geom_rect(data=data.frame(xmin=3, xmax=6, ymin=3, ymax=5),
            aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),
            fill="green", alpha=.2)





            share|improve this answer























            • Thanks @user2559998. I did toy around with that. Still, I don't see how that is any different to hard coding the values :/

              – nzcoops
              Jul 9 '13 at 0:10











            • Creating a new data.frame (even using the data, not hard-coded) seems to be better. Note that "color" can't be used when alpha is used.

              – igorjrr
              May 3 '18 at 19:42















            2














            ggplot(df, aes(xmin = x, xmax = x + 1, ymin = y, ymax = y + 2)) +
            geom_rect(alpha=.2) +
            geom_rect(data=data.frame(xmin=3, xmax=6, ymin=3, ymax=5),
            aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),
            fill="green", alpha=.2)





            share|improve this answer























            • Thanks @user2559998. I did toy around with that. Still, I don't see how that is any different to hard coding the values :/

              – nzcoops
              Jul 9 '13 at 0:10











            • Creating a new data.frame (even using the data, not hard-coded) seems to be better. Note that "color" can't be used when alpha is used.

              – igorjrr
              May 3 '18 at 19:42













            2












            2








            2







            ggplot(df, aes(xmin = x, xmax = x + 1, ymin = y, ymax = y + 2)) +
            geom_rect(alpha=.2) +
            geom_rect(data=data.frame(xmin=3, xmax=6, ymin=3, ymax=5),
            aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),
            fill="green", alpha=.2)





            share|improve this answer













            ggplot(df, aes(xmin = x, xmax = x + 1, ymin = y, ymax = y + 2)) +
            geom_rect(alpha=.2) +
            geom_rect(data=data.frame(xmin=3, xmax=6, ymin=3, ymax=5),
            aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax),
            fill="green", alpha=.2)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 8 '13 at 8:35









            user2559998user2559998

            211 bronze badge




            211 bronze badge












            • Thanks @user2559998. I did toy around with that. Still, I don't see how that is any different to hard coding the values :/

              – nzcoops
              Jul 9 '13 at 0:10











            • Creating a new data.frame (even using the data, not hard-coded) seems to be better. Note that "color" can't be used when alpha is used.

              – igorjrr
              May 3 '18 at 19:42

















            • Thanks @user2559998. I did toy around with that. Still, I don't see how that is any different to hard coding the values :/

              – nzcoops
              Jul 9 '13 at 0:10











            • Creating a new data.frame (even using the data, not hard-coded) seems to be better. Note that "color" can't be used when alpha is used.

              – igorjrr
              May 3 '18 at 19:42
















            Thanks @user2559998. I did toy around with that. Still, I don't see how that is any different to hard coding the values :/

            – nzcoops
            Jul 9 '13 at 0:10





            Thanks @user2559998. I did toy around with that. Still, I don't see how that is any different to hard coding the values :/

            – nzcoops
            Jul 9 '13 at 0:10













            Creating a new data.frame (even using the data, not hard-coded) seems to be better. Note that "color" can't be used when alpha is used.

            – igorjrr
            May 3 '18 at 19:42





            Creating a new data.frame (even using the data, not hard-coded) seems to be better. Note that "color" can't be used when alpha is used.

            – igorjrr
            May 3 '18 at 19:42











            0














            For those attempting to specify fill and alpha values in geom_rect while faceting, I found I had to specify the a row in the dataframe that accorded with each facet to make the rectangle appear in that facet. For a three faceted point plot and two rectangles to extend across the 3 facets:



            plotpnts = ggplot(SHDates, aes(x=Order, y=NewMean))
            Fig2 = plotpnts +

            # Rectangles for time periods
            geom_rect(data=SHDates[1,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
            geom_rect(data=SHDates[11,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
            geom_rect(data=SHDates[22,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
            geom_rect(data=SHDates[1,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
            geom_rect(data=SHDates[11,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
            geom_rect(data=SHDates[22,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +





            share|improve this answer



























              0














              For those attempting to specify fill and alpha values in geom_rect while faceting, I found I had to specify the a row in the dataframe that accorded with each facet to make the rectangle appear in that facet. For a three faceted point plot and two rectangles to extend across the 3 facets:



              plotpnts = ggplot(SHDates, aes(x=Order, y=NewMean))
              Fig2 = plotpnts +

              # Rectangles for time periods
              geom_rect(data=SHDates[1,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
              geom_rect(data=SHDates[11,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
              geom_rect(data=SHDates[22,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
              geom_rect(data=SHDates[1,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
              geom_rect(data=SHDates[11,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
              geom_rect(data=SHDates[22,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +





              share|improve this answer

























                0












                0








                0







                For those attempting to specify fill and alpha values in geom_rect while faceting, I found I had to specify the a row in the dataframe that accorded with each facet to make the rectangle appear in that facet. For a three faceted point plot and two rectangles to extend across the 3 facets:



                plotpnts = ggplot(SHDates, aes(x=Order, y=NewMean))
                Fig2 = plotpnts +

                # Rectangles for time periods
                geom_rect(data=SHDates[1,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
                geom_rect(data=SHDates[11,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
                geom_rect(data=SHDates[22,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
                geom_rect(data=SHDates[1,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
                geom_rect(data=SHDates[11,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
                geom_rect(data=SHDates[22,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +





                share|improve this answer













                For those attempting to specify fill and alpha values in geom_rect while faceting, I found I had to specify the a row in the dataframe that accorded with each facet to make the rectangle appear in that facet. For a three faceted point plot and two rectangles to extend across the 3 facets:



                plotpnts = ggplot(SHDates, aes(x=Order, y=NewMean))
                Fig2 = plotpnts +

                # Rectangles for time periods
                geom_rect(data=SHDates[1,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
                geom_rect(data=SHDates[11,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
                geom_rect(data=SHDates[22,], xmin=0,ymin=500,xmax=39,ymax=1100, fill="red", alpha=0.4) +
                geom_rect(data=SHDates[1,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
                geom_rect(data=SHDates[11,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +
                geom_rect(data=SHDates[22,], xmin=0,ymin=1000,xmax=39,ymax=1400, fill="orange", alpha=0.4) +






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 11 at 20:25









                LA WandsniderLA Wandsnider

                31 bronze badge




                31 bronze badge



























                    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%2f17521438%2fgeom-rect-and-alpha-does-this-work-with-hard-coded-values%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

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript