【问题标题】:Turning off ggplot2 grids in rpy2在 rpy2 中关闭 ggplot2 网格
【发布时间】:2013-02-01 20:27:21
【问题描述】:

在使用theme_bw 主题时,关闭 Rpy2 中所有网格的正确方法是什么?我知道我可以打开theme_bw,如下:

ggplot2.theme_set(ggplot2.theme_bw(12))

但不确定如何关闭网格。谢谢。

【问题讨论】:

    标签: python r ggplot2 rpy2


    【解决方案1】:

    基本上和使用 R 中的 ggplot2 时一样。

    这是一个关闭与 X 轴相交的网格的示例。更多“主题化”情节的方法可以在 ggplot2 的文档和教程中找到。

    from rpy2.robjects.lib.ggplot2 import ggplot, \
                                   aes_string, \
                                   geom_histogram, \
                                   element_blank, \
                                   theme_bw, \
                                   theme
    from rpy2.robjects import r
    
    nogrid_x_theme = theme(**{'panel.grid.major.x': element_blank(),
                             'panel.grid.minor.x': element_blank()})
    iris = r('iris')
    p = ggplot(iris) + geom_histogram(aes_string(x = 'Sepal.Width'))
    p += theme_bw() + nogrid_x_theme
    p.plot()
    

    【讨论】:

    • 谢谢。最后一个问题:如何将其全局应用于使用 rpy2 使用 ggplot2 制作的所有绘图,而不是像您拥有的那样将其添加到每个绘图对象中 p += ... nogrid_x_theme
    • @user248237 : 可能是编写一个 Python 函数,从数据框中绘制完整图,所以你看不到添加?
    猜你喜欢
    • 2013-09-04
    • 2012-03-21
    • 2011-11-23
    • 2013-09-03
    • 2020-02-25
    • 1970-01-01
    • 2013-02-02
    • 2016-06-23
    • 2013-03-06
    相关资源
    最近更新 更多