初学者在这里 . 我希望使用我使用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))
我想将这些合并到一个数据集total_data中 . 两人都有54个障碍物 .
total_data <- merge(menthlth_perc_bystate,exercise_perc_bystate,by="state")
据推测,散点图将在一个轴上显示状态的百分比不稳定性(menthlth_perc_bystate),而在另一个轴上显示状态百分比运动(exercise_perc_by_state) . 我用ggplot尝试了这个,但是收到了一个错误:
ggplot(total_data, aes(x = total_data$menthlth_perc_bystate, y = total_data$exercise_perc_bystate)) + geom_point()
错误:美学必须是长度1或与数据(54)相同:x,y
1 回答
在ggplot的
aes()
函数中,您放置了为数据参数提供的数据框中的裸列名称 . 所以在你的例子中它将是:虽然我不确定你的例子中是什么
total_ex
. 此外,使用total_ex$menthlth_perc_bystate
意味着您正在数据框total_ex
中查找名为menthlth_perc_bystate
的列 . 该列不存在,它是不同数据框的名称 . 合并两个数据帧后,结果数据框中的列将为state
,percent_instability
和perc_exercise
.