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 8257837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:20:10+00:00 2026-06-08T02:20:10+00:00

I am following this example to create polygons to plot using ggplot I am

  • 0

I am following this example to create polygons to plot using ggplot I am able to follow that example and create separate convex hulls if my data is subsetted; however, when I try to apply ddply as I have a grouping variable I am unable to. Here’s the code from the example with a added grouping variable :

library(grDevices) # load grDevices package
df <- data.frame(X = c(-62,  -40,   9,  13,  26,  27,  27),
             Y = c( 7, -14,  10,   9,  -8, -16,  12), id = c(1, 1, 1, 2, 2, 3, 3))
con.hull.pos <- ddply(df, .(id), summarize, hullpos = chull(X, Y)) # get convex hull positions by each ID

Now, to get a complete polygon for each ID, we need to get all the rows by each ID as given in con.hull.pos but we also need to add the first row of each group.

df[ddply(con.hull.pos, .(id), function(x) x[1, ])$hullpos, ] # first row of position
df[con.hull.pos$hullpos  ,]  ## all rows of position
rbind(df[con.hull.pos$hullpos  ,] , df[ddply(con.hull.pos, .(id), function(x) x[1, ])$hullpos, ])

and here my code fails as the first row using ddply is not the same as first row from the convex hull for an ID. Thus, the polygon is not complete. Can anyone please help me in applying the given example by grouping by a variable.

When subsetting manually, this code works as it creates three separate polygons covering three id areas

id1_df <- subset(df, id==1)
id1_con.hull.pos <- chull(id1_df$X, id1_df$Y)   

id2_df <- subset(df, id==2)
id2_con.hull.pos <- chull(id2_df$X, id2_df$Y)

id3_df <- subset(df, id==3)
id3_con.hull.pos <- chull(id3_df$X, id3_df$Y)

id1_con.hull <- rbind(id1_df[id1_con.hull.pos,], id1_df [id1_con.hull.pos[1],])
id2_con.hull <- rbind(id2_df [id2_con.hull.pos ,], id2_df  [id2_con.hull.pos [1],])
id3_con.hull <- rbind(id3_df [id3_con.hull.pos,], id3_df [id3_con.hull.pos[1],])             

poly_borders <- rbind(id1_con.hull, id2_con.hull, id3_con.hull)     

plot(Y ~ X, data = df) # plot data
lines(poly_borders) # add lines for convex hull
  • 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-06-08T02:20:11+00:00Added an answer on June 8, 2026 at 2:20 am

    Two hints:

    1. Use the chull function in base R to compute the convex hull.
    2. Use dlply to store the resulting chull in a list, rather than a data frame

    Then your code turns to:

    x <- dlply(df, .(id), function(piece)piece[chull(piece$X, piece$Y), -3])
    
    plot(Y~X, df)
    lapply(x, polygon)
    

    Which produces this plot:

    enter image description here


    If you want to plot this in ggplot it’s even easier, but use ddply:

    x <- ddply(df, .(id), function(piece)piece[chull(piece$X, piece$Y), ])
    ggplot(x, aes(X, Y, group=id)) + geom_polygon(fill="cyan", colour="blue") + geom_line()
    

    enter image description here

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

Sidebar

Related Questions

Following this nice example I found, I was trying to create a function that
I am following this example (First comment), How to create a custom ListView with
I'm following this example for creating a simple Web application using JAX-RS, MongoDB and
I am following this example to get JSONP data from remote server. jQuery append
Check this example before reading the question - http://www.sqlfiddle.com/#!2/fcf3e/8 The following data comes from
I'm trying to create a menu in Qt following this example http://doc.qt.nokia.com/latest/mainwindows-menus.html but I
I'm following this basic example how to create a system driver: http://sriramk.com/blog/2007/09/world-windows-driver-from-scratch.html When I
I have used this example to create a database that I copy over on
I'm new to cakephp and following this tutorial. http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html I have created the blog
I'm following this example: http://www.codinghorror.com/blog/2005/12/getting-started-with-indexing-service.html However, the conversion to dataset shows empty columns for

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.