r - 我可以使用 ggplot 分别控制 x 和 y 轴吗?

标签 r ggplot2

这是来自 ggplot wiki 的 ggplot :

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

enter image description here



是否可以分别控制这些轴,例如只使 x 轴变黑?似乎没有axis.line.xaxis.line.y是其中的选项。

我试过的
  • 维基证明,例如,可以控制轴的颜色
    baseplot + opts(axis.line = theme_segment(colour = 'black', size = 2))
    

    enter image description here
  • 使用 geom_segment工作,但有线条必须与情节编号匹配的限制。

    有没有办法得到,例如baseplot 中的轴最大值和最小值以及刻度目的?这将减少潜在的错误。更新此问题的答案,“不,还没有”,已涵盖 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

    最佳答案

    不支持单独控制轴线。
    您可以在绘制后删除或编辑线条:

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

    更新

    在 0.9.1- 中,这可能会更改为:
    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"))
    

    关于r - 我可以使用 ggplot 分别控制 x 和 y 轴吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6541329/

    相关文章:

    r - 使用表达式标签时组合填充和颜色图例

    r - 在 r 中循环 ggplot2 公式

    r - 我该如何处理错误 "Error: all(lengths == 1L | lengths == n) is not TRUE"?

    r - R 中的多列子集 - 更优雅的代码?

    css - R shinyDashboard 自定义框状态颜色

    r - 条形图,几何底部和 x 轴之间没有空间,上方保持空间

    使用多个 aes 设置绘制构面时从 ggplot 中的图例中删除元素

    r - 使用 ggplot 的调色板

    r - 使用新数据预测.lm

    r - 如何创建循环文件夹并返回表格和绘图的 knitr 报告