r - 如何在 ggplot 主题中设置 scale_ defaults?

标签 r ggplot2 themes scale

我想创建一个具有不同于默认刻度线的主题,以避免每次创建自定义绘图时都重复 + scale_x_continuous

让我们举一个简单的例子,我只想要极限处的刻度线:

A = 1; f = 5; p = 0; d = 0.4
t = seq(from = 0, to = 10, by = 0.01)

x = A * sin(t * f + p) * exp(-d * t)
numticks = 1

qplot(x = t, y = x) + theme_classic() + 
  scale_x_continuous(breaks = scales::trans_breaks("identity", function(x) x, n=numticks) )

这看起来很棒(见下文),但理想情况下我无需使用 scale_x_continuous 就可以产生相同的结果,因为它很笨重。我想用 + mytheme 替换 theme_classic() 应该可以解决问题。

这是我迄今为止在解决方案方面的最佳尝试:

mytheme <- theme_classic() + 
theme(scale_x_continuous(breaks=trans_breaks("identity", function(x) x, n=numticks)) 

不确定这是否可能,很高兴每次都继续输入 scale_x_continuous(...)。但最好临时更改默认值,或者在自定义主题中更好。

顺便说一句,这是我想要制作的绘图轴,我想制作许多这样的绘图(用于绘图 harmonographs):

custom x axis

最佳答案

不确定这是否是您想要的,但这会为您节省一些空间:

custom_ticks <- function(n) {
  scale_x_continuous(breaks = 
    scales::trans_breaks("identity", function(x) x, n=n))
}

p <- qplot(x = t, y = x) + theme_classic() 
p + custom_ticks(2)
p + custom_ticks(10)

关于r - 如何在 ggplot 主题中设置 scale_ defaults?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20543823/

相关文章:

r - 如何加入具有多列和多个值的 data.table

r - Monty Hall In R - setdiff 意想不到的结果

r - ggplot 堆叠条形排序失败,函数 desc 未知

android - 如何以我的风格获取我自己定义的属性值

html - 网站 wordpress 中的文本溢出

r - 数据框中行名称的交集(数据子集)?

r - R 中的 Monty Hall 游戏,带有基本函数

r - 更改gganimate框架标题的标签

r - 使用 ggplot2 在堆积条形图中对分类数据进行排序

android - 我无法更改我的 AlertDialog 主题