如何仅在散点图中选择前五名选项

发布于 2025-02-09 05:20:37 字数 1000 浏览 1 评论 0 原文

我只想在散点图中选择前五名。这是代码:

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) 

这是输出图:

以下是所需的图:

”在此处输入图像描述”

I want to choose only the top five options in a scatterplot. Here's the code:

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) 

This is the output graph:
enter image description here

The following is the desired graph:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稍尽春風 2025-02-16 05:20:37

管道 nest%&gt;%head(5)%&gt;%unnest 以数据框中出现的顺序为您提供前5组行。然后,ggplot2将以alpha词法方式对它们进行排序:

library(dslabs)
library(tidyverse)

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 %>%
  nest(-pollster) %>%
  head(5) %>%
  unnest(data) %>%
  ggplot(aes(spread, pollster)) +
  geom_point(color="black", binwidth = .01) 
#> Warning: All elements of `...` must be named.
#> Did you want `data = -pollster`?
#> Warning: Ignoring unknown parameters: binwidth

“”

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:

library(dslabs)
library(tidyverse)

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 %>%
  nest(-pollster) %>%
  head(5) %>%
  unnest(data) %>%
  ggplot(aes(spread, pollster)) +
  geom_point(color="black", binwidth = .01) 
#> Warning: All elements of `...` must be named.
#> Did you want `data = -pollster`?
#> Warning: Ignoring unknown parameters: binwidth

Created on 2022-06-21 by the reprex package (v2.0.0)

初熏 2025-02-16 05:20:37

从本质上讲,这是一个“如何选择n个行/值的n组”问题。

在这里,首先总结并使用此信息来过滤原始数据,在这里进行了一种方法。

library(dslabs)
library(tidyverse)

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)

polls %>% 
##count number within each group and slice the first 5
count(pollster) %>%
slice_max(n, n = 5) %>%
## merge back with your original data
semi_join(polls, ., by = "pollster") %>%
## plot as suggested by yourself
ggplot(aes(spread, pollster)) +
  geom_point(color="black", binwidth = .01) 
           

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.

library(dslabs)
library(tidyverse)

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)

polls %>% 
##count number within each group and slice the first 5
count(pollster) %>%
slice_max(n, n = 5) %>%
## merge back with your original data
semi_join(polls, ., by = "pollster") %>%
## plot as suggested by yourself
ggplot(aes(spread, pollster)) +
  geom_point(color="black", binwidth = .01) 
           

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文