Imminence
Imminence

Reputation: 49

Pie chart of frequency counts

I've imported a 1-column excel file using gdata, the data is as follows

3 4 3 3 1 4 1 3 2 3 1 1 4 2 3 3 2 6 1 1 3 3 2 2 2 2 1 3 2 1 6 1 3 2 2 1 2 2 4 2

I'm using the pie(md[, 1]) command to create a pie chart for the data, however, I'm getting the following chart when I do this:

Pie chart.

It's taking the data as 1-40 and then creating the pie width to the data sample rather than having 5 segments (1,2,3,4,6) with width created by the amount of times the result appears, i.e. the frequency counts of unique elements in the vector. How can I achieve that?

Upvotes: 1

Views: 4258

Answers (1)

talat
talat

Reputation: 70276

Use the ?table function to compute frequencies before applying pie:

table(x)
#x
# 1  2  3  4  6 
#10 13 11  4  2 

Then, to produce the pie chart of frequencies:

pie(table(x))

produces:

pie

x <- scan(text = "3 4 3 3 1 4 1 3 2 3 1 1 4 2 3 3 2 6 1 1 3 3 2 2 2 2 1 3 2 1 6 1 3 2 2 1 2 2 4 2")

Upvotes: 8

Related Questions