【问题标题】:Is there a way to customize the ticks in ggplot2 themes? [duplicate]有没有办法自定义 ggplot2 主题中的刻度? [复制]
【发布时间】:2020-04-15 00:53:55
【问题描述】:

如你所知,ggplot2中有很多主题,如theme_economist、theme_minimal、theme_tufte等。

例如,我想将此代码添加到我的可视化中:

theme_economist(axis.text.x = element_text(angle = 90))

但我收到“未使用的参数”错误,因为我只能将此参数添加到主题函数。

您可以简单地使用以下代码:

ggplot(mtcars,aes(x=mpg)) + geom_histogram(binwidth=5) + theme_economist()

你有什么建议吗?

提前致谢。

【问题讨论】:

  • 为什么不ggplot(mtcars,aes(x=mpg)) + geom_histogram(binwidth=5) + theme_economist() + theme(axis.text.x = element_text(angle = 90))
  • your_theme <- function(...) theme_economist() + theme(...)。然后做your_plot + your_theme(axis.text.x = element_text(angle = 90))

标签: r ggplot2


【解决方案1】:

您似乎有两个选择。

选项 1

创建一个将主题元素添加到原始函数的新函数,然后在ggplot() 序列中使用该函数。在这里,theme_new 是带有 90 度刻度标签的新主题。请注意,当您使用 theme_new 时,请省略括号(即 theme_new 而不是 theme_new())。

library(ggplot2)
library(ggthemes)

df <- data.frame(
    x = rnorm(1000, 500, 100),
    y = rnorm(1000, 500, 100)
)

theme_new <- theme_economist() + theme(
    axis.text.x = element_text(angle = 45)
)

ggplot(df, aes(x=x, y=y)) +
    geom_point() +
    theme_new

选项 2:

通过复制ggthemes 库中的theme_economist() 定义来创建您自己的主题,并将代码替换为您想要的设计元素。如果要查看定义,可以查看here

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多