0

I successfully created small multiple radar charts using the 'msleep' ggplot2 package, as below.

msleep radar charts

But when I load my own dataset and try to plot using my own data, I keep getting the following error:

Error in select(country, democracy, freedom, corruption) :
object 'country' not found

Here is the original code:

# prepare data
data(msleep, package = "ggplot2")
library(ggradar)
library(scales)
library(dplyr)

plotdata <- msleep %>%
  filter(name %in% c("Cow", "Dog", "Pig")) %>%
  select(name, sleep_total, sleep_rem, 
         sleep_cycle, brainwt, bodywt) %>%
  rename(group = name) %>%
  mutate_at(vars(-group),
            funs(rescale))
plotdata

# generate radar chart
ggradar(plotdata, 
        grid.label.size = 4,
        axis.label.size = 4, 
        group.point.size = 5,
        group.line.width = 1.5,
        legend.text.size= 10) +
  labs(title = "Mammals, size, and sleep")

And here is my code that's throwing me the error:

# prepare data
library(ggradar)
library(scales)
library(dplyr)

radarData <- read.csv(file="Index_Data - radar-chart-wide (2).csv")

radarData
  select(country, democracy, freedom, corruption) %>%
  rename(country = groups) %>%
  mutate_at(vars(-country),
            funs(rescale))
  radarData

And here's a screenshot of what my data looks like:

personal data

OTStats
  • 1,820
  • 1
  • 13
  • 22
bluedaniel
  • 2,079
  • 5
  • 31
  • 49
  • 1
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Nov 25 '19 at 16:15
  • 1
    first of all, there seems to be a `%>%` missing, just before `select(...)` – mrhd Nov 25 '19 at 16:36

3 Answers3

0

I think the problem here is with the rename call. It follows the syntax

rename(new_name = old_name)

but you used it the other way around.

Anyway, since you refer to column country in the mutate_at statement after that, you probably want to keep the name as it is. So you can omit the call to rename:

df <- radarData %>% 
  select(country, democracy, freedom, corruption) %>%
  mutate_at(vars(-country),
            funs(rescale))
df
mrhd
  • 1,036
  • 7
  • 16
0

Thanks for your help. I got it working and shortened the data for easiness...

Here's the code:

install.packages("devtools")
devtools::install_github("ricardo-bion/ggradar")

# prepare data
library(ggradar)
library(scales)
library(dplyr)
library(ggplot2)

radarDataShort <- read.csv(file="Index_Data - radar-chart-wide2 (1).csv")

dfShort <- radarDataShort %>%
  select(country, democracy, freedom, corruption) %>%
  #mutate_at(vars(),
  mutate_at(vars(-country),
            funs(rescale))
dfShort

# generate radar chart
ggradar(dfShort, 
        grid.label.size = 4,
        axis.label.size = 4, 
        group.point.size = 0,
        group.line.width = 1) 

radar chart

However, when I try to facet_wrap, I get the following error:

Error: At least one layer must contain all faceting variables: country. * Plot is missing country * Layer 1 is missing country * Layer 2 is missing country * Layer 3 is missing country * Layer 4 is missing country * Layer 5 is missing country * Layer 6 is missing country * Layer 7 is missing country * Layer 8 is missing country * Layer 9 is missing country * Layer 10 is missing country * Layer 11 is missing country * Layer 12 is missing country * Layer 13 is missing country

And here's my latest code w/ the facet_wrap

# prepare data
library(ggradar)
library(scales)
library(dplyr)
library(ggplot2)

radarDataShort <- read.csv(file="Index_Data - radar-chart-wide2 (1).csv")

dfShort <- radarDataShort %>%
  select(country, democracy, freedom, corruption) %>%
  #mutate_at(vars(),
  mutate_at(vars(-country),
            funs(rescale))
dfShort

# generate radar chart
ggradar(dfShort, 
        grid.label.size = 4,
        axis.label.size = 4, 
        group.point.size = 0,
        group.line.width = 1) +
facet_wrap(~country)

Thanks again!

bluedaniel
  • 2,079
  • 5
  • 31
  • 49
0

I figured it out. For some reason, the facet_wrap required (~group) and NOT (~country). Not sure why, as group is not included in my data. Perhaps it's a generic function in ggradar?

Thank you anyway!

bluedaniel
  • 2,079
  • 5
  • 31
  • 49