[R] Limit the y-axis line with ggplot2 (not the axis itself, but the line used at the left of the graph) [SOLVED]

Marc Girondot marc.girondot at u-psud.fr
Tue Oct 25 06:33:08 CEST 2016


After many tries, here is a solution using grob.
I post here in case it could help someone.
Note that this solution is not 100% optimal as it uses a trick (limits = 
c(-0.05, 1.02)) to show fully the points.

Marc

library("ggplot2"); require("gtable"); require("grid")

p <- ggplot()+
   geom_point(aes(x=c(20, 29, 32), y=c(0, 0.4, 1)))+
   scale_y_continuous(breaks=c(0, 0.25, 0.5, 0.75, 1),
                      expand = c(0, 0), limits = c(-0.05, 1.02))+
   xlim(20, 32) +
   labs(x="Label for x axis")+
   labs(y="Label for y axis") +
   geom_segment(aes(x=25, xend=25, y=0, yend=1), linetype=2)+
   # This part is just to make it more nice
   theme(panel.background = element_rect(fill = 'white'),
         panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         plot.margin = unit(c(0.5, 1, 0.5, 0.5), "cm"),
         axis.text.x=element_text(size=14),
         axis.text.y=element_text(size=14),
         axis.title.x=element_text(size=18),
         axis.title.y=element_text(size=18),
         axis.ticks.length=unit(0.3,"cm"),
         panel.border = element_blank(),
         axis.line.x = element_line(),
         axis.line.y = element_line())


# I prepare the legend to be shown at the top
plegend <- ggplot() +
   geom_blank() +
   geom_segment(aes(x=25, xend=25, y=0, yend=0.4), linetype=2) +
   annotate("text", x = 25 , y=0.5, label="Text 1")+
   scale_y_continuous(expand=c(0,0), limits = c(0,1)) +
   scale_x_continuous(limits = c(20, 32)) +
   theme_minimal() + theme(line=element_blank(),
                           text=element_blank(),
                           panel.background=element_blank())

# extract the panel only
gl <- gtable_filter(ggplotGrob(plegend), "panel")

# And draw both
g <- ggplotGrob(p)
g <- gtable_add_rows(x = g, heights = unit(2, "cm"), pos = 0)
g <- gtable_add_grob(g, gl, t = 2, l=4, b=1, r=4)
grid.newpage()
grid.draw(g)



Le 24/10/2016 à 13:08, Marc Girondot via R-help a écrit :
> Hello everybody,
>
> Using ggplot2 package, is there a way to force to stop the y-axis line 
> at a specified point ? (not using ylim because I want that some text 
> written using annotate() at the top of the graph is still shown).
>
> Bellow is a simple example to show what I would like do:
>
> Thanks a lot
>
> Marc
>
>
>
>
>
> library("ggplot2")
>
> g <- ggplot()+
>   geom_point(aes(x=c(20, 29, 32), y=c(0, 0.4, 1)))+
>   scale_y_continuous(breaks=c(0, 0.25, 0.5, 0.75, 1))+
>   labs(x="Label for x axis")+
>   labs(y="Label for y axis") +
>   annotate("text", x = 25 , y=1.2, label="Text 1")+
>   annotate("text", x = 22 , y=1.0, label="How to stop the y-axis line 
> here !")+
>   geom_segment(aes(x=25, xend=25, y=0, yend=1.1), linetype=2)+
>   # This part is just to make it more nice
>   theme(panel.background = element_rect(fill = 'white'),
>         panel.grid.major = element_blank(),
>         panel.grid.minor = element_blank(),
>         plot.margin = unit(c(0.5, 1, 0.5, 0.5), "cm"),
>         axis.text.x=element_text(size=14),
>         axis.text.y=element_text(size=14),
>         axis.title.x=element_text(size=18),
>         axis.title.y=element_text(size=18),
>         axis.ticks.length=unit(0.3,"cm"),
>         panel.border = element_blank(),
>         axis.line.x = element_line(),
>         axis.line.y = element_line())
> g
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
__________________________________________________________
Marc Girondot, Pr

Laboratoire Ecologie, Systématique et Evolution
Equipe de Conservation des Populations et des Communautés
CNRS, AgroParisTech et Université Paris-Sud 11 , UMR 8079
Bâtiment 362
91405 Orsay Cedex, France

Tel:  33 1 (0)1.69.15.72.30   Fax: 33 1 (0)1.69.15.73.53
e-mail: marc.girondot at u-psud.fr
Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html
Skype: girondot



More information about the R-help mailing list