Search code examples
rpie-chartpercentage

No correct association of name and percentage to create a pie chart


I want to create a simple pie chart, with data from one column. The variations there are name_yes, name_no, what the pie chart should show with name and percentage at the end.

So this is the code I hava, running fine, with export but the labeling is wrong and shows 2x name_yes. Maybe there is the problem, for the wrong output of the pie chart.

The pie chart displays the correct percentage but the wrong label with 2x yes, and not like it should 1x no and 1x yes Here ist the pie chart:

png(file="hydrologicalp.png", width=600, height=400)
hydrological <- xname$hydrological
hydrologicalp <- paste(hydrological, round((table(hydrological)/sum(table(hydrological)))*100), "%", sep = " ")
pie(table(hydrological), label = hydrologicalp, main = "hydrological")
dev.off() 

Having a look at the created "hydrologicalp" table shows, that the percentage values arte just given alternately, without correct association to the name_yes ore name_no. The hydrologicalp Table:

hydrologicalp
  [1] "hydrological_yes 28 %" "hydrological_yes 72 %"
  [3] "hydrological_yes 28 %" "hydrological_yes 72 %"
  [5] "hydrological_yes 28 %" "hydrological_yes 72 %"
  [7] "hydrological_no 28 %"  "hydrological_yes 72 %"
  [9] "hydrological_yes 28 %" "hydrological_yes 72 %"
....... and so on

How can I fix this? Thanks in advance.


Solution

  • you needed to have made hydrological unique before you paste it together with the results of tabling it. p.s. I was a bit annoyed about having to make representative data to get this up and running; in the future when seeking assistance please take steps towards this critical element.

    hydrological <- c("a","a","b")
    (hydrologicalp <- paste(unique(hydrological), round((table(hydrological)/sum(table(hydrological)))*100), "%", sep = " "))
    pie(table(hydrological), label = hydrologicalp, main = "hydrological")