Hi
Here are some alternatives that involve messing about with the grobs and
viewports after the plot has been drawn. The percentage optimality of
these solutions is up for debate ...
###
# Edit the y-axis line after drawing
library("ggplot2"); library("grid")
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
# Force creation of all grobs
grid.force()
# List all grobs
grid.ls()
# Get y-axis line y-locations
grid.get("axis.line.y..polyline", grep=TRUE)$y
# Edit y-axis line y-locations (trial-and-error to get 0.81)
grid.edit("axis.line.y..polyline", grep=TRUE, y=unit(c(0, 0.81), "npc"))
###
# Insert marker to work off and edit the y-axis line after drawing
library("ggplot2"); library("grid")
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()) +
# marker
geom_point(aes(x=20, y=1), col="red")
g
# Force creation of all grobs
grid.force()
# List all grobs
grid.ls()
# Navigate to panel viewport
downViewport("panel.6-4-6-4")
# Get marker
marker <- grid.get("geom_point", grep=TRUE, global=TRUE)[[2]]
# Edit y-axis line y-locations (based on marker)
grid.edit("axis.line.y..polyline", grep=TRUE,
y=convertY(unit(0:1, c("npc", "groby"), list(NULL, marker)),
"npc"))
# grid.edit("axis.line.y..polyline", grep=TRUE,
# y=unit(c(0, convertY(grobY(marker, 0), "npc", valueOnly=TRUE)),
# "npc"))
# Remove marker
grid.remove(marker$name)
###
# Just draw the plot, with margin above, vertical dash line clipped
library("ggplot2"); library("grid")
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),
limits=c(-.05, 1), expand=c(0, 0),
oob=function(x, range) x)+
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(3, 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
# Force creation of all grobs
grid.force()
# List all grobs
grid.ls()
# Navigate to panel viewport
downViewport("panel.6-4-6-4")
# Get the line segment and label
seg <- grid.get("segments", grep=TRUE)
lab <- grid.get("text", grep=TRUE)
# Push new viewport with clipping off
pushViewport(viewport(clip="off"))
# Draw line segment and text
grid.draw(seg)
grid.draw(lab)
# Clean up
upViewport()
Paul
On 25/