What does this error mean [R Programming]?

Viewed 24

My data frame

> dput(wt)
structure(list(Teams = structure(1:21, .Label = c("Arsenal", 
"Aston Villa", "Bournemouth", "Brighton", "Burnley", "Chelsea", 
"Crystal Palace", "Draw", "Everton", "Leicester", "Liverpool", 
"Man City", "Man United", "Newcastle", "Norwich", "Sheffield United", 
"Southampton", "Tottenham", "Watford", "West Ham", "Wolves"), class = "factor"), 
`Total Number Of Wins` = c(6L, 6L, 5L, 6L, 7L, 11L, 7L, 50L, 
 7L, 14L, 18L, 14L, 8L,7L, 3L, 7L, 7L, 8L, 4L, 6L, 7L)), class = "data.frame",                                              
 row.names = c(NA, -21L))

My pie ggplot code

pie = ggplot(wt, aes(x="", y="Total Number of Wins", fill="teams")) + geom_bar(stat='identity',width = 1)

pie = pie + coord_polar("y", start=0) + geom_text(aes(label = paste0(round(value*1), "%")), position = position_stack(vjust = 0.5))

The error I get is:

Error in paste0(round(value * 1), "%") : object 'value' not found.

What should I replace the 'value' with? Can someone please enlighten me.

1 Answers

I'm not sure what you are trying to achieve here, but it looks like its because "value" here, which you used in your code, doesn't have an assigned variable. I basically loaded the data you had and saved one variable to make it work:

value <- wt$`Total Number Of Wins`

Then printed the plot using the same plot script you used:

pie = ggplot(wt, 
             aes(x="",
                 y="Total Number of Wins", 
                 fill="teams")) + 
  geom_bar(stat='identity',
           width = 1)

pie = pie + 
  coord_polar("y", 
              start=0) + 
  geom_text(aes(label = paste0(round(value*1), 
                               "%")), 
            position = position_stack(vjust = 0.5))

Which gave me this:

enter image description here

I think what you meant to do was color code this, and you just need to make one more minor edit. R is case sensitive, so you need to capitalize the "Teams" variable in your original pie code:

pie = ggplot(wt, 
             aes(x="",
                 y="Total Number of Wins", 
                 fill=Teams)) + # capitalized variable w/o quotes
  geom_bar(stat='identity',
           width = 1)

Which if you now run both plot codes, itll give you this:

enter image description here

Related