[英]Geom_bar plot only shows one bar per plot
I am trying to compare two NBA players in various stats by using geom_bar
but for some reason it is only plotting one bar per stat.我正在尝试使用geom_bar
比较两个 NBA 球员的各种统计数据,但由于某种原因,它只为每个统计数据绘制一个条形图。 I would like to have both bars on each plot to see how the two players compare to each other, or in other words a red and a blue on every plot我想在每个 plot 上都有两个条,以查看两个玩家之间的比较,或者换句话说,每个 plot 上的红色和蓝色
This is what I have tried这是我尝试过的
url <- paste0('https://www.basketball-reference.com/leagues/NBA_2022_per_game.html')
# scrape the url
webpage <- read_html(url)
hp_table <-
html_nodes(webpage, "table")
nba_table <- html_table(hp_table)
# convert to dataframe
df <- as.data.frame(nba_table)
df <- df[,-1]
df
longer <- df %>% pivot_longer(-Player)
player_list <- c('Precious Achiuwa', 'Steven Adams')
filtered <- longer %>% filter(Player == player_list)
ggplot(filtered, aes(x = Player, y = value)) +
geom_bar(aes(fill = Player),
stat = 'identity')+
facet_wrap(~name, scales = 'free_y')+
theme(axis.text.x = element_blank())
url <- paste0('https://www.basketball-reference.com/leagues/NBA_2022_per_game.html')
# scrape the url
webpage <- read_html(url)
hp_table <-
html_nodes(webpage, "table")
nba_table <- html_table(hp_table)
# convert to dataframe
df <- as.data.frame(nba_table)
df <- df[,-1]
df <- df %>% pivot_longer(cols = Pos:PTS) %>%
mutate(value = as.numeric(value)) %>%
filter(!is.na(value)) %>%
filter(Player %in% c("Precious Achiuwa", "Steven Adams"))
ggplot(df, aes(x = Player, fill = Player, y = value)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~name, "free_y") +
theme(axis.text.x = element_blank())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.