我有一個包含三列的資料集。一個是播放器;第二個是 Run_Value;第三個是 Num_years。我希望 Player 出現在 Y 軸上,Run_Value 顯示為 X 軸上的條,Num_Years 顯示為 Run_Value 條內或旁邊的一個較小的條。以下是我的條形圖當前的顯示方式。Num_Years 資料未顯示,這是我需要的。
此外,如果在每個 Run_Value 條的末尾可以出現它的 Run_Value,那就太好了。
我在 Stackoverflow 上搜索了一個類似的問題,但找不到。如果有,請分享鏈接。
--我在我的螢屏上看不到這兩個上傳的影像,不知道為什么。
Dplyr 代碼:
sp2358test <- spAll |>
filter(RunValue < 0) |>
select(Player, RunValue) |>
add_count(Player, name="Num_years") |>
filter(Num_years %in% c(2:8)) |>
arrange(Num_years, Player) |>
group_by(Player, Num_years) |>
summarise(Run_Value = sum(RunValue))
部分 dplyr 結果

我的 ggplot2 代碼
sp2358test |>
ggplot(aes(fill=Run_Value, y=Player, x=Run_Value))
geom_bar(position='dodge', stat='identity')
lims(x = c(-250,0))
guides(fill=guide_legend(title="Legend"))
ggtitle("Mets Starters with Top Run Value Totals",
subtitle = "Data from Statcast for 2008-2021")
theme(plot.title = element_text(size = 14, color = "black"),
plot.subtitle = element_text(size = 11, color = "purple"),
text=element_text(color="blue"),
axis.text=element_text(color="black"),
legend.title = element_text(color = "black", size = 11)
)
我的 ggplot2 影像

spAll 資料幀的 dput 結果:
structure(list(Player = c("deGrom, Jacob", "Dickey, R.A.", "Gee, Dillon",
"Harvey, Matt", "Lugo, Seth", "Maine, John", "Matz, Steven",
"Niese, Jonathon", "Pelfrey, Mike", "Santana, Johan", "Syndergaard, Noah",
"Wheeler, Zack"), Num_years = c(8L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
3L, 3L, 5L, 3L), Run_Value = c(-240.4, -56.3, -11.2, -70.3, -8.5,
-8.1, -16, -11.8, -20.7, -87.8, -77.5, -43.1)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), groups = structure(list(
Player = c("deGrom, Jacob", "Dickey, R.A.", "Gee, Dillon",
"Harvey, Matt", "Lugo, Seth", "Maine, John", "Matz, Steven",
"Niese, Jonathon", "Pelfrey, Mike", "Santana, Johan", "Syndergaard, Noah",
"Wheeler, Zack"), .rows = structure(list(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L), .drop = TRUE))
uj5u.com熱心網友回復:
您可能希望避免使用兩個 x 軸。考慮使用 facet_wrap 的替代方案。請注意,您首先需要將資料轉換為長格式。我已將 df 用于您的資料集。
library(ggplot2)
library(dplyr)
library(tidyr)
df2 <- df %>%
pivot_longer(cols = c(Num_years, Run_Value), names_to = "metric")
df2 %>%
ggplot()
geom_col(aes(y=Player, x = value, fill = value))
facet_wrap(~rev(metric),scales = "free_x")
ggtitle("Mets Starters with Top Run Value Totals",
subtitle = "Data from Statcast for 2008-2021")
theme(plot.title = element_text(size = 14, color = "black"),
plot.subtitle = element_text(size = 11, color = "purple"),
text=element_text(color="blue"),
axis.text=element_text(color="black"),
legend.title = element_text(color = "black", size = 11)
)
它讓你看起來像這樣。問題是您有兩個變數正在使用填充比例,而這兩個變數的值非常不同。你的意圖是什么?

uj5u.com熱心網友回復:
這是另一個更接近您的原始內容和您想要描述的內容。
library(ggplot2)
library(dplyr)
library(tidyr)
df2 <- df %>%
pivot_longer(cols = c(Num_years, Run_Value), names_to = "metric")
df2 %>%
ggplot()
geom_bar(aes(y=Player, x = value, group = metric, fill = metric), stat='identity',
position = "dodge")
ggtitle("Mets Starters with Top Run Value Totals",
subtitle = "Data from Statcast for 2008-2021")
theme(plot.title = element_text(size = 14, color = "black"),
plot.subtitle = element_text(size = 11, color = "purple"),
text=element_text(color="blue"),
axis.text=element_text(color="black"),
legend.title = element_text(color = "black", size = 11)
)
您在一個變數中有正數,而在另一個變數中有負數。忽略 xlim 很重要,否則它將洗掉 Num-years 變數的所有正值。

uj5u.com熱心網友回復:
我想這就是你要找的。我反轉了 Run_Value 的符號,以便可以將資料繪制為正值。我按年份對 y 軸進行了排序。我使用顏色來區分兩個 x 軸。次軸必須縮放到第一個,所以我除以 10。這就是為什么它變為 25 而主軸變為 250。為了對齊,Years 通過乘以 10 來縮放。

df %>%
mutate(Run_Value = -(Run_Value)) %>%
ggplot()
geom_bar(aes(x = Run_Value, y = reorder(Player, Num_years)), stat = 'identity', fill = "orange")
geom_bar(aes(x = (Num_years * 10), y = Player), stat = 'identity', fill = "blue", width = 0.3)
ggtitle("Mets Starters with Top Run Value Totals",
subtitle = "Data from Statcast for 2008-2021")
scale_x_continuous(name = "Negative Run Value", sec.axis = sec_axis(~ ./10, name = "Years"))
theme(plot.title = element_text(size = 14, color = "black"),
plot.subtitle = element_text(size = 11, color = "purple"),
text=element_text(color="blue"),
axis.text=element_text(color="black"),
legend.title = element_text(color = "black", size = 11),
axis.title.x = element_text(color = "orange"),
axis.title.x.top = element_text(color = "blue"))
labs( y = "Player (Ordered by Years)")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/427681.html
