How can we make xkcd style graphs?

前端 未结 7 1425
心在旅途
心在旅途 2020-11-28 00:12

Apparently, folk have figured out how to make xkcd style graphs in Mathematica and in LaTeX. Can we do it in R? Ggplot2-ers? A geom_xkcd and/or theme_xkcd?

I guess i

相关标签:
7条回答
  • 2020-11-28 00:30

    Here's an attempt at the fonts, based on links from the xkcd forums and the extrafont package:

    As noted above there is a forum discussion about fonts on the xkcd site: I grabbed the first one I could find, there may be other (better?) options (@jebyrnes posts another source for possible fonts in comments above -- the TTF file is here; someone reported a 404 error for that source, you might alternatively try here or here, substituting those URLs appropriately for xkcdFontURL below; you may have to work a bit harder to retrieve the Github-posted links)

       xkcdFontURL <- "http://simonsoftware.se/other/xkcd.ttf"
       download.file(xkcdFontURL,dest="xkcd.ttf",mode="wb")
    

    (This is for quickie, one-off use: for regular use you should put it in some standard system font directory.)

       library(extrafont)
    

    The most useful information about fonts was on the extrafont github site -- this is taken from there

    font_import(".")   ## because we downloaded to working directory
    loadfonts()
    

    Example taken more or less verbatim from the github site:

    library(ggplot2)
    p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
      ggtitle("Fuel Efficiency of 32 Cars") +
      xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
      theme(text=element_text(size=16, family="xkcd"))
    
    ggsave("xkcd_ggplot.pdf", plot=p,  width=4, height=4)
    ## needed for Windows:
    ##   Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.05/bin/gswin32c.exe")
    embed_fonts("xkcd_ggplot.pdf")
    

    enter image description here

    0 讨论(0)
  • 2020-11-28 00:31

    Thinking along the same line as some of the other answers, I've "un-ggplotted" the chart and also added on the flexibility of the x-axis label locations (which seems to be common in xkcd) and an arbitrary label on the chart.

    Note that I had a few issues with loading the Humor Sans font and manually downloaded it to working directory.

    enter image description here

    And the code...

    library(ggplot2)
    library(extrafont)
    
    ### Already have read in fonts (see previous answer on how to do this)
    loadfonts()
    
    ### Set up the trial dataset 
    data <- NULL
    data$x <- seq(1, 10, 0.1)
    data$y1 <- sin(data$x)
    data$y2 <- cos(data$x)
    data$xaxis <- -1.5
    
    data <- as.data.frame(data)
    
    ### XKCD theme
    theme_xkcd <- theme(
        panel.background = element_rect(fill="white"), 
        axis.ticks = element_line(colour=NA),
        panel.grid = element_line(colour="white"),
        axis.text.y = element_text(colour=NA), 
        axis.text.x = element_text(colour="black"),
        text = element_text(size=16, family="Humor Sans")
        )
    
     ### Plot the chart
     p <- ggplot(data=data, aes(x=x, y=y1))+
          geom_line(aes(y=y2), position="jitter")+
          geom_line(colour="white", size=3, position="jitter")+
          geom_line(colour="red", size=1, position="jitter")+
          geom_text(family="Humor Sans", x=6, y=-1.2, label="A SIN AND COS CURVE")+
          geom_line(aes(y=xaxis), position = position_jitter(h = 0.005), colour="black")+
          scale_x_continuous(breaks=c(2, 5, 6, 9), 
          labels = c("YARD", "STEPS", "DOOR", "INSIDE"))+labs(x="", y="")+
          theme_xkcd
    
    ggsave("xkcd_ggplot.jpg", plot=p, width=8, height=5)
    
    0 讨论(0)
  • 2020-11-28 00:36

    Here is my take on the lines with ggplot2 using some of the code from above:

    ggplot()+geom_line(aes(x=seq(0,1,length.out=1000),y=sin(x)),position=position_jitter(width=0.02),lwd=1.5,col="white")+
      geom_line(aes(x=seq(0,1,length.out=1000),y=sin(x)),position=position_jitter(width=0.004),lwd=1.4,col="red")+
      geom_line(aes(x=seq(0,1,length.out=1000),y=cos(x)),position=position_jitter(width=0.02),lwd=1.5,col="white")+
      geom_line(aes(x=seq(0,1,length.out=1000),y=cos(x)),position=position_jitter(width=0.004),lwd=1.4,col="blue")+
      theme_bw()+theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())
    

    Not sure how to replace the axes, but could use the same approach with jitter. Then it's a matter of importing the font from XKCD and layering with geom_text.

    0 讨论(0)
  • 2020-11-28 00:40

    You might want to consider the following package:

    Package xkcd: Plotting ggplot2 graphics in a XKCD style.

    library(xkcd)
    vignette("xkcd-intro")
    

    Some examples (Scatterplots, Bar Charts):

    • Scatterplot:

    Scatterplot

    • Bar Chart:

    Bar Chart

    0 讨论(0)
  • 2020-11-28 00:44

    Basic line-drawing function:

    xkcd_line <- function(x, y, color) {
      len <- length(x);
      rg <- par("usr");
      yjitter <- (rg[4] - rg[3]) / 1000;
      xjitter <- (rg[2] - rg[1]) / 1000;
      x_mod <- x + rnorm(len) * xjitter;
      y_mod <- y + rnorm(len) * yjitter;
      lines(x_mod, y_mod, col='white', lwd=10);
      lines(x_mod, y_mod, col=color, lwd=5);
    }
    

    Basic axis:

    xkcd_axis <- function() {
      rg <- par("usr");
      yaxis <- 1:100 / 100 * (rg[4] - rg[3]) + rg[3];
      xaxis <- 1:100 / 100 * (rg[2] - rg[1]) + rg[1];
      xkcd_line(1:100 * 0 + rg[1] + (rg[2]-rg[1])/100, yaxis,'black')
      xkcd_line(xaxis, 1:100 * 0 + rg[3] + (rg[4]-rg[3])/100, 'black')
    }
    

    And sample code:

    data <- data.frame(x=1:100)
    data$one <- exp(-((data$x - 50)/10)^2)
    data$two <- sin(data$x/10)
    plot.new()
    plot.window(
        c(min(data$x),max(data$x)),
        c(min(c(data$one,data$two)),max(c(data$one,data$two))))
    xkcd_axis()
    xkcd_line(data$x, data$one, 'red')
    xkcd_line(data$x, data$two, 'blue')
    

    Produces:

    Example chart

    0 讨论(0)
  • 2020-11-28 00:48

    This is a very, very rough start and only covers (partially) the hand-drawn look and feel of the lines. It would take a little bit of work to automate this but adding some AR(1) noise to the response function could make it seem slightly hand drawn

    set.seed(551)
    x <- seq(0, 1, length.out = 1000)
    y <- sin(x)
    
    imperfect <- arima.sim(n = length(y), model = list(ar = c(.9999)))
    imperfect <- scale(imperfect)
    z <- y + imperfect*.005
    plot(x, z, type = "l", col = "blue", lwd = 2)
    
    0 讨论(0)
提交回复
热议问题