如何仅在散点图中选择前五名选项
我只想在散点图中选择前五名。这是代码:
library(dslabs)
data(polls_us_election_2016)
polls <- polls_us_election_2016 %>%
filter(state == "U.S." & enddate >= "2016-10-31" &
(grade %in% c("A+","A","A-","B+") | is.na(grade)))
polls <- polls %>%
mutate(spread = rawpoll_clinton/100 - rawpoll_trump/100)
d_hat <- polls %>%
summarize(d_hat = sum(spread * samplesize) / sum(samplesize)) %>%
pull(d_hat)
p_hat <- (d_hat+1)/2
moe <- 1.96 * 2 * sqrt(p_hat * (1 - p_hat) / sum(polls$samplesize))
polls %>% ggplot(aes(spread, pollster)) +
geom_point(color="black", binwidth = .01)
以下是所需的图:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
管道
nest%&gt;%head(5)%&gt;%unnest
以数据框中出现的顺序为您提供前5组行。然后,ggplot2将以alpha词法方式对它们进行排序:由 href =“ https://reprex.tidyverse.org” rel =“ nofollow noreferrer”> reprex软件包(v2.0.0)
The pipe
nest %>% head(5) %>% unnest
gives you the first 5 groups of rows in the order they occur in the data frame. Then, ggplot2 will sort them in an alpha lexical way:Created on 2022-06-21 by the reprex package (v2.0.0)
从本质上讲,这是一个“如何选择n个行/值的n组”问题。
在这里,首先总结并使用此信息来过滤原始数据,在这里进行了一种方法。
This is essentially a "how to chose groups with n most rows/ values" problem.
Here one approach by first summarising and using this information to filter the orginal data.