0

I'm super new to R and wanted to make a pie chart of the results found from a 6 faced die. From what I understand, this is supposed to throw the 6-faced die 500 times, and then map out the results in a pie chart. It seems fairly simple, but the pie chart splits into 500 ways. What am I doing wrong?

die <- 1:6
result <- sample(die, size = 500, replace = TRUE)
pie(result, labels = die)   
  • 6
    ?table; do your plot on the output of that for a display of counts (or scale that by the total count for proportions). (A pie chart is a poor choice for this by the way; I'd typically use a variant of plot(table(...)) to show such results.) – Glen_b Mar 14 '23 at 22:20
  • 2
    This is essentially off-topic, but I love that the first sentence in *Note* in the documentation for pie() is "Pie charts are a very bad way of displaying information." – Sal Mangiafico Mar 14 '23 at 22:38
  • 2
    I have been using R every day for over a decade, have posted a thousand R solutions here on CV, and have never once invoked pie. (But see https://stats.stackexchange.com/a/451376/919 ;-).) Following @Glen_b's recommendation, I looked at pie(table(sample.int(6, 500, replace = TRUE))). Rather pretty -- nice pastel colors rarely seen in any other R functions -- and quite useless! For the record, this function defaults to a cycle of six colors, c("white", "lightblue", "mistyrose", "lightcyan", "lavender", "cornsilk"). – whuber Mar 14 '23 at 22:56
  • The example is perhaps a little too simple, since die is both the population data and the labels for the chart. An example with an unfair die: die <- c(1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,5,5,5,6,6); result <- sample(die, size = 500, replace = TRUE); Table = table(result); Table; pie(Table, labels=labels(Table)[[1]]); round(prop.table(table(result)), 2) – Sal Mangiafico Mar 14 '23 at 23:00
  • 1
    @SalMangiafico not super important but die<-rep(1:6,c(5, 4, 4, 3, 3, 2)) would save some typing, alternatively just directly go sample(6,500,replace=TRUE,p=c(5, 4, 4, 3, 3, 2)) – Glen_b Mar 14 '23 at 23:04

1 Answers1

2

You want something inheriting from table class:

pie(table(result)) 
Alex J
  • 2,151