我想使用風向和例外資料stat_bin_2d。stat_density_2d我需要將風向分成8組。如下所示,A 是 using 的默認輸出stat_bin_2d,而 B 是stat_density_2d. C是風向被分成8個因子后,并以風向為因子繪制。D 是當風向作為因子變回數字時。如您所見,瓷磚之間存在間隙。F 試圖將 C (作為因子的風向)與連續 繪制在一起stat_density_2d。
有沒有辦法將離散(它基于連續)的規模與連續相匹配?
有沒有辦法加寬 D 中的瓷磚以匹配 C?
有沒有辦法手動輸入bin資訊stat_bin_2d,讓它自動生成C,但輸出仍然是連續的?
以下是我的嘗試。
# For reproducability
set.seed(190)
#Create a sample dataset and turn them into a data.table
Direction = runif(1000, min = 0, max = 360)
Anomaly = runif(1000, min = -10, max = 20)
Data = data.table(Direction, Anomaly)
#Plot a simple stat_bin_2d
ggplot(data = Data, aes(x = Anomaly, y = Direction)) stat_bin_2d() labs(title =
"(A) Default stat_bin_2d")
#Plot a simple stat_bin_2d with stat_density_2d
ggplot(data = Data, aes(x = Anomaly, y = Direction)) stat_bin_2d()
stat_density_2d(
geom = "polygon",
colour = "red",
alpha = 0,
bins = 6
)
labs(title = "(B) Default stat_bin_2d and stat_density_2d")
#Bins the direction into 16 bins, which are the major direction of the wind. First as abbreviation, second as mean direction.
Data[, Direction_Factor_Name := cut(
Direction,
breaks = seq(-22.5, 382.5, 45),
labels = c("N", "NE", "E", "SE", "S", "SW", "W", "NW", "N")
)]
Data[, Direction_Factor := cut(Direction,
breaks = seq(-22.5, 382.5, 45),
labels = c(seq(0, 350, 45), 0))]
Data[, Direction_New := as.numeric(levels(Direction_Factor))[Direction_Factor]]
ggplot() stat_bin_2d(data = Data, aes(x = Anomaly, y = Direction_Factor)) labs(title =
"(C) stat_bin_2d with Factor")
ggplot(data = Data, aes(x = Anomaly, y = Direction_New)) stat_bin_2d() labs(title =
"(D) stat_bin_2d with \nas.numeric(levels(Direction_Factor))[Direction_Factor]")
#Try to plot using Direction as factor with stat_density_2d
ggplot(data = Data, aes(x = Anomaly, y = Direction_Factor)) stat_bin_2d()
stat_density_2d(
geom = "polygon",
colour = "red",
alpha = 0,
bins = 6
)
labs(title = "(E) Default stat_bin_2d and stat_density_2d")
#Error in seq_len(n) : argument must be coercible to non-negative integer
#Try something else, ,but the y-axis does not match
ggplot(data = Data, aes(x = Anomaly, y = Direction))
stat_bin_2d(aes(y = Direction_Factor))
stat_bin_2d()
stat_density_2d(
geom = "polygon",
colour = "red",
alpha = 0,
bins = 6
)
labs(title = "(F) stat_bin_2d and stat_density_2d\nand stat_bin_2d with factor.")





更新:我現在使用 binwidth 引數。下面是我正在制作的整體情節的一個例子。雖然艾倫卡梅隆的極地圖非常好,但它不適合這個圖的目的。我可能會在其他地方使用它。

uj5u.com熱心網友回復:
您可以通過設定兩個軸的 binwidth 來處理連續資料:
ggplot(data = Data, aes(x = Anomaly, y = Direction))
stat_bin_2d(binwidth = c(4, 45))

如果要在 y 軸上使用離散值,請將 binwidth 設為 1:
ggplot(data = Data, aes(x = Anomaly, y = Direction_Factor_Name))
stat_bin_2d(binwidth = c(4, 1))

或者您可以使用極坐標獲得更直觀的結果:
library(geomtextpath)
ggplot(data = Data, aes(x = Anomaly, y = Direction_Factor_Name))
stat_bin_2d(binwidth = c(4, 1))
coord_curvedpolar(theta = "y", start = -pi/8)
geom_vline(xintercept = 0, color = "white")
theme_minimal()
scale_fill_viridis_c(option = "C")
theme(axis.text = element_text(size = 14, face = 2),
axis.title.x = element_blank())

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450730.html
