Skip to content

Instantly share code, notes, and snippets.

@emhart
Created October 17, 2012 22:41
Show Gist options
  • Save emhart/3908799 to your computer and use it in GitHub Desktop.
Save emhart/3908799 to your computer and use it in GitHub Desktop.
ggplot options
### Set-up theme
theme_popblk = function(size=12) {
o = list(
#axis.line=theme_blank(),
axis.text.x=theme_text(size=size*.9),
axis.text.y=theme_text(size=size*.9),
# axis.text.y=theme_blank(),
# axis.ticks.y=theme_blank(),
# axis.ticks=theme_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),
axis.title.x=theme_text(size=size*1),
axis.title.y=theme_text(size=size*1,angle=90),
# legend.background=theme_rect(fill="white", colour=NA),
legend.key=theme_rect(colour="white"),
legend.key.size=unit(1.2, "lines"),
# legend.position="right",
legend.text=theme_text(size=size),
legend.title=theme_text(size=size, face="bold",
hjust=0),
panel.background=theme_rect(fill="white",colour="black"),
# panel.border=theme_blank(),
# panel.grid.major=theme_line(colour="gray70"),
panel.grid.major=theme_blank(),
panel.grid.minor=theme_blank(),
panel.margin=unit(0, "lines"),
plot.background=theme_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=theme_text(size=size*1.2),
strip.background=theme_rect(fill="grey90",
colour="black"),
strip.text.x=theme_text(size=size*1) )
# strip.text.y=theme_text(size=size*1, angle=-90))
return(structure(o, class="options")) }
###### Minimal example taken from ggplot docs #####
df <- data.frame(
trt = factor(c(1, 1, 2, 2)),
resp = c(1, 5, 3, 4),
group = factor(c(1, 2, 1, 2)),
se = c(0.1, 0.3, 0.3, 0.2)
)
df2 <- df[c(1,3),]
p <- ggplot(df, aes(fill=group, y=resp, x=trt))
p + geom_bar(position="dodge", stat="identity")
#### By adding theme, background color should be stripped, fonts changed, etc... worked before ggplot update
p + geom_bar(position="dodge", stat="identity") + theme_popblk()
### however the following error is generated ####
# > p + geom_bar(position="dodge", stat="identity") + theme_popblk()
# Error: Don't know how to add o to a plot
# In addition: There were 14 warnings (use warnings() to see them)
@karthik
Copy link

karthik commented Oct 17, 2012

### Set-up theme
theme_popblk <- function(size = 12) { 
  theme(axis.text.x = element_text(size = size*.9),
    axis.text.y = element_text(size = size*.9),
    axis.ticks.length = grid::unit(0.3, "cm"), 
    axis.ticks.margin = grid::unit(0.5, "cm"), 
    axis.title.x = element_text(size = size*1), 
    axis.title.y = element_text(size = size*1,angle = 90), 
    legend.key = element_rect(colour = "white"), 
    legend.key.size = grid::unit(1.2, "cm"), 
    legend.text = element_text(size = size), 
    legend.title = element_text(size = size, face = "bold", 
                            hjust = 0), 
    panel.background = element_rect(fill = "white",colour = "black"), 
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), 
    panel.margin = grid::unit(0, "cm"), 
    plot.background = element_blank(), 
    plot.margin = grid::unit(c(1, 1, 0.5, 0.5), "cm"), 
    plot.title = element_text(size = size*1.2), 
    strip.background = element_rect(fill = "grey90", 
                                colour = "black"), 
    strip.text.x = element_text(size = size*1))
 }


###### Minimal example taken from ggplot docs #####


df <- data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  se = c(0.1, 0.3, 0.3, 0.2)
)
df2 <- df[c(1,3),]

p <- ggplot(df, aes(fill=group, y=resp, x=trt))
p + geom_bar(position="dodge", stat="identity")

#### By adding theme, background color should be stripped, fonts changed, etc... worked before ggplot update 
p + geom_bar(position="dodge", stat="identity") + theme_popblk()

### however the following error is generated ####
# > p + geom_bar(position="dodge", stat="identity") + theme_popblk()
# Error: Don't know how to add o to a plot
# In addition: There were 14 warnings (use warnings() to see them)

@sckott
Copy link

sckott commented Oct 17, 2012

Looks like you can modify themes easily too by %+replace%, e.g., in hadley's documentation:

theme_grey(base_size = base_size, base_family = base_family) %+replace% 
        theme(axis.text = element_text(size = rel(0.8)), axis.ticks = element_line(colour = "black"), 
            legend.key = element_rect(colour = "grey80"), panel.background = element_rect(fill = "white", 
                colour = NA), panel.border = element_rect(fill = NA, 
                colour = "grey50"), panel.grid.major = element_line(colour = "grey90", 
                size = 0.2), panel.grid.minor = element_line(colour = "grey98", 
                size = 0.5), strip.background = element_rect(fill = "grey80", 
                colour = "grey50"), strip.background = element_rect(fill = "grey80", 
                colour = "grey50"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment