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

  • Home
  • SEARCH
  • 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 9137455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:06:19+00:00 2026-06-17T09:06:19+00:00

I’m making a set of pie charts in R (with a loop) out of

  • 0

I’m making a set of pie charts in R (with a loop) out of subsets from a single data frame. I get nice pie charts but the colors are allocated always in the same order in each pie while I would like to have the colors allocated to the same values, throughout all the pies.

my code is :

for (i in 1:length(voisins)) {                
    y <- subset(zz, Destination==voisins[i])    
    pie(y$pc,labels=y$Names, col=terrain.colors(nrow(y)) , main=c(y$country[1])) 

I would like to have the same color for each y$Names.

I’m not allowed to put an image here, so I put it there : http://cl.ly/image/080t2Q1v1q3V

It would be easier to read if the same region had the same color ! Is there a way to achieve this ?

Solution :

thank you, Arun, for your answer but unfortunately this doesn’t produce the result I want, probably I didn’t explain it clearly enough. I believe ggplot is a better graphical package and I will learn how to use it soon, but for the moment I don’t have time and your answer anyway put me on the right track. I found a solution : actually if you make a bigger “t” (with more rows) in your example and then take subsets of it resulting in smaller t1, t2, t3, …, with not always the same t$id, so let’s say in t1 we would have id = (2,4,9,3) and in t2 id = (4,6,3), and so on. You will see that the colors are allocated always in the same order, which gives in t1 2=red, 4=green, 9=blue, 3=violet, while in t2 4=red, 6=green and 3=blue. So the colors vary from one pie to another and I want to have always 2=red, 4=green, 3=blue, 9=violet. The solution I found is to merge a color column to t according to t$id, then keep it in the subsets t1, t2, t3, … and set col=t1$color , col=t2$color, and so on in the pie command. It was finally easy but I had hoped for an automatic solution and I will see Paul’s one later. Didier

  • 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-17T09:06:20+00:00Added an answer on June 17, 2026 at 9:06 am

    Let me first state that piecharts are a bad idea to begin with as they have major perceptual problems, see e.g. here.

    Then to get back ot the problem, I would use ggplot, and more specifically facetting. First create one piechart:

    pie <- ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) +
     geom_bar(width = 1)
    pie + coord_polar(theta = "y")
    

    enter image description here

    To make a piechart per level of a variable you can use facet_wrap, e.g. per unique level of the variable gear in mtcars:

    pie + coord_polar(theta = "y") + facet_wrap(~ factor(gear))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms

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.