can't print ggplot with Chinese characters to pdf files

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

can't print ggplot with Chinese characters to pdf files

miao
I install the Chinese font "Kaiti TC" on my mac, but I can't print the
figures to pdf file by "marrangeGrob" command, which is in the package
"gridExtra". Error message after I type "ggsave(......)" (last line of the
program):

"Saving 7.47 x 5.15 in image
Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  :
  invalid font type
In addition: There were 50 or more warnings (use warnings() to see the
first 50)"

How can I install the font so that I can see the figures on the pdf files?
Some information that may be useful:
(1) If I use the English ggtitle, there is no problem at all. The pdf file
present the figures perfectly.
(2) If I use Chinese title, I can still see the figures p1 and p2 on
Studio, but I can't see it at the pdf file produced by ggsave. It gives an
error message



rm(list=ls())
library(ggplot2)
library(gridExtra)
df1<-data.frame(x=1:2, y=3:4, z=5:6)
#p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test1")
#p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test2")
p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("測試")  #Chinese title
p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("中文")  #Chinese title
p1<-p1+theme(text = element_text(family = "Kaiti TC"))
p2<-p2+theme(text = element_text(family = "Kaiti TC"))

p<-array(list(NA), dim=2)
p[[1]]<-p1
p[[2]]<-p2
p_series <- lapply(1:(length(p)), function(.x) p[.x][[1]])
m2 <- marrangeGrob(p_series, nrow=2, ncol=1)
ggsave("test_plot_chinese.pdf", m2)

        [[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: [FORGED] can't print ggplot with Chinese characters to pdf files

Paul Murrell-2
Hi

Instead of ...

ggsave("test_plot_chinese.pdf", m2)

... try ...

cairo_pdf("test_plot_chinese.pdf")
print(m2)
dev.off()

Paul

On 13/10/17 02:12, John wrote:

> I install the Chinese font "Kaiti TC" on my mac, but I can't print the
> figures to pdf file by "marrangeGrob" command, which is in the package
> "gridExtra". Error message after I type "ggsave(......)" (last line of the
> program):
>
> "Saving 7.47 x 5.15 in image
> Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  :
>    invalid font type
> In addition: There were 50 or more warnings (use warnings() to see the
> first 50)"
>
> How can I install the font so that I can see the figures on the pdf files?
> Some information that may be useful:
> (1) If I use the English ggtitle, there is no problem at all. The pdf file
> present the figures perfectly.
> (2) If I use Chinese title, I can still see the figures p1 and p2 on
> Studio, but I can't see it at the pdf file produced by ggsave. It gives an
> error message
>
>
>
> rm(list=ls())
> library(ggplot2)
> library(gridExtra)
> df1<-data.frame(x=1:2, y=3:4, z=5:6)
> #p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test1")
> #p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test2")
> p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("測試")  #Chinese title
> p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("中文")  #Chinese title
> p1<-p1+theme(text = element_text(family = "Kaiti TC"))
> p2<-p2+theme(text = element_text(family = "Kaiti TC"))
>
> p<-array(list(NA), dim=2)
> p[[1]]<-p1
> p[[2]]<-p2
> p_series <- lapply(1:(length(p)), function(.x) p[.x][[1]])
> m2 <- marrangeGrob(p_series, nrow=2, ncol=1)
> ggsave("test_plot_chinese.pdf", m2)
>
> [[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.
>

--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
[hidden email]
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
[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: [FORGED] can't print ggplot with Chinese characters to pdf files

miao
Thanks, Paul. Following your solution,  I got this error message:

Warning message:
In cairo_pdf("test_plot_chinese.pdf") : failed to load cairo DLL

Is there anything else I need to install?

Thanks,

John

2017-10-12 19:24 GMT-07:00 Paul Murrell <[hidden email]>:

> Hi
>
> Instead of ...
>
> ggsave("test_plot_chinese.pdf", m2)
>
> ... try ...
>
> cairo_pdf("test_plot_chinese.pdf")
> print(m2)
> dev.off()
>
> Paul
>
>
> On 13/10/17 02:12, John wrote:
>
>> I install the Chinese font "Kaiti TC" on my mac, but I can't print the
>> figures to pdf file by "marrangeGrob" command, which is in the package
>> "gridExtra". Error message after I type "ggsave(......)" (last line of the
>> program):
>>
>> "Saving 7.47 x 5.15 in image
>> Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,
>> :
>>    invalid font type
>> In addition: There were 50 or more warnings (use warnings() to see the
>> first 50)"
>>
>> How can I install the font so that I can see the figures on the pdf files?
>> Some information that may be useful:
>> (1) If I use the English ggtitle, there is no problem at all. The pdf file
>> present the figures perfectly.
>> (2) If I use Chinese title, I can still see the figures p1 and p2 on
>> Studio, but I can't see it at the pdf file produced by ggsave. It gives an
>> error message
>>
>>
>>
>> rm(list=ls())
>> library(ggplot2)
>> library(gridExtra)
>> df1<-data.frame(x=1:2, y=3:4, z=5:6)
>> #p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test1")
>> #p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test2")
>> p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("測試")  #Chinese title
>> p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("中文")  #Chinese title
>> p1<-p1+theme(text = element_text(family = "Kaiti TC"))
>> p2<-p2+theme(text = element_text(family = "Kaiti TC"))
>>
>> p<-array(list(NA), dim=2)
>> p[[1]]<-p1
>> p[[2]]<-p2
>> p_series <- lapply(1:(length(p)), function(.x) p[.x][[1]])
>> m2 <- marrangeGrob(p_series, nrow=2, ncol=1)
>> ggsave("test_plot_chinese.pdf", m2)
>>
>>         [[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/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
> --
> Dr Paul Murrell
> Department of Statistics
> The University of Auckland
> Private Bag 92019
> Auckland
> New Zealand
> 64 9 3737599 x85392
> [hidden email]
> http://www.stat.auckland.ac.nz/~paul/
>

        [[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: [FORGED] can't print ggplot with Chinese characters to pdf files

Paul Murrell-2
Hi

By the looks of it, you need to install Cairo graphics ...

https://www.cairographics.org/download/

Paul

On 13/10/17 15:48, John wrote:

> Thanks, Paul. Following your solution,  I got this error message:
>
> Warning message:
> In cairo_pdf("test_plot_chinese.pdf") : failed to load cairo DLL
>
> Is there anything else I need to install?
>
> Thanks,
>
> John
>
> 2017-10-12 19:24 GMT-07:00 Paul Murrell <[hidden email]
> <mailto:[hidden email]>>:
>
>     Hi
>
>     Instead of ...
>
>     ggsave("test_plot_chinese.pdf", m2)
>
>     ... try ...
>
>     cairo_pdf("test_plot_chinese.pdf")
>     print(m2)
>     dev.off()
>
>     Paul
>
>
>     On 13/10/17 02:12, John wrote:
>
>         I install the Chinese font "Kaiti TC" on my mac, but I can't
>         print the
>         figures to pdf file by "marrangeGrob" command, which is in the
>         package
>         "gridExtra". Error message after I type "ggsave(......)" (last
>         line of the
>         program):
>
>         "Saving 7.47 x 5.15 in image
>         Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label),
>         x$x, x$y,  :
>             invalid font type
>         In addition: There were 50 or more warnings (use warnings() to
>         see the
>         first 50)"
>
>         How can I install the font so that I can see the figures on the
>         pdf files?
>         Some information that may be useful:
>         (1) If I use the English ggtitle, there is no problem at all.
>         The pdf file
>         present the figures perfectly.
>         (2) If I use Chinese title, I can still see the figures p1 and p2 on
>         Studio, but I can't see it at the pdf file produced by ggsave.
>         It gives an
>         error message
>
>
>
>         rm(list=ls())
>         library(ggplot2)
>         library(gridExtra)
>         df1<-data.frame(x=1:2, y=3:4, z=5:6)
>         #p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test1")
>         #p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test2")
>         p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("測試")
>         #Chinese title
>         p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("中文")
>         #Chinese title
>         p1<-p1+theme(text = element_text(family = "Kaiti TC"))
>         p2<-p2+theme(text = element_text(family = "Kaiti TC"))
>
>         p<-array(list(NA), dim=2)
>         p[[1]]<-p1
>         p[[2]]<-p2
>         p_series <- lapply(1:(length(p)), function(.x) p[.x][[1]])
>         m2 <- marrangeGrob(p_series, nrow=2, ncol=1)
>         ggsave("test_plot_chinese.pdf", m2)
>
>                  [[alternative HTML version deleted]]
>
>         ______________________________________________
>         [hidden email] <mailto:[hidden email]> mailing list
>         -- To UNSUBSCRIBE and more, see
>         https://stat.ethz.ch/mailman/listinfo/r-help
>         <https://stat.ethz.ch/mailman/listinfo/r-help>
>         PLEASE do read the posting guide
>         http://www.R-project.org/posting-guide.html
>         <http://www.R-project.org/posting-guide.html>
>         and provide commented, minimal, self-contained, reproducible code.
>
>
>     --
>     Dr Paul Murrell
>     Department of Statistics
>     The University of Auckland
>     Private Bag 92019
>     Auckland
>     New Zealand
>     64 9 3737599 x85392
>     [hidden email] <mailto:[hidden email]>
>     http://www.stat.auckland.ac.nz/~paul/
>     <http://www.stat.auckland.ac.nz/~paul/>
>
>

--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
[hidden email]
http://www.stat.auckland.ac.nz/~paul/

______________________________________________
[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: [FORGED] can't print ggplot with Chinese characters to pdf files

miao
Hi,


   Sorry to bother you with this question here.

   I tried to install Macports on my Mac OS Sierra, and type "sudo port
install cairo", but it did not respond. I haven't seen any file name or app
called Macports, but one file called "port" which is located at


loca/bin/port"


   How should I do it?


   Thanks,


John

*******


Last login: Sun Oct 15 02:52:49 on console

Johns-MacBook-Pro:~ john$ /opt/local/bin/port ; exit;

MacPorts 2.4.2

Entering shell mode... ("help" for help, "quit" to quit)

[Users/john] > sudo port install cairo

Error: Unrecognized action "port sudo"




2017-10-12 19:55 GMT-07:00 Paul Murrell <[hidden email]>:

> Hi
>
> By the looks of it, you need to install Cairo graphics ...
>
> https://www.cairographics.org/download/
>
> Paul
>
> On 13/10/17 15:48, John wrote:
>
>> Thanks, Paul. Following your solution,  I got this error message:
>>
>> Warning message:
>> In cairo_pdf("test_plot_chinese.pdf") : failed to load cairo DLL
>>
>> Is there anything else I need to install?
>>
>> Thanks,
>>
>> John
>>
>> 2017-10-12 19:24 GMT-07:00 Paul Murrell <[hidden email]
>> <mailto:[hidden email]>>:
>>
>>
>>     Hi
>>
>>     Instead of ...
>>
>>     ggsave("test_plot_chinese.pdf", m2)
>>
>>     ... try ...
>>
>>     cairo_pdf("test_plot_chinese.pdf")
>>     print(m2)
>>     dev.off()
>>
>>     Paul
>>
>>
>>     On 13/10/17 02:12, John wrote:
>>
>>         I install the Chinese font "Kaiti TC" on my mac, but I can't
>>         print the
>>         figures to pdf file by "marrangeGrob" command, which is in the
>>         package
>>         "gridExtra". Error message after I type "ggsave(......)" (last
>>         line of the
>>         program):
>>
>>         "Saving 7.47 x 5.15 in image
>>         Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label),
>>         x$x, x$y,  :
>>             invalid font type
>>         In addition: There were 50 or more warnings (use warnings() to
>>         see the
>>         first 50)"
>>
>>         How can I install the font so that I can see the figures on the
>>         pdf files?
>>         Some information that may be useful:
>>         (1) If I use the English ggtitle, there is no problem at all.
>>         The pdf file
>>         present the figures perfectly.
>>         (2) If I use Chinese title, I can still see the figures p1 and p2
>> on
>>         Studio, but I can't see it at the pdf file produced by ggsave.
>>         It gives an
>>         error message
>>
>>
>>
>>         rm(list=ls())
>>         library(ggplot2)
>>         library(gridExtra)
>>         df1<-data.frame(x=1:2, y=3:4, z=5:6)
>>         #p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test1")
>>         #p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test2")
>>         p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("測試")
>>  #Chinese title
>>         p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("中文")
>>  #Chinese title
>>         p1<-p1+theme(text = element_text(family = "Kaiti TC"))
>>         p2<-p2+theme(text = element_text(family = "Kaiti TC"))
>>
>>         p<-array(list(NA), dim=2)
>>         p[[1]]<-p1
>>         p[[2]]<-p2
>>         p_series <- lapply(1:(length(p)), function(.x) p[.x][[1]])
>>         m2 <- marrangeGrob(p_series, nrow=2, ncol=1)
>>         ggsave("test_plot_chinese.pdf", m2)
>>
>>                  [[alternative HTML version deleted]]
>>
>>         ______________________________________________
>>         [hidden email] <mailto:[hidden email]> mailing list
>>         -- To UNSUBSCRIBE and more, see
>>         https://stat.ethz.ch/mailman/listinfo/r-help
>>         <https://stat.ethz.ch/mailman/listinfo/r-help>
>>         PLEASE do read the posting guide
>>         http://www.R-project.org/posting-guide.html
>>         <http://www.R-project.org/posting-guide.html>
>>         and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>     --     Dr Paul Murrell
>>     Department of Statistics
>>     The University of Auckland
>>     Private Bag 92019
>>     Auckland
>>     New Zealand
>>     64 9 3737599 x85392
>>     [hidden email] <mailto:[hidden email]>
>>     http://www.stat.auckland.ac.nz/~paul/
>>     <http://www.stat.auckland.ac.nz/~paul/>
>>
>>
>>
> --
> Dr Paul Murrell
> Department of Statistics
> The University of Auckland
> Private Bag 92019
> Auckland
> New Zealand
> 64 9 3737599 x85392
> [hidden email]
> http://www.stat.auckland.ac.nz/~paul/
>

        [[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: [FORGED] can't print ggplot with Chinese characters to pdf files

Bert Gunter-2
Wrong list. Post to r-sig-mac instead.

Bert



On Oct 15, 2017 6:47 PM, "John" <[hidden email]> wrote:

> Hi,
>
>
>    Sorry to bother you with this question here.
>
>    I tried to install Macports on my Mac OS Sierra, and type "sudo port
> install cairo", but it did not respond. I haven't seen any file name or app
> called Macports, but one file called "port" which is located at
>
>
> loca/bin/port"
>
>
>    How should I do it?
>
>
>    Thanks,
>
>
> John
>
> *******
>
>
> Last login: Sun Oct 15 02:52:49 on console
>
> Johns-MacBook-Pro:~ john$ /opt/local/bin/port ; exit;
>
> MacPorts 2.4.2
>
> Entering shell mode... ("help" for help, "quit" to quit)
>
> [Users/john] > sudo port install cairo
>
> Error: Unrecognized action "port sudo"
>
>
>
>
> 2017-10-12 19:55 GMT-07:00 Paul Murrell <[hidden email]>:
>
> > Hi
> >
> > By the looks of it, you need to install Cairo graphics ...
> >
> > https://www.cairographics.org/download/
> >
> > Paul
> >
> > On 13/10/17 15:48, John wrote:
> >
> >> Thanks, Paul. Following your solution,  I got this error message:
> >>
> >> Warning message:
> >> In cairo_pdf("test_plot_chinese.pdf") : failed to load cairo DLL
> >>
> >> Is there anything else I need to install?
> >>
> >> Thanks,
> >>
> >> John
> >>
> >> 2017-10-12 19:24 GMT-07:00 Paul Murrell <[hidden email]
> >> <mailto:[hidden email]>>:
> >>
> >>
> >>     Hi
> >>
> >>     Instead of ...
> >>
> >>     ggsave("test_plot_chinese.pdf", m2)
> >>
> >>     ... try ...
> >>
> >>     cairo_pdf("test_plot_chinese.pdf")
> >>     print(m2)
> >>     dev.off()
> >>
> >>     Paul
> >>
> >>
> >>     On 13/10/17 02:12, John wrote:
> >>
> >>         I install the Chinese font "Kaiti TC" on my mac, but I can't
> >>         print the
> >>         figures to pdf file by "marrangeGrob" command, which is in the
> >>         package
> >>         "gridExtra". Error message after I type "ggsave(......)" (last
> >>         line of the
> >>         program):
> >>
> >>         "Saving 7.47 x 5.15 in image
> >>         Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label),
> >>         x$x, x$y,  :
> >>             invalid font type
> >>         In addition: There were 50 or more warnings (use warnings() to
> >>         see the
> >>         first 50)"
> >>
> >>         How can I install the font so that I can see the figures on the
> >>         pdf files?
> >>         Some information that may be useful:
> >>         (1) If I use the English ggtitle, there is no problem at all.
> >>         The pdf file
> >>         present the figures perfectly.
> >>         (2) If I use Chinese title, I can still see the figures p1 and
> p2
> >> on
> >>         Studio, but I can't see it at the pdf file produced by ggsave.
> >>         It gives an
> >>         error message
> >>
> >>
> >>
> >>         rm(list=ls())
> >>         library(ggplot2)
> >>         library(gridExtra)
> >>         df1<-data.frame(x=1:2, y=3:4, z=5:6)
> >>         #p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test1")
> >>         #p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test2")
> >>         p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("測試")
> >>  #Chinese title
> >>         p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("中文")
> >>  #Chinese title
> >>         p1<-p1+theme(text = element_text(family = "Kaiti TC"))
> >>         p2<-p2+theme(text = element_text(family = "Kaiti TC"))
> >>
> >>         p<-array(list(NA), dim=2)
> >>         p[[1]]<-p1
> >>         p[[2]]<-p2
> >>         p_series <- lapply(1:(length(p)), function(.x) p[.x][[1]])
> >>         m2 <- marrangeGrob(p_series, nrow=2, ncol=1)
> >>         ggsave("test_plot_chinese.pdf", m2)
> >>
> >>                  [[alternative HTML version deleted]]
> >>
> >>         ______________________________________________
> >>         [hidden email] <mailto:[hidden email]> mailing list
> >>         -- To UNSUBSCRIBE and more, see
> >>         https://stat.ethz.ch/mailman/listinfo/r-help
> >>         <https://stat.ethz.ch/mailman/listinfo/r-help>
> >>         PLEASE do read the posting guide
> >>         http://www.R-project.org/posting-guide.html
> >>         <http://www.R-project.org/posting-guide.html>
> >>         and provide commented, minimal, self-contained, reproducible
> code.
> >>
> >>
> >>     --     Dr Paul Murrell
> >>     Department of Statistics
> >>     The University of Auckland
> >>     Private Bag 92019
> >>     Auckland
> >>     New Zealand
> >>     64 9 3737599 x85392
> >>     [hidden email] <mailto:[hidden email]>
> >>     http://www.stat.auckland.ac.nz/~paul/
> >>     <http://www.stat.auckland.ac.nz/~paul/>
> >>
> >>
> >>
> > --
> > Dr Paul Murrell
> > Department of Statistics
> > The University of Auckland
> > Private Bag 92019
> > Auckland
> > New Zealand
> > 64 9 3737599 x85392
> > [hidden email]
> > http://www.stat.auckland.ac.nz/~paul/
> >
>
>         [[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.

        [[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: [FORGED] can't print ggplot with Chinese characters to pdf files

miao
In reply to this post by Paul Murrell-2
Hi,

   Following Paul's instruction, I have installed the Cairo. I tried to run
the program, and there is no error message at all. I did see the Chinese
title in the plot if I ask my RStudio to show the plot (if I type "p1"),
but the pdf file shows the plots without the Chinese titles.

library(ggplot2)
library(gridExtra)
df1<-data.frame(x=1:2, y=3:4, z=5:6)
#p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test1")
#p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test2")
p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("中文")
p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("測試")
p1<-p1+theme(text = element_text(family = "Kaiti TC"))
p2<-p2+theme(text = element_text(family = "Kaiti TC"))

p<-array(list(NA), dim=2)
p[[1]]<-p1
p[[2]]<-p2
p_series <- lapply(1:(length(p)), function(.x) p[.x][[1]])
m2 <- marrangeGrob(p_series, nrow=2, ncol=1)
#ggsave("test_plot_chinese.pdf", m2)
cairo_pdf("test_plot_chinese.pdf")
print(m2)
dev.off()




2017-10-12 19:55 GMT-07:00 Paul Murrell <[hidden email]>:

> Hi
>
> By the looks of it, you need to install Cairo graphics ...
>
> https://www.cairographics.org/download/
>
> Paul
>
> On 13/10/17 15:48, John wrote:
>
>> Thanks, Paul. Following your solution,  I got this error message:
>>
>> Warning message:
>> In cairo_pdf("test_plot_chinese.pdf") : failed to load cairo DLL
>>
>> Is there anything else I need to install?
>>
>> Thanks,
>>
>> John
>>
>> 2017-10-12 19:24 GMT-07:00 Paul Murrell <[hidden email]
>> <mailto:[hidden email]>>:
>>
>>
>>     Hi
>>
>>     Instead of ...
>>
>>     ggsave("test_plot_chinese.pdf", m2)
>>
>>     ... try ...
>>
>>     cairo_pdf("test_plot_chinese.pdf")
>>     print(m2)
>>     dev.off()
>>
>>     Paul
>>
>>
>>     On 13/10/17 02:12, John wrote:
>>
>>         I install the Chinese font "Kaiti TC" on my mac, but I can't
>>         print the
>>         figures to pdf file by "marrangeGrob" command, which is in the
>>         package
>>         "gridExtra". Error message after I type "ggsave(......)" (last
>>         line of the
>>         program):
>>
>>         "Saving 7.47 x 5.15 in image
>>         Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label),
>>         x$x, x$y,  :
>>             invalid font type
>>         In addition: There were 50 or more warnings (use warnings() to
>>         see the
>>         first 50)"
>>
>>         How can I install the font so that I can see the figures on the
>>         pdf files?
>>         Some information that may be useful:
>>         (1) If I use the English ggtitle, there is no problem at all.
>>         The pdf file
>>         present the figures perfectly.
>>         (2) If I use Chinese title, I can still see the figures p1 and p2
>> on
>>         Studio, but I can't see it at the pdf file produced by ggsave.
>>         It gives an
>>         error message
>>
>>
>>
>>         rm(list=ls())
>>         library(ggplot2)
>>         library(gridExtra)
>>         df1<-data.frame(x=1:2, y=3:4, z=5:6)
>>         #p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test1")
>>         #p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test2")
>>         p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("測試")
>>  #Chinese title
>>         p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("中文")
>>  #Chinese title
>>         p1<-p1+theme(text = element_text(family = "Kaiti TC"))
>>         p2<-p2+theme(text = element_text(family = "Kaiti TC"))
>>
>>         p<-array(list(NA), dim=2)
>>         p[[1]]<-p1
>>         p[[2]]<-p2
>>         p_series <- lapply(1:(length(p)), function(.x) p[.x][[1]])
>>         m2 <- marrangeGrob(p_series, nrow=2, ncol=1)
>>         ggsave("test_plot_chinese.pdf", m2)
>>
>>                  [[alternative HTML version deleted]]
>>
>>         ______________________________________________
>>         [hidden email] <mailto:[hidden email]> mailing list
>>         -- To UNSUBSCRIBE and more, see
>>         https://stat.ethz.ch/mailman/listinfo/r-help
>>         <https://stat.ethz.ch/mailman/listinfo/r-help>
>>         PLEASE do read the posting guide
>>         http://www.R-project.org/posting-guide.html
>>         <http://www.R-project.org/posting-guide.html>
>>         and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>     --     Dr Paul Murrell
>>     Department of Statistics
>>     The University of Auckland
>>     Private Bag 92019
>>     Auckland
>>     New Zealand
>>     64 9 3737599 x85392
>>     [hidden email] <mailto:[hidden email]>
>>     http://www.stat.auckland.ac.nz/~paul/
>>     <http://www.stat.auckland.ac.nz/~paul/>
>>
>>
>>
> --
> Dr Paul Murrell
> Department of Statistics
> The University of Auckland
> Private Bag 92019
> Auckland
> New Zealand
> 64 9 3737599 x85392
> [hidden email]
> http://www.stat.auckland.ac.nz/~paul/
>

        [[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: [FORGED] can't print ggplot with Chinese characters to pdf files

Martin Møller Skarbiniks Pedersen
On 20 October 2017 at 04:21, John <[hidden email]> wrote:
>
> Hi,
>
>    Following Paul's instruction, I have installed the Cairo. I tried to
run
> the program, and there is no error message at all. I did see the Chinese
> title in the plot if I ask my RStudio to show the plot (if I type "p1"),
> but the pdf file shows the plots without the Chinese titles.


[...]

>
> cairo_pdf("test_plot_chinese.pdf")
> print(m2)
> dev.off()
>

The help page for cairo_pdf says: "
‘cairo_pdf’ and ‘cairo_ps’ sometimes record _bitmaps_ and not vector
graphics.  On the other hand, they can (on suitable platforms) include a
much
     wider range of UTF-8 glyphs, and embed the fonts used.
"
I am not sure what sometimes means but maybe the fonts are not embedded in
the pdf-file?

try:
embed_fonts('test_plot_chinese.pdf')

note that it might fails if you haven't installed gs (ghostscript)


Regards
Martin

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