Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

r - can I separately control the x and y axes using ggplot?

Here is a ggplot from the ggplot wiki:

baseplot <- ggplot(data.frame(x=1:10, y=10:1)) +
    geom_point(aes(x = x, y = y))
baseplot

enter image description here

Question

Is it possible to control these axes separately, e.g. to make only the x-axis black? It does not appear that axis.line.x and axis.line.y are among the options.

What I have tried

  1. The wiki demonstrates that, e.g., it is possible to control the color of the axis

    baseplot + opts(axis.line = theme_segment(colour = 'black', size = 2))
    

    enter image description here

  2. using geom_segment works but has the limitation that the lines have to be matched to the plot numbers.

    Is there a way to get, e.g. the axis max and min and ticks from the baseplot object? That would reduce potential bugs. update the answer to this question, "no, not yet", was covered previously.

    baseplot + geom_segment(aes(x = c(0,0), y = c(0,0), 
                            yend = c(0, max(y)), xend = c(max(x), 0), 
                            size = c(0.5, 0.1))) + 
               geom_segment(aes(x = 0, y = y, 
                            xend = -1, 
                            yend = y, 
                            size = 0.1))
    

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is not supported to control axis line separately. You can remove or edit the line after drawing:

> baseplot + opts(axis.line = theme_segment(colour = 'black', size = 2))
> grid.remove(gPath("axis_v", "axis.line.segments"), grep=TRUE)

> baseplot + opts(axis.line = theme_segment(colour = 'black', size = 2))
> grid.edit(gPath("axis_v", "axis.line.segments"), grep=TRUE, gp=gpar(col="red"))
> grid.edit(gPath("axis_h", "axis.line.segments"), grep=TRUE, gp=gpar(col="blue"))

UPDATED

In 0.9.1-, this may change like:

grid.edit(gPath("axis-l", "axis.line.segments"), grep=TRUE, gp=gpar(col="red"))
grid.edit(gPath("axis-b", "axis.line.segments"), grep=TRUE, gp=gpar(col="blue"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...