這是我的代碼:
ggplot(data = tibble(x = 1:20, y = 1:20),
aes(x = x, y = y))
geom_curve(x = 10, y = 7.55, xend = 15, yend = 7.6,
curvature = .28, size = .7250, color ='black')
geom_point()
我怎樣才能填充這條曲線下方的區域?之間x = 10和x = 12和考慮ymax是曲線。
uj5u.com熱心網友回復:
我不知道如何提取geom_curve渲染程序中產生的內部資料,但這里有一個方法:
gg <- ggplot(data = tibble(x = 1:20, y = 1:20),
aes(x = x, y = y))
geom_curve(x = 10, y = 7.55, xend = 15, yend = 7.6,
curvature = .28, size = .7250, color ='black')
geom_point()
我們將自己制作曲線。我在51這里使用只是為了獲得很多積分;根據曲線的大小和最終繪圖的解析度,您可能需要更少或更多。
curvedat <- data.frame(x = seq(10, 15, len = 51))
curvedat$y <- with(curvedat, 4 5*abs((x - 10) / 5 - 0.5)^2)
gg geom_path(data = curvedat)

(這里將兩條曲線一起顯示以證明它們的相似性。沒有假設在圖中保持原始曲線。此外,第二條曲線顯示為向下偏移,我假設您會在您的適用性決定中解決這個問題。 )
一旦你的曲線看起來不錯,然后在它周圍包裹兩個“低”點并添加為多邊形:
gg
geom_polygon(data = rbind(
data.frame(x = min(curvedat$x), y = -Inf),
curvedat,
data.frame(x = max(curvedat$x), y = -Inf)
))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/325054.html
上一篇:如何疊加兩個ggplots
下一篇:如何使用R繪制世界地圖
