Home > Blockchain >  Add an extra tick and label to bottom horizontal axis for a ggplot with two horizontal axies
Add an extra tick and label to bottom horizontal axis for a ggplot with two horizontal axies

Time:11-01

I am trying to add an extra tick and label to an existing ggplot. Namely,

> 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

is my data. Using codes:

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")

I can obtain the plot:

enter image description here

How can I add a tick at v = 12.5 on the bottom axis? I found an answer below but could not make it work

enter image description here

CodePudding user response:

Example data:

> 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))

First option: You can use grid to add a line and text. However, here you have to set the x and y parameters manually.

Add this after your 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

output: enter image description here

Second option: Use geom_segment to add line and grid.text for 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"))

output: enter image description here

If you want to change line color: add color="red" as parameter to geom_segment Change text color: add gp=gpar(col="red") as grid.text parameter.

CodePudding user response:

you can use geom_segment with xend and yend

  • Related