簡體   English   中英

復制和修改默認主題

[英]Copying and modifying a default theme

我想為ggplot創建一個基於theme_bw()的新主題。

我想以下步驟是必要的(在偽代碼中):

  1. 制作theme_bw()的副本: theme_new() <- theme_bw()
  2. 修改副本: theme_update(axis.title.x = theme_text(family = base_family, size = base_size, vjust = 0.5))

任何關於如何實現這一點的建議將非常感謝!


編輯: @Andrie,我根據自己的需要修改了你的答案:

theme_new <- theme_set(theme_bw())
theme_new <- theme_update(axis.title.x = theme_text(family = base_family, size = base_size, vjust = 0.5))

但是,我收到以下錯誤:

ggplot(mtcars, aes(factor(cyl))) + geom_bar()

匹配錯誤(gparname,names(gpars)):找不到對象'base_size'


編輯: 2017年10月31日,@ Andrie提供的答案工作得很好。 R版本3.4.1,ggplot2_2.2.1

您的代碼只需要進行一些小的更改(主要是刪除括號並在正確的位置添加括號)

theme_new <- theme_set(theme_bw())

theme_new <- theme_update(
    panel.background = element_rect(fill="lightblue"))

ggplot(mtcars, aes(factor(cyl))) + geom_bar()

在此輸入圖像描述


參考:

維基建議使用modifyList執行此操作的一種方法,

theme_new <- function (base_size = 12, base_family = "", ...){
 modifyList (theme_bw (base_size = base_size, base_family = base_family),
          list (axis.title.x = theme_text(family = base_family, 
                size = base_size, vjust = 0.5)))
}

對於較新的版本,基於此處的文章

txt <- element_text(size = 14, colour = "black", face = "plain")
bold_txt <- element_text(size = 14, colour = "black", face = "bold")

theme_whatever <- function(base_size = 14, base_family = "Palatino")
{
  theme_bw(base_size = base_size, base_family = base_family) +
  theme(
    legend.key = element_blank(), 
    strip.background = element_blank(), 

    text = txt, 
    plot.title = txt, 

    axis.title = txt, 
    axis.text = txt, 

    legend.title = bold_txt, 
    legend.text = txt ) 
}

請注意,我使用txttxt_bold來避免txt_bold寫入相同的內容。

試試這個:

### Set up a blank theme
theme_none <- theme(
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  panel.background = element_blank(),
  axis.title.x = element_text(colour=NA),
  axis.title.y = element_blank(),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.line = element_blank()
  #axis.ticks.length = element_blank()
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM