我研究了 19 世紀幾十年(=x)的物種滅絕(=y)。我正在繪制一條平滑樣條曲線,它顯示滅絕物種的累積值,跨越一組顯示每十年絕對值的幾個條形圖。現在,平滑樣條曲線在最后一根柱的中心結束,因為值出現在 X 位置的中心。由于這在視覺上令人不快,我想擴展樣條線,使其繼續到情節的邊界。
我試過這個position論點,但躲閃沒有幫助。該geom_bspline功能似乎也沒有提供解決方案(例如 type="open" 沒有幫助)。
所以我正在尋找一種允許從 ggforce 包中擴展 geom_bspline 的解決方案(或類似的解決方案)。
下面我添加了情節和情節的代碼示例。

library("tidyverse")
library("ggforce")
## DATASET
dftest <- tibble(
x = seq(1,11,1),
y1 = c(3, 0 , 1 , 0 , 2 ,0, 20, 126, 426, 400 , 37),
y2 = c( 3 ,3 , 4 , 4 , 6 , 6 , 26, 152 , 578 , 978, 1015)
)
###### Figure ------
ggplot(dftest, aes(x=x))
### BARS
geom_bar(aes(y= y1), fill="#FC301A", stat="identity")
### SPLINE
geom_bspline(aes(x=x,y=y2),data=dftest)
### SCALES
scale_x_discrete(name ="", drop = FALSE)
scale_y_continuous(expand = c(0,0),
limits = c(0, 1200),
breaks = seq(0,1200,200),name = "Kumulativ",
sec.axis = sec_axis(~ ((. - a)/b) 2,
name = "Absolut"),
labels=scales::comma_format(big.mark = "."))
### THEME
theme_classic()
theme(
panel.grid.major.y = element_line(colour = "grey70", size = 0.2), # linien
###axis
axis.ticks.length = unit(1, "mm"),
axis.text.x = element_text(family = "Helvetica", size=10,angle = 90,vjust = 0.5,
margin = unit(c(0.2,0,0,0),"cm")),
axis.text.y = element_text(family = "Helvetica", size=10),
axis.text.y.right = element_text(color = "red",family = "Helvetica", size=10),
axis.title.y.right = element_text(color = "red",family = "Helvetica", size=10,
margin = unit(c(0,0,0,0.25),"cm")),
axis.title.y.left = element_text(family = "Helvetica", size=10,
margin = unit(c(0,0.25,0,0),"cm"))
)
uj5u.com熱心網友回復:
樣條曲線必須在結之間進行插值,并且除非您推斷結束結,否則無法推斷,因此最簡單的方法是創建資料框的副本并添加一個額外的行,其中包含額外的時間測量,例如x = 11.5 或 x = 11.7(無論你覺得“看起來正確”的地方),但具有相同的y2值:
dftest2 <- rbind(dftest, tail(dftest, 1))
dftest2$x[nrow(dftest2)] <- 11.7
然后你可以這樣做:
ggplot(dftest, aes(x = x))
geom_bar(aes(y= y1), fill="#FC301A", stat="identity")
geom_bspline(aes(x = x, y = y2), data = dftest2)
scale_x_discrete(name ="", drop = FALSE)
scale_y_continuous(expand = c(0,0),
limits = c(0, 1200),
breaks = seq(0,1200,200),name = "Kumulativ",
sec.axis = sec_axis(~ ((. - 0)/2.3) 2,
name = "Absolut"),
labels=scales::comma_format(big.mark = "."))
theme_classic()
theme(
panel.grid.major.y = element_line(colour = "grey70", size = 0.2),
axis.ticks.length = unit(1, "mm"),
axis.text.x = element_text(family = "Helvetica",
size=10,angle = 90, vjust = 0.5,
margin = unit(c(0.2,0,0,0),"cm")),
axis.text.y = element_text(family = "Helvetica",size=10),
axis.text.y.right = element_text(color = "red",
family = "Helvetica", size = 10),
axis.title.y.right = element_text(color = "red",family = "Helvetica",
size=10,
margin = unit(c(0,0,0,0.25),"cm")),
axis.title.y.left = element_text(family = "Helvetica", size=10,
margin = unit(c(0,0.25,0,0),"cm"))
)

Note that your code was not fully reproducible because you didn't include the variables a and b for your secondary axis, so I had to approximate your secondary axis here.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/443265.html
上一篇:更改GGDAG中特定節點的顏色
