我可以使用ggplot分别控制x和y轴吗? [英] can I separately control the x and y axes using ggplot?

查看:120
本文介绍了我可以使用ggplot分别控制x和y轴吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是来自。

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


解决方案



  mytheme<  -  theme_classic()
mytheme $ axis.line.x< - mytheme $ axis.line.y< - mytheme $ axis .line
mytheme $ axis.line.x $ color< - 'red'
ggplot(iris,aes(Sepal.Length,Sepal.Width)) + geom_point()+ mytheme


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

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

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

解决方案

It seems you can also achieve it easily by adapting a theme thusly:

mytheme <- theme_classic()
mytheme$axis.line.x <- mytheme$axis.line.y <- mytheme$axis.line
mytheme$axis.line.x$colour <- 'red'
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + mytheme

这篇关于我可以使用ggplot分别控制x和y轴吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆