Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additions to theme_tufte(). And more actively suggesting the use of extrafont #33

Closed
dannguyen opened this issue Jun 2, 2015 · 5 comments

Comments

@dannguyen
Copy link

Proposed changes

  1. Make Gill Sans the base font
  2. Add axis-lines; Tufte doesn't necessarily advocate the removal of all lines (see References
  3. Add some vertical spacing in axis titles

I realize #1 is the most problematic. Given that extrafont is a fairly stable library at this point, and is a suggested requirement for ggthemes, maybe it's worth it to include a helper function that throws a warning/suggestion if extrafont isn't installed. And if it is installed, then use the choose_font() functionality. The assumption here is that many users of ggthemes are already looking for convenience of packaged design, and typography is an essential enough component that it's worth actively suggesting the dependency of extrafont.

The main drawback, besides the extra dependency, is the user having to then run font_import(). Perhaps that could be implemented as a helper function too, to wrap up the library install and font import at some point during the first use of ggthemes...

Set up the data and libraries

This is setup just to run my example, not necessarily for the end-user:

library(dplyr)
library(httr)
library(extrafont)
font_import() # if first time running extrafont

library(ggthemes)
# via http://zevross.com/blog/2014/08/04/beautiful-plotting-in-r-a-ggplot2-cheatsheet-3/
DATA_URL <- 'http://zevross.com/blog/wp-content/uploads/2014/08/chicago-nmmaps.csv'
dat <- content(GET(DATA_URL))

Credit: Data and examples modified from Zevross's excellent ggplot2 cheat sheet

Set up the plot

tuftePal <- c("#990033", "#367a37", "#cc6600", "#333333")
g <- ggplot(dat, aes(o3, temp, color = factor(season))) +
     geom_point() +
     labs(x = "Ozone",  y = "Temperature (F)") +
     scale_colour_manual(name = "Seasons", values = tuftePal)

Current theme_tufte()

g + theme_tufte() + ggtitle('Current theme_tufte()')

current theme_tufte()

Adding extrafont library check

is.extrafont.installed <- function(){
  if(is.element("extrafont", installed.packages()[,1])){
    library(extrafont)
    # probably need something here to run font_import()
    return(T)
  }else{
    warning("Library extrafont installed; using system sans/serif libraries as fallback fonts. 
    To enable full font support, run: 
      install.packages('extrafont') 
      font_import()")
    return(F)
  }
}

Defining theme_tufte_revised()

# set font
base_font_family_tufte <- function(){
  if(is.extrafont.installed()){
    library(extrafont)
    tuftefont <- choose_font(c("Gill Sans MT", "Gill Sans", "GillSans", "Verdana", "serif"), quiet = FALSE)  
  }else{
    tuftefont <- "serif"
  }
  return(tuftefont)
}

theme_tufte_revised <- function(base_size = 11, base_family = base_font_family_tufte(), ticks = TRUE) {

  ret <- theme_bw(base_family = base_family, base_size = base_size) + 
    theme(
          axis.line = element_line(color = 'black'),
          axis.title.x = element_text(vjust = -0.3), 
          axis.title.y = element_text(vjust = 0.8),
          legend.background = element_blank(), 
          legend.key = element_blank(), 
          legend.title = element_text(face="plain"),
          panel.background = element_blank(), 
          panel.border = element_blank(),
          panel.grid = element_blank(),
          plot.background = element_blank(),
          strip.background = element_blank()
    )

  if (!ticks) {
    ret <- ret + theme(axis.ticks = element_blank())
  }

  ret
} 
g + theme_tufte_revised() + ggtitle('Revised theme_tufte()')

theme_tufte_revised()

References

The Visual Display of Quantitative Information:

"Redesign of the Bar Chart/Histogram", p.128

Still, a thin baseline looks good

img

What's your font? (RIP aaronsw):

The display font is Gill Sans, a classic and elegant sans serif font. Gill designed several excellent fonts, which are widely available. In addition to the display font at this website, I use Gill sans a few places in Enivisioning Information and in Visual Explantions.

Also: Chartjunk

@jrnold
Copy link
Owner

jrnold commented Jun 6, 2015

Regarding the font choice, if I recall, the plots in all the books use serif labels (Bembo), and Gill Sans is reserved for section headings?

Making extrafont a requirement is a good idea. I'd rather handle dependencies in the package requirements than conditional statements within the code. Originally my thinking was that fonts are so heavily dependent on what the user has available it wasn't worth it to handle those cases, and it is easy to just set it with font_family= arguments when needed. But that was before I contributed choose_font to extrafont. So now it may make sense to make extrafont a requirement, and set appropriate font_family defaults for each theme using choose_font.

However, I don't think I'll add axes to the theme. The combination of theme_tufte + geom_rangeframe covers the scatterplot examples given in Visual Display of Quantative Information_. And think once you add axes like the ones in your example, the theme isn't much different than theme_classic included in _ggplot2* (with a change of default font). In general, I think the themes don't need to cover all possible cases. They only need to get the look of the plot 90% there, leaving the user to write the final touches to adapt them to their cases.

Looking at the bar chart example reminded me that there is something that is missing: a geom based off of geom_hline or geom_vline that acts like axis lines but can cover other geoms and produce the effect in the bar plot example.

@baptiste
Copy link

baptiste commented Jun 7, 2015

Speaking of fonts, did you see https://github.com/yixuan/showtext/ ? It looks like it might be a good alternative to extrafont.

@shrektan
Copy link

  • I think showtext package seems a better alternative.
  • I might suggest the geom_rangeframe is the default choice of axis. I really love it.

@jrnold
Copy link
Owner

jrnold commented Jul 11, 2016

This issues be dormant for a while, and I don't see myself implementing it in the near future. I'll largely leave it to users to choose the appropriate font with base_family and use whatever package they like for rendering fonts.

@jrnold jrnold closed this as completed Jul 11, 2016
@aminadibi
Copy link

Wish you'd implement both suggestions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants