我想創建一個在 Y 軸上具有 topBatter 并且在 Y 軸上具有 topTen 距離的線段/點圖。當我創建它時,情節顛倒了 X 和 Y。
我有點不知道為什么這個不起作用。
感謝您的任何指導。我很欣賞這個社區。
#Top HR by Distance
topTenDistance <- c(485, 502, 484, 492, 489, 499, 500, 497, 489, 501)
topBatter <- c("Ba", "Ca", "Da", "Fa", "Ga", "Ha", "Ja", "Ka", "Ya", "Ja")
topCombined <- cbind(topBatter, topTenDistance)
topCombined <- as.data.frame(topCombined)
topCombined %>%
ggplot(aes(x=topTenDistance, y=topBatter))
geom_segment( aes(xend=topTenDistance, yend=0))
geom_point( size=1.5, color="#134A8E")
scale_y_discrete()
coord_flip()
ggtitle("Top HRs by Distance")
labs(subtitle = "MLB 2022 Season")
xlab("Player's Name")
ylab("Distance (in ft")

uj5u.com熱心網友回復:
有幾件事。首先,之后
topTenDistance <- c(485, 502, 484, 492, 489, 499, 500, 497, 489, 501)
topBatter <- c("Ba", "Ca", "Da", "Fa", "Ga", "Ha", "Ja", "Ka", "Ya", "Ja")
topCombined <- cbind(topBatter, topTenDistance)
topCombined <- as.data.frame(topCombined)
topCombined$topTenDistance是性格:
as_tibble(topCombined)
# A tibble: 10 × 2
topBatter topTenDistance
<chr> <chr>
1 Ba 485
2 Ca 502
3 Da 484
4 Fa 492
5 Ga 489
6 Ha 499
7 Ja 500
8 Ka 497
9 Ya 489
10 Ja 501
那沒有幫助。所以像這樣構建你的data.frame:
topCombined <- data.frame(
topTenDistance=c(485, 502, 484, 492, 489, 499, 500, 497, 489, 501),
opBatter=c("Ba", "Ca", "Da", "Fa", "Ga", "Ha", "Ja", "Ka", "Ya", "Ja")
)
然后
topCombined %>%
ggplot(aes(x=topTenDistance, y=topBatter))
geom_segment(aes(xend=480, yend=topBatter))
geom_point( size=1.5, color="#134A8E")
scale_y_discrete()
ggtitle("Top HRs by Distance")
labs(subtitle = "MLB 2022 Season")
ylab("Player's Name")
xlab("Distance (in ft)")
生產

這是你想要的嗎?您可以將xend呼叫中的值設定geom_segment為您認為美觀的任何值。
請注意,“雙點”Ja是您的測驗資料的一個特征。
順便,
topCombined %>%
ggplot(aes(y=topBatter))
geom_linerange(aes(xmin=480, xmax=topTenDistance))
geom_point(aes(x=topTenDistance), size=1.5, color="#134A8E")
labs(
title="Top HRs by Distance",
subtitle = "MLB 2022 Season",
y="Player's Name",
x="Distance (in ft)"
)
使用稍微簡化的管道生成相同的圖形。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523488.html
標籤:rggplot2
上一篇:更改李克特包R中每個條的顏色
下一篇:根據alpha值獲取顏色的名稱
