當geom_ribbon()與形狀美學一起使用時,陰影區域的不透明度似乎有所不同,從而在該區域中創建了塊。我重新創建了一個問題,我發現這些不透明度變化僅在包含形狀美學時才存在。
資料設定:
alpha <- c("A","B","C","D", "E", "F", "G")
percent <- c(0.012, -0.02, 0.015, -0.01, 0.89, 0.12, -0.25)
flow <- c(-5, 2, -3, 3, 1, 4, -2)
shape <- c("D", "D", "L", "L", "L", "D", "L")
df <- data.frame(alpha,percent,flow, shape)
x_min = min(df$percent)
x_min = round(x_min/0.01)*0.01 - 0.01
x_max = max(df$percent)
x_max = round(x_max/0.01)*0.01 0.01
y_min = min(df$flow)
y_min = round(y_min)
y_max = max(df$flow)
y_max = round(y_max)
n_row = nrow(df)
沒有形狀美學的圖表,geom_ribbon() 有效:
df %>%
ggplot(aes(x = percent, y = flow, label = alpha))
geom_point()
geom_text_repel(show.legend = FALSE, size = 3)
scale_size_continuous(labels = scales::percent)
theme_bw()
scale_x_continuous(labels = scales::percent_format(accuracy = 0.1L))
scale_y_continuous(labels = scales::dollar_format(negative_parens = TRUE, suffix = "m"))
geom_ribbon(aes(x = seq(x_min, x_max 0.01, length.out = n_row), ymin = 0, ymax = y_max), alpha = 0.04, linetype = 0, show.legend = FALSE, fill = "green")
geom_ribbon(aes(x = seq(x_min, x_max 0.01, length.out = n_row), ymin = y_min, ymax = 0), alpha = 0.04, linetype = 0, show.legend = FALSE, fill = "red")

具有形狀美學的圖表,geom_ribbon() 似乎不起作用:
df %>%
ggplot(aes(x = percent, y = flow, label = alpha, shape = shape))
geom_point()
geom_text_repel(show.legend = FALSE, size = 3)
scale_size_continuous(labels = scales::percent)
theme_bw()
scale_x_continuous(labels = scales::percent_format(accuracy = 0.1L))
scale_y_continuous(labels = scales::dollar_format(negative_parens = TRUE, suffix = "m"))
geom_ribbon(aes(x = seq(x_min, x_max 0.01, length.out = n_row), ymin = 0, ymax = y_max), alpha = 0.04, linetype = 0, show.legend = FALSE, fill = "green")
geom_ribbon(aes(x = seq(x_min, x_max 0.01, length.out = n_row), ymin = y_min, ymax = 0), alpha = 0.04, linetype = 0, show.legend = FALSE, fill = "red")

uj5u.com熱心網友回復:
問題是某種過度繪圖,并且您添加背景的方式通過geom_ribbon. 基本上,通過添加shape全域美學,您的資料會被分組,并且您的功能區會被多次繪制,每個組一次。要解決此問題shape,請通過將其移動到內部來制作本地 aes geom_point:
library(ggplot2)
ggplot(df, aes(x = percent, y = flow, label = alpha))
geom_point(aes(shape = shape))
ggrepel::geom_text_repel(show.legend = FALSE, size = 3)
scale_size_continuous(labels = scales::percent)
theme_bw()
scale_x_continuous(labels = scales::percent_format(accuracy = 0.1L))
scale_y_continuous(labels = scales::dollar_format(negative_parens = TRUE, suffix = "m"))
geom_ribbon(aes(x = seq(x_min, x_max 0.01, length.out = n_row), ymin = 0, ymax = y_max), alpha = 0.04, linetype = 0, show.legend = FALSE, fill = "green")
geom_ribbon(aes(x = seq(x_min, x_max 0.01, length.out = n_row), ymin = y_min, ymax = 0), alpha = 0.04, linetype = 0, show.legend = FALSE, fill = "red")

但是,geom_ribbon在我看來,您可以通過添加背景矩形來更輕松地獲得結果,并且更不容易出錯annotate,這取決于您想要的結果,甚至不需要計算最小值和最大值。相反,您可以簡單地使用-Infand Inf:
ggplot(df, aes(x = percent, y = flow, label = alpha, shape = shape))
geom_point()
ggrepel::geom_text_repel(show.legend = FALSE, size = 3)
scale_size_continuous(labels = scales::percent)
theme_bw()
scale_x_continuous(labels = scales::percent_format(accuracy = 0.1))
scale_y_continuous(labels = scales::dollar_format(negative_parens = TRUE, suffix = "m"))
annotate(geom = "rect", xmin = -Inf, xmax = Inf, ymin = 0, ymax = Inf, alpha = 0.04, fill = "green")
annotate(geom = "rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 0, alpha = 0.04, fill = "red")

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