Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5980603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:49:28+00:00 2026-05-22T21:49:28+00:00

Suppose I draw the following plot composed of two polygons : library(ggplot2) polys <-

  • 0

Suppose I draw the following plot composed of two polygons :

library(ggplot2)

polys <- data.frame( 
  id = rep(factor(c("1","2")),each=4),
  x = c(1,2,2,1,2,3,3,2),
  y = c(1,1,2,2,1,1,2,2),
  value = rep(c(3, 4), each=4)
) 

(p <- ggplot(polys, aes(x=x, y=y)) + geom_polygon(aes(fill=value, group=id)))

Which gives :

enter image description here

Is there a way to determine the color given to each polygon from the p ggplot object ? I’ve played a bit with str(p) but didn’t find anything useful for this.

Thanks in advance for any hint !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:49:29+00:00Added an answer on May 22, 2026 at 9:49 pm

    You can do that by:

    > x <- ggplotGrob(p)
    > r <- getGrob(x, gPath("geom_polygon", "GRID.polygon"), grep=T, global=T) 
    > r
    (polygon[GRID.polygon.766], polygon[GRID.polygon.769]) 
    > r[[1]]$gp$fill
    [1] "#3B4FB8FF" "#3B4FB8FF" "#3B4FB8FF" "#3B4FB8FF"
    > r[[2]]$gp$fill
    [1] "#B71B1AFF" "#B71B1AFF" "#B71B1AFF" "#B71B1AFF"
    
    1. ggplotGrob makes grob tree
    2. getGrob extracts grobs that you want by using gPath
    3. $gp$fill shows the color of the polygon.

    In the future version of ggplot2, probably you will get information more easily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following: class Shape a where draw a :: a ->
Suppose I have the following CSS rule in my page: body { font-family: Calibri,
Suppose the following: >>> s = set([1, 2, 3]) How do I get a
Is there any way to force Flex to draw empty PieChart when all data
The cvLine() function can draw a straight line given two points P1(x1,y1) and P2(x2,y2).
Suppose I draw a cube like this: gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVerticesBuffer); gl.glTranslatef(x, y, z);
Suppose I have an application that will draw 20 little UIImageViews on the screen.
The following is supposed to draw an axis in the middle of the screen.
I am using a custom MKOverlay to draw weather data over a MKMapView. The
Suppose I have following inheritance tree: SDLBullet inherits from Bullet inherits from Entity EnemyBullet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.