[R] Graphs for scientific publication ?

David Winsemius dwinsemius at comcast.net
Mon May 4 23:10:15 CEST 2015


On May 4, 2015, at 3:13 AM, Jeremy Clark wrote:

> Dear All,
> 
> Many thanks for your very comprehensive replies. Here I provide some
> coding which on my system has the following effects:
> 1) The italic R is not rendered by CairoX11, but is rendered by quartz.
> 2) Both geom_smooth and geom_abline here give stepped lines (I've
> realised the angle of the line makes quite a difference to this). I
> presume that these are not "anti-aliased" - so I was hoping that Cairo
> would change this. Unfortunately the command Cairo() does not open any
> device, and the CairoX11 device gives similar lines to that from
> quartz.
> 3) As I must turn off general clipping (because I need to add some
> text which overlaps the plot edge) it would be useful to be able to
> clip particular lines to the plot edge - although this is not a
> catastrophe as I can create a new truncated dataframe and plot the
> lines from this.
> 
> My system is MacBook Air, with all Xcode recently updated including
> IOS 8.2, OS X 10.10, Xcode 6.2. I previously, and fairly recently,
> installed X11 (and Xquartz), and also updated R and R Cairo, but none
> of this has affected the above behaviour.
> 
> Any advice gratefully received.
> 
> Yours sincerely,
> 
> Jeremy Clark
> 
> library(ggplot2)
> library(grid)
> library(Cairo)
> 
> theme_jack <- function (base_size = 16, base_family = "") {
>    theme_classic(base_size = base_size, base_family = base_family) %+replace%
>        theme(
>            plot.title = element_text(size=15, vjust=3),
>            axis.text = element_text(colour = "black", family="Times",
> face=c('bold'), size = 18),
>            axis.title.x = element_text(colour = "black",
> family="Times", face=c('bold'),   vjust             = -1,    size =
> 20),
>            axis.title.y = element_text(colour = "black",
> family="Times", angle=90,      face=c('bold'), vjust= 2, size = 20),
>            panel.background = element_rect(fill="white"),
>            panel.grid.minor = element_blank(),
>            panel.grid.major = element_blank(),
>            plot.background = element_rect(fill="white"),
>            panel.border = element_blank(),
>            panel.background = element_blank(),
>            plot.margin=unit(c(1,1.5,1.3,1.3),"cm")
>    )
> }
> theme_set(theme_jack())
> 
> DataX <- seq(1, 40, by = 1)
> DataY <- seq(1, 40, by = 1)
> Datadf <- data.frame(DataX, DataY)
> 
> ## replace "quartz" with "CairoX11" or "X11" for various effects:
> 
> quartz(width = 6 , height = 6) ## quartz renders the italic R correctly
> p1 <- ggplot() + ggtitle("Title") + coord_cartesian(xlim = c(1, 40),
> ylim = c(0, 40)) + scale_y_continuous(breaks = c(0, 10, 20, 30),
> labels = c("0", "10", "20", "30"), expand = c(0, 0)) + ylab("Y-axis")
> + scale_x_continuous(breaks = c(10, 20, 30, 40), expand = c(0, 0)) +
> geom_point(data = Datadf, aes(x = DataX, y = DataY)) + xlab("X-axis")
> 

> predy <- as.integer(c(38, 25, 20, 14, 8))
> predx <- as.integer(c(20, 21, 22, 24, 25))
> datapreddf <- as.data.frame(predx, predy)
> myplm <- lm(predy ~ predx, data = datapreddf)
> lmxrange <- data.frame(predx = seq(from = 20, to = 30, by = 0.01))
> lmyrange <- predict.lm(myplm, newdata <- lmxrange)
> lmdataframe <- data.frame(lmxrange, lmyrange)
> p2 <- p1 + geom_smooth(data = lmdataframe, aes(x = predx, y =
> lmyrange), method=lm, se=FALSE, color = "black") +
> geom_abline(aes(intercept = as.vector(coefficients(myplm)[1]),
> slope=as.vector(coefficients(myplm)[2]+2)), data=lmdataframe) ## both
> give stepped lines in both quartz and CairoX11
> 
> rsquaredlm = NULL
> rsquaredlm[[6]] <- 3 ## false value
> listr2 <- list(r2 = rsquaredlm[[6]])
> eq1 <- substitute(italic(R)^2 == r2, listr2)
> eqstr1 <- as.character(as.expression(eq1))
> q3 <- p2 +  annotate(geom = "text", x = 20, y = 30, label = eqstr1,
> parse = TRUE, vjust = 1)
> gt <- ggplot_gtable(ggplot_build(q3))
> 
> gt$layout$clip[gt$layout$name=="panel"] <- "off"

> ## (necessary toallow additional text
> ## overlap - not shown) - clipping of lines can be done with other
#   coding - although it
> 
> ## would be nice to be able to do this more efficiently
> grid.draw(gt)
> 
> ## _____________________________________________
> 
> Cairo() ## doesn't open any device

I would not expect it to. The default for the file argument to Cairo() is "". I get an error when I try this on a MacPro running 0sx 10.7.5:

> Cairo(file="test.png")
Fontconfig error: "/Library/Frameworks/R.framework/Resources/fontconfig/fonts/conf.d/10-scale-bitmap-fonts.conf", line 70: non-double matrix element

At any rate one would not generally attempt to smooth out jaggies with the default .png setting for Cairo. One would use CairoPDF or CairoSVG depending on the preferences of ones publisher. (I've never used CairoPDF(), since the ordinary pdf() device "just works".)

This does seem to be way too much code for demonstration of difficulties with getting proper output from a vector graphics device. The onscreen version of quartz has limited resolution but the "Save as..." output should be pdf.

You need to realize that there are several different fonts being used by these devices:

> names( quartzFonts())
[1] "serif" "sans"  "mono" 
> names( CairoFonts())
NULL
>  CairoFonts()
> #  I get nothing ... consistent with the error report that suggests I have not followed the advice in ?CairoFonts: "This function is only available when the cairo graphics library is configured with FreeType and Fontcofig support."

Since I've never needed it, I suggest you try to use pdf() for vector output.

-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list