Graphs for scientific publication ?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Graphs for scientific publication ?

Abiologist
Dear All,

First of all, many thanks to all R contributors for a fantastic
program, and especially to Hadley Wickham for creating ggplot2. The
following is intended to be a warning that, if the apparently
superficial problems described are not sorted out, R could well find
itself being superceded. The reason is that a new user wants to draw a
graph, and perhaps publish in a scientific journal a graph created
using R, well before wanting to do a complex regression (and the
latter is relatively easy). So here goes:

1) The saga of the straight line. I implemented a geom_abline - it
looked superb. Unfortunately I had to disable clip to allow text - now
my abline looked ridiculous. My search found plotrix: ablineclip -
fantastic I thought - but it applies to plot and not geom_plot. I
switched to geom_segment - the rendering looked trash. I switched to
geom_smooth - should work but as I don't know the x values beforehand
I'll have to clip a new dataframe - it that a hassle ? - Yes it is !

            So my general question is - why isn't ggplot2 already part
of R base - or at least if someone is to create useful packages for
plot - perhaps a subtle hint could be made that they should also apply
to ggplot2 (and perhaps to lattice ?? - also personally I would scrap
qplot as an unnecessary distraction which is not easier to implement
than ggplot). In general duplication of packages for plot and ggplot
doesn't seem like a good idea.


2) The saga of the italic letter. I found, to my dismay, that to
insert an italic letter into my plot I had to learn a whole new
language called plotmath - which wouldn't accept normal R coding, and
didn't even have normal control functions such as /n for a new line.
This is ridiculous (and I'm not sure how plotmath managed to get into
R base).

            So my question is, when is plotmath going to have a
complete overhaul to allow eg. "," instead of, or as well as, ~,~, and
normal control functions such as \n ?

3) A related question to (2) is: where is geom_textbox ?

4) Where are examples with scientific graph defaults ?  (meaning a
two-axis graph which is publishable - I will post my own after this is
published in a years time, but as suggested above, while the graph
looks good the implementation of this is not pretty).

Having said that - good luck with implementation - and many thanks for
all your hard work !

Yours sincerely,

Abiologist

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Graphs for scientific publication ?

Bert Gunter
Jeremy:

I suggest you have a look at the latest edition of Paul Murrell's
book, "R Graphics", as you seem to be unaware that ggplot2 (as well as
a 3rd graphics paradigm, the lattice package) and base graphics are
built on 2 different and incompatible graphics engines.

Obviously, you are entitled to your opinions and graphical
predilections vary, but I do not think R-Help is a good venue for
these sorts of discussions. The R-devel list might be a better place
to discuss such matters.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Thu, Apr 30, 2015 at 5:05 AM, Jeremy Clark <[hidden email]> wrote:

> Dear All,
>
> First of all, many thanks to all R contributors for a fantastic
> program, and especially to Hadley Wickham for creating ggplot2. The
> following is intended to be a warning that, if the apparently
> superficial problems described are not sorted out, R could well find
> itself being superceded. The reason is that a new user wants to draw a
> graph, and perhaps publish in a scientific journal a graph created
> using R, well before wanting to do a complex regression (and the
> latter is relatively easy). So here goes:
>
> 1) The saga of the straight line. I implemented a geom_abline - it
> looked superb. Unfortunately I had to disable clip to allow text - now
> my abline looked ridiculous. My search found plotrix: ablineclip -
> fantastic I thought - but it applies to plot and not geom_plot. I
> switched to geom_segment - the rendering looked trash. I switched to
> geom_smooth - should work but as I don't know the x values beforehand
> I'll have to clip a new dataframe - it that a hassle ? - Yes it is !
>
>             So my general question is - why isn't ggplot2 already part
> of R base - or at least if someone is to create useful packages for
> plot - perhaps a subtle hint could be made that they should also apply
> to ggplot2 (and perhaps to lattice ?? - also personally I would scrap
> qplot as an unnecessary distraction which is not easier to implement
> than ggplot). In general duplication of packages for plot and ggplot
> doesn't seem like a good idea.
>
>
> 2) The saga of the italic letter. I found, to my dismay, that to
> insert an italic letter into my plot I had to learn a whole new
> language called plotmath - which wouldn't accept normal R coding, and
> didn't even have normal control functions such as /n for a new line.
> This is ridiculous (and I'm not sure how plotmath managed to get into
> R base).
>
>             So my question is, when is plotmath going to have a
> complete overhaul to allow eg. "," instead of, or as well as, ~,~, and
> normal control functions such as \n ?
>
> 3) A related question to (2) is: where is geom_textbox ?
>
> 4) Where are examples with scientific graph defaults ?  (meaning a
> two-axis graph which is publishable - I will post my own after this is
> published in a years time, but as suggested above, while the graph
> looks good the implementation of this is not pretty).
>
> Having said that - good luck with implementation - and many thanks for
> all your hard work !
>
> Yours sincerely,
>
> Abiologist
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Graphs for scientific publication ?

David Carlson
More useful to the r-help list would be a reproducible example of the data you are using and a clear statement of what you are trying to accomplish. It is likely that all of your requirements can be easily met, but you spent most of your message talking about what you have tried without telling us where you want to end up. People on the list are familiar with base graphics, lattice graphics, and ggplot2. If you list your requirements clearly, you might end up with three solutions.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Bert Gunter
Sent: Thursday, April 30, 2015 1:41 PM
To: Jeremy Clark
Cc: [hidden email]
Subject: Re: [R] Graphs for scientific publication ?

Jeremy:

I suggest you have a look at the latest edition of Paul Murrell's
book, "R Graphics", as you seem to be unaware that ggplot2 (as well as
a 3rd graphics paradigm, the lattice package) and base graphics are
built on 2 different and incompatible graphics engines.

Obviously, you are entitled to your opinions and graphical
predilections vary, but I do not think R-Help is a good venue for
these sorts of discussions. The R-devel list might be a better place
to discuss such matters.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll




On Thu, Apr 30, 2015 at 5:05 AM, Jeremy Clark <[hidden email]> wrote:

> Dear All,
>
> First of all, many thanks to all R contributors for a fantastic
> program, and especially to Hadley Wickham for creating ggplot2. The
> following is intended to be a warning that, if the apparently
> superficial problems described are not sorted out, R could well find
> itself being superceded. The reason is that a new user wants to draw a
> graph, and perhaps publish in a scientific journal a graph created
> using R, well before wanting to do a complex regression (and the
> latter is relatively easy). So here goes:
>
> 1) The saga of the straight line. I implemented a geom_abline - it
> looked superb. Unfortunately I had to disable clip to allow text - now
> my abline looked ridiculous. My search found plotrix: ablineclip -
> fantastic I thought - but it applies to plot and not geom_plot. I
> switched to geom_segment - the rendering looked trash. I switched to
> geom_smooth - should work but as I don't know the x values beforehand
> I'll have to clip a new dataframe - it that a hassle ? - Yes it is !
>
>             So my general question is - why isn't ggplot2 already part
> of R base - or at least if someone is to create useful packages for
> plot - perhaps a subtle hint could be made that they should also apply
> to ggplot2 (and perhaps to lattice ?? - also personally I would scrap
> qplot as an unnecessary distraction which is not easier to implement
> than ggplot). In general duplication of packages for plot and ggplot
> doesn't seem like a good idea.
>
>
> 2) The saga of the italic letter. I found, to my dismay, that to
> insert an italic letter into my plot I had to learn a whole new
> language called plotmath - which wouldn't accept normal R coding, and
> didn't even have normal control functions such as /n for a new line.
> This is ridiculous (and I'm not sure how plotmath managed to get into
> R base).
>
>             So my question is, when is plotmath going to have a
> complete overhaul to allow eg. "," instead of, or as well as, ~,~, and
> normal control functions such as \n ?
>
> 3) A related question to (2) is: where is geom_textbox ?
>
> 4) Where are examples with scientific graph defaults ?  (meaning a
> two-axis graph which is publishable - I will post my own after this is
> published in a years time, but as suggested above, while the graph
> looks good the implementation of this is not pretty).
>
> Having said that - good luck with implementation - and many thanks for
> all your hard work !
>
> Yours sincerely,
>
> Abiologist
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Graphs for scientific publication ?

Ista Zahn
In reply to this post by Abiologist
On Thu, Apr 30, 2015 at 8:05 AM, Jeremy Clark <[hidden email]> wrote:
> Dear All,
>
> First of all, many thanks to all R contributors for a fantastic
> program, and especially to Hadley Wickham for creating ggplot2. The
> following is intended to be a warning that, if the apparently
> superficial problems described are not sorted out, R could well find
> itself being superceded.

In my opinion that can and should happen, but my prediction is that R
has such a big lead in terms of available functionality and packages
that no one will catch up for at least a decade.

The reason is that a new user wants to draw a

> graph, and perhaps publish in a scientific journal a graph created
> using R, well before wanting to do a complex regression (and the
> latter is relatively easy). So here goes:
>
> 1) The saga of the straight line. I implemented a geom_abline - it
> looked superb. Unfortunately I had to disable clip to allow text - now
> my abline looked ridiculous. My search found plotrix: ablineclip -
> fantastic I thought - but it applies to plot and not geom_plot. I
> switched to geom_segment - the rendering looked trash. I switched to
> geom_smooth - should work but as I don't know the x values beforehand
> I'll have to clip a new dataframe - it that a hassle ? - Yes it is !

As others have mentioned we can probably help you if you give us a
reproducible example and a clear description of what you are trying to
accomplish. Absent that this just sounds like complaining for the sake
of it.

>
>             So my general question is - why isn't ggplot2 already part
> of R base

I think packages are added to the base distribution relatively
infrequently these days. Is

install.packages("ggplot2")

really an issue?

- or at least if someone is to create useful packages for
> plot - perhaps a subtle hint could be made that they should also apply
> to ggplot2 (and perhaps to lattice ??

I'm not understanding what you are trying to say here.

- also personally I would scrap
> qplot as an unnecessary distraction which is not easier to implement
> than ggplot).

ggplot2 is in maintenance mode, so it is unlikely that major changes
like that will be introduced.

 In general duplication of packages for plot and ggplot
> doesn't seem like a good idea.

I'm not sure what kind of duplication you are referring to here,
though in general I also wish there was less duplicated functionality
spread across various R packages.

>
>
> 2) The saga of the italic letter. I found, to my dismay, that to
> insert an italic letter into my plot I had to learn a whole new
> language called plotmath - which wouldn't accept normal R coding, and
> didn't even have normal control functions such as /n for a new line.
> This is ridiculous (and I'm not sure how plotmath managed to get into
> R base).

library(ggplot2)

d1 <- data.frame(x = 1, y = 1, t = "some text")
d2 <- d1
d2$x <- 2

ggplot(d1, aes(x = x, y = y, label = t)) +
  geom_text(hjust = 0, size = 10) +
  geom_text(data=d2, fontface="italic", hjust=1, size = 10)

Works for me.

>
>             So my question is, when is plotmath going to have a
> complete overhaul to allow eg. "," instead of, or as well as, ~,~, and
> normal control functions such as \n ?

Probably never (though you could do it yourself if you think it is
worth spending the time to improve it).

>
> 3) A related question to (2) is: where is geom_textbox ?

I don't think there is one. You could make one following the
documentation at
https://github.com/hadley/ggplot2/wiki/Creating-a-new-geom

>
> 4) Where are examples with scientific graph defaults ?  (meaning a
> two-axis graph which is publishable - I will post my own after this is
> published in a years time, but as suggested above, while the graph
> looks good the implementation of this is not pretty).

Lot's of people publish ggplot2 graphs, standards differ from field to
field and from journal to journal.
http://scholar.google.com/scholar?cites=14238124760782644329&as_sdt=40000005&sciodt=0,22&hl=en
will give you some examples. Beyond that I think you'll have to be
more specific about what exactly you want the graphs to look like.

>
> Having said that - good luck with implementation - and many thanks for
> all your hard work !
>
> Yours sincerely,
>
> Abiologist
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Graphs for scientific publication ?

Abiologist
In reply to this post by Abiologist
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 to
allow 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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Graphs for scientific publication ?

Ista Zahn
Hi Jeremy,

On Mon, May 4, 2015 at 6:13 AM, Jeremy Clark <[hidden email]>
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.

I don't have a Mac available, but it works as expected here on Linux. You
may wish to ask on the r-sig-mac mailing list.

> 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,

Are you sure about that? It opens a png device by default here. See ?Cairo
especially the type argument.

and the CairoX11 device gives similar lines to that from
> quartz.

Generally graphics that you wish to publish or otherwise disseminate will
be written to a png, pdf, or similar. How does

png("tst.png", width=5, height=5, units = "in", res=300)
q3
dev.off()

look?

> 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.

It would have been nice to have a reproducible example of what you are
trying to accomplish by turning off clipping. There may be an easier way,
but it's hard to say for sure without knowing what the goal is.

Best,
Ista

>
> 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 to
> allow 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
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Graphs for scientific publication ?

David Winsemius
In reply to this post by Abiologist

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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Graphs for scientific publication ?

Abiologist
This post has NOT been accepted by the mailing list yet.
In reply to this post by Ista Zahn

The coding I've settled on to save file without clipping is:

library(gridExtra)
gt <- ggplot_gtable(ggplot_build(q3))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
gt4 <- arrangeGrob(gt)
ggsave <- ggplot2::ggsave; body(ggsave) <- body(ggplot2::ggsave)[-2] ## from Baptiste
ggsave("gt.pdf", plot = gt4, width = 6, height = 6)
ggsave("gt.png", plot = gt4, width = 6, height = 6)

Part of the problem with plotmath is that as soon as paste is used the syntax needed is different eg. substitute and substitute(paste( do not accept the same syntax eg. == or "=".