Breaking lines in colnames or rownames

Asked

Viewed 248 times

3

This is an example of a report that I’m doing. As the margin settings are already adjusted, when I change the names of the columns with the colnames it ends up passing the margin. Any help on how to break this line ? For example the "Predicted ARIMA". Will an image of how it could look. Exemplo

x <- runif(n = 15, min = 1, 10)
y <- runif(n = 15, min = 1, 10)
z <- runif(n = 15, min = 1, 10)
w <- runif(n = 15, min = 1, 10)
k <- runif(n = 15, min = 1, 10)


df_exemplo <- data.frame(x, y, z, w, k)
colnames(df_exemplo) <- c("Previsto ARIMA", "Previsto Holtwinters", "Observado", "Erro ARIMA (%)", "Erro Holtwinters (%)")
  • This is an Excel question?

  • @Noisy just the example is in Excel to show how I wanted to show in PDF, I forgot to comment that this is a report in Sweave. If there’s any way to break this line directly through the R, I’ve tried using the " n" but not the funnel either.

  • @Rafaelb, where will the text appear? If it is in a graph, for example, the line break with "\n" works both in the r-base how much in the ggplot2

  • @Tomásbarcellos the text will appear in the name of a column (colnames) of a table.

  • If it’s for interactive use only, View(df_exemplo) already breaks the lines even without entering any "\n"

1 answer

3


I recommend using the package kableExtra to format tables in knitr or sweave. It works in conjunction with the function kable package knitr and the results are very nice. See below:

inserir a descrição da imagem aqui

Follow the code used to create this table:

\documentclass{article}

\usepackage{booktabs}
\usepackage{makecell}

\begin{document}

<<Dados>>=
x <- runif(n = 15, min = 1, 10)
y <- runif(n = 15, min = 1, 10)
z <- runif(n = 15, min = 1, 10)
w <- runif(n = 15, min = 1, 10)
k <- runif(n = 15, min = 1, 10)

df_exemplo <- data.frame(x, y, z, w, k)
colnames(df_exemplo) <- c("Previsto ARIMA", "Previsto Holtwinters", 
                          "Observado", "Erro ARIMA (%)", 
                          "Erro Holtwinters (%)")
@


<<TabelaComQuebra, warning=FALSE>>=
library(knitr)
library(kableExtra)

kable(df_exemplo, "latex", booktabs=TRUE, digits=4) %>%
  column_spec(1:5, width = c("1.5cm", "2cm", "2cm", "2cm", "2.5cm"))
@


\end{document}

Note that I had to load two Latex packets for the table to come out in this format. The line break in the table title exited automatically when using the function column_spec. I just set the width I’d like the column to have and the software did the rest.

  • Warning message: In table_info$align_vector[column] <- unlist(lapply(table_info$align_vector_origin[column], : number of items to replace is not a Multiple of Replacement length

  • I tried to change the sequence in "column_spec" and add an extra column or remove a column, however it did not work.

  • 1

    You were right, you were using it on another table.

  • 1

    Big Thanks, Marcus. I will migrate from xtable pro Kable, from now on.

  • Kable queen, xtable anything : )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.