我正在嘗試向現有的 ggplot 添加額外的刻度和標簽。即,
> df_partial_1[10:20,]
X v Tv_partial upper lower
10 10 10 9.473527 9.561264 9.385789
11 11 11 10.185980 10.292432 10.079528
12 12 12 10.876827 11.002762 10.750893
13 13 13 11.546730 11.692879 11.400580
14 14 14 12.196326 12.363378 12.029275
15 15 15 12.826239 13.014827 12.637651
16 16 16 13.437071 13.647775 13.226367
17 17 17 14.029405 14.262751 13.796060
18 18 18 14.603809 14.860269 14.347349
19 19 19 15.160833 15.440830 14.880835
20 20 20 15.701009 16.004918 15.397100
是我的資料。使用代碼:
require(ggplot2)
ggplot(df_partial_1[10:20,], aes(x = log(v), y = log(Tv_partial))) theme_bw() xlab("v") ylab(bquote(lnT[v])) scale_x_continuous(labels = ~floor(exp(.)), sec.axis=sec_axis(~., name = "ln v"))
geom_line(aes(y=log(upper)), linetype="dashed")
geom_line(aes(y=log(Tv_partial)))
geom_line(aes(y=log(lower)), linetype="dashed")
我可以獲得情節:

如何v = 12.5在底軸上添加一個刻度?我在下面找到了答案,但無法使其作業

uj5u.com熱心網友回復:
示例資料:
> dput(df_partial_1)
structure(list(X = 10:20, v = 10:20, Tv_partial = c(9.4856413914822,
10.146674007643, 10.8167272810824, 10.9265517373569, 11.1305892295204,
11.5879461094737, 11.9317112197168, 12.3461310826242, 13.3646452780813,
13.7879541954026, 14.9864157652482), upper = c(10.3638426321559,
11.7010454921983, 11.9959417306818, 12.7341023595072, 12.7746323188767,
12.7814749288373, 13.2831874601543, 13.5523599018343, 14.0208351933397,
15.6414199303836, 15.7897500004619), lower = c(8.29995565023273,
8.36215050611645, 8.57158890971914, 8.72145544551313, 8.85831257328391,
10.4443473443389, 11.2121904632077, 11.5232664612122, 12.0696932873689,
13.0791664700955, 13.9007287397981)), class = "data.frame", row.names = c(NA,
-11L))
第一個選項:您可以使用grid添加一行和文本。但是,在這里您必須手動設定 x 和 y 引數。
在你的 ggplot 之后添加這個:
library("grid")
grid.lines(x = unit(c(0.385, 0.385), "npc"), y = unit(c(0.06, 0.1), "npc")) # line
grid.text("12.5", .36, .11,just=c("left", "bottom")) # text
輸出:

第二個選項:用于geom_segment添加行和grid.text文本:
ggplot(df_partial_1, aes(x = log(v), y = log(Tv_partial))) theme_bw() xlab("v") ylab(bquote(lnT[v])) scale_x_continuous(labels = ~floor(exp(.)), sec.axis=sec_axis(~., name = "ln v"))
geom_line(aes(y=log(upper)), linetype="dashed")
geom_line(aes(y=log(Tv_partial)))
geom_line(aes(y=log(lower)), linetype="dashed") geom_segment(aes(x = log(12.5), y = -Inf, xend = log(12.5), yend = 2.1))
grid.text("12.5", .36, .11,just=c("left", "bottom"))
輸出:

如果要更改線條顏色:添加color="red"為引數geom_segment
更改文本顏色:添加gp=gpar(col="red")為grid.text引數。
uj5u.com熱心網友回復:
您可以將 geom_segment 與 xend 和 yend 一起使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/524539.html
標籤:rggplot2
上一篇:在R中繪制3D物件
