0
votes

Beginner here. I'm hoping to create a scatterplot using two datasets that I created using group by:

menthlth_perc_bystate <- brfss2013 %>% 
  group_by(state) %>%
  summarise(percent_instability = sum(menthlth > 15, na.rm = TRUE) / n()) %>%
  arrange(desc(percent_instability))

exercise_perc_bystate <- brfss2013 %>%
  group_by(state) %>%
  summarise(perc_exercise = sum(exeroft1 > 30, na.rm = TRUE) / n()) %>%
  arrange(desc(perc_exercise))

I want to merge these into one dataset, total_data. Both have 54 obs.

total_data <- merge(menthlth_perc_bystate,exercise_perc_bystate,by="state")

Presumably the scatter plot would take on one axis the state's percent instability (menthlth_perc_bystate) and on another the states percent exercise (exercise_perc_by_state). I tried this using ggplot but got an error:

ggplot(total_data, aes(x = total_data$menthlth_perc_bystate, y = total_data$exercise_perc_bystate)) + geom_point()

The error: Aesthetics must be either length 1 or the same as the data (54): x, y

1

1 Answers

0
votes

In the aes() function of ggplot you put the bare column names from the data frame you provided for the data argument. So in your example it would be:

ggplot(total_data , 
       aes(x = percent_instability,
           y = perc_exercise)) + 
geom_point()

Although I'm not sure what total_ex is in your example. Also, using total_ex$menthlth_perc_bystate implies you are looking for a column named menthlth_perc_bystate in the data frame total_ex. That column does not exist, it is the name of a different data frame. Once you have merged the two data frames, the columns in the resulting data frame will be state, percent_instability, and perc_exercise.