-1

Currently having a difficult time with this error. I am relatively new to R and still working out the kinks but here what I am trying to do.

I have a file that has this exact format:

2j2j 902223
6i2i 2311
0i2b 23345
1k2c 4324

I want to convert this into a pie chart in R, but I am having a difficult time putting everything where it needs to be. I want the first column to be the labels, and the second column to be the data that is that being visualized. The methodology I am using putting each column into its own variable. I am extremely confused what would be effective, a scan() function or a read.table function?

Jaap
  • 81,064
  • 34
  • 182
  • 193
user3802685
  • 65
  • 1
  • 7
  • 1
    What exactly are you having difficulties with? Did you try `read.table(path_to_your_file)`? Can you show some code of what you've tried? Furthermore, are you sure you want a piechart? They are notoriously hard to interpret. – Heroka Apr 23 '16 at 20:14
  • Initially having a function read a data file and spliting each column into it own variable. I've got the pie function figured out, now I'm stuck on this portion. – user3802685 Apr 23 '16 at 20:23
  • Please don't make a pie chart with R. Leave that for the Excel users. The `waffle` package presents a much better alternative. So do bar charts. – hrbrmstr Apr 24 '16 at 03:11

2 Answers2

0

Assuming that your data is stored in a file named "temp.dat" you could use

mydat <- read.table("temp.dat")
#> mydat
#    V1     V2
#1 2j2j 902223
#2 6i2i   2311
#3 0i2b  23345
#4 1k2c   4324
pie(mydat$V2,labels=mydat$V1)

for a simple pie chart. The result is not very pretty, for the reasons pointed out by @Heroka in the comments.

enter image description here

Of course, one could try to embellish it by changing the colors or using a 3D representation, but this will probably not improve significantly the understanding of the data.

Community
  • 1
  • 1
RHertel
  • 23,412
  • 5
  • 38
  • 64
0

Before I build an example, please remember that pie charts are not recommended in the R documentation. The authors recommend bar or dot plots, mainly because people are able to judge length better than volume.

Say that, first, you should have, for example a dataframe with observations. In this case, I create a dataframe df with 400 vehicles:

df <- data.frame(400)
df <- rep( c("car", "truck", "other", "bike", "suv"), c(60, 120, 20, 50, 150))

Since pie charts are especially useful for proportions, let's have a look on the proportions of our vehicles, than we will report on the graph in this case:

paste(prop.table(table(df))*100, "%", sep = "")
[1] "15%"   "5%"    "30%"   "12.5%" "37.5%"

Then you can draw your pie chart,

pie(table(df), labels = paste(round(prop.table(table(e3))*100), "%", sep = ""), 
col = heat.colors(5), main = "Vehicles proportions - n: 400")

enter image description here

Finally, you can add it a legend:

legend("topright", legend = c("car", "truck", "other", "bike", "suv"), 
fill = heat.colors(5), title = "Categories", cex = 0.5)
Worice
  • 3,847
  • 3
  • 28
  • 49