1

I'm trying to export a ggplot graphic to Word using plot2docx (from the rrtable package). I have this code for the data:

data0 = data.frame(
    GrR=c("Ejército Nacional","Paramilitares","Farc","Policía Nacional",
          "Sin información","Grupo armado","Grupos de limpieza social",
          "DAS","ELN","CTI","Otros agentes del Estado"),
    Cantidad=c(127,49,11,5,5,4,3,3,1,1,1)
)

data0$GrR = factor(
    data0$GrR,
    levels = c("Ejército Nacional","Paramilitares","Farc","Policía Nacional",
               "Sin información","Grupo armado","Grupos de limpieza social",
               "DAS","ELN","CTI","Otros agentes del Estado"))

This for the plot:

z = ggplot(data=data0, aes(x=GrR, y=Cantidad)) +
    geom_bar(stat="identity", width=0.5, fill = "blue") +
    geom_text(aes(label=scales::comma(Cantidad, scale = 1, accuracy = 1)),
              vjust=0.6,
              hjust=-0.5,
              color="black",
              position = position_dodge(0.5), size=5) +
    ggtitle("Víctimas de desaparición, según presunto grupo armado responsable") +
    labs(y="", x="") +
    coord_flip() +
    theme_minimal() +
    theme(plot.title = element_text(color="black", size=16, face="bold", hjust = 0.5)) +
    theme(axis.text.x=element_blank())

This for sending it to word:

 plot2docx(z)

It gives me this:

image

As you see, the title is incomplete. Also, the value of the last bar is not displayed. I have tried adding height and width specifications, but the image remains the same. Any idea on how to solve it? Thank you in advance.

LuizZ
  • 945
  • 2
  • 11
  • 23

2 Answers2

0

You can simply add a new line character to your title to break it.

+ ggtitle("Víctimas de desaparición, según presunto\ngrupo armado responsable")
Eric Leung
  • 2,354
  • 12
  • 25
0

If you are using RStudio, I recommend you run your code in a RMarkdown document. It will make much easier to export all your charts and results to Word.

To do so, click on File -> New File -> R Markdown . A dialog box will open. You can choose a title if you want, and you may leave as HTML, since it is more flexible and can be switch to Word any time.

A sample document will open. You may first click on the "knit" button, and then "knit to Word" just to see how it works.

enter image description here

After the document is knited to Word, you can close it, go back to R, and read the document. Copy your code inside the code chunks. Then knit your document to Word again.

If you still have problem with you chart size, check this post: How to set size for local image using knitr for markdown?

LuizZ
  • 945
  • 2
  • 11
  • 23