I would like to create a new theme for ggplot
that is based on theme_bw()
.
I imagine the following steps are necessary (in pseudocode):
Try like this one:
### 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()
)
For the newer versions, based on the article here
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 )
}
Note that I use txt
and txt_bold
to avoid writing same stuff over and over again.
Your code just needs a few small changes to work (mainly removing brackets and adding brackets at the right places)
theme_new <- theme_set(theme_bw())
theme_new <- theme_update(
panel.background = element_rect(fill="lightblue"))
ggplot(mtcars, aes(factor(cyl))) + geom_bar()
Reference:
the wiki suggests one way to do this using 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)))
}