在 R 中使用两个数据集创建散点图
Creating a scatter plot using two data sets in R
这里是初学者。我希望使用我使用 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 个 obs。
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()
错误:Aesthetics must be either length 1 or the same as the data (54): x, y
在 ggplot 的 aes()
函数中,您输入了为数据参数提供的数据框中的裸列名称。所以在你的例子中它将是:
ggplot(total_data ,
aes(x = percent_instability,
y = perc_exercise)) +
geom_point()
尽管我不确定您的示例中的 total_ex
是什么。
此外,使用 total_ex$menthlth_perc_bystate
意味着您正在数据框 total_ex
中查找名为 menthlth_perc_bystate
的列。该列不存在,它是另一个数据框的名称。合并两个数据框后,生成的数据框中的列将为 state
、percent_instability
和 perc_exercise
.
这里是初学者。我希望使用我使用 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 个 obs。
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()
错误:Aesthetics must be either length 1 or the same as the data (54): x, y
在 ggplot 的 aes()
函数中,您输入了为数据参数提供的数据框中的裸列名称。所以在你的例子中它将是:
ggplot(total_data ,
aes(x = percent_instability,
y = perc_exercise)) +
geom_point()
尽管我不确定您的示例中的 total_ex
是什么。
此外,使用 total_ex$menthlth_perc_bystate
意味着您正在数据框 total_ex
中查找名为 menthlth_perc_bystate
的列。该列不存在,它是另一个数据框的名称。合并两个数据框后,生成的数据框中的列将为 state
、percent_instability
和 perc_exercise
.