I'm making a table using formattable and I'd like to hide the column titles for the descriptive columns.
So for example, for dataframe "df" I'd like the resulting table to only show the column titles for msmt1, 2, and 3 and then have blank title names for the "site" and "variable" columns.
library(formattable)
df<-data.frame("site" = rep(c("1", "2"), 3),
"variable" = c("C", "C", "O", "O", "N", "N"),
"msmt1" = runif(6),
"msmt2" = runif(6),
"msmt3" = runif(6))
formattable(df)
Replacing the df column names with " " makes them show up as "X." in the table.
Is this possible in formattable?
How about this:
formattable(df, col.names = c("","","msmt1", "msmt2", "msmt3"))
In case you have more columns and don't want to specify them all literally, you could make it more dynamic like this:
formattable(df, col.names = c(rep("", 2), colnames(df)[3:ncol(df)]))
Related
I want to create a table of calendar plots. I used calendR to create the plots (over 70 plots) and I want to get them into a table. I often work with gt tables and I found a working function for adding ggplots to the table.
library(calendR)
library(gt)
plot = calendR(start = "M", # Weeks start on Monday
mbg.col = 4, # Background color of the month names
months.col = "white", # Color of the text of the month names
special.days = "weekend", # Color the weekends
special.col = "lightblue", # Color of the special.days
lty = 0, # Line type (no line)
weeknames = c("M", "T", # Week names
"W", "T",
"F", "S",
"S"),
title.size = 20, # Title size
orientation = "p") # Background image
dplyr::tibble(
Year1 = NA,
Year2 = NA
) %>%
gt() %>%
text_transform(
locations = cells_body(columns = c(Year1, Year2)),
fn = function(x) {
plot %>%
ggplot_image(height = px(300))
}
)
As you see, the plots are not readable because they are too big or do not have the right resolution. How can I solve this problem? I want to have at least 2 plots a row, more would be great.
In a data frame we have this format:
df <- data.frame(id = c(1,2,3), text_en_1 = c("Google", "Yahoo", "Amazon"), text_en_2 = c("Amazon", "Yahoo", "NA"), text_en_3 = c("Ieekkj","NA","NA"))
NA in this data frame is a missing value not a text. I just insert it to show the missing value.
How is it possible to could for every row using the id column how many columns have a value.
Example expected out put
data.frame(id = c(1,2,3), number_of_columns_with_text = c(3,2,1))
data.frame(id = df[,1], number_of_columns_with_text = rowSums(!is.na(df[,-1])))
I am having a dataframe with both character and numeric columns. I am trying to center the character column (which is done) and (I don't know how to) center flushed-right numeric column. Further, both column names are centered:
Some relevant posts (this and this), but I couldn't incorporate the techniques (if relevant) to my code.
The following code produces the table below:
df <- data.frame(name = c("a", "bb", "ccc"), number = c(10, 193048, 200))
kable(df, format="latex", align = c("c", "r")) %>%
kable_styling(full_width = TRUE) %>%
row_spec(0, align = "c")
I understand that I can use LaTeX code to produce a single user-customised table. But I need the example to be reproducible in Rmd (imagine I have lots of df) and therefore need to specify within kable().
Should I modify the LaTeX table environment (i.e. \begin{tabu} to \linewidth {>{\centering}X>{\centering}X}) and if so how in R, or use something like multirow to customise the second column??
Thanks to #Fran from this post, answer below:
df <- data.frame(name = c("a", "bb", "ccc"), number = c(10, 193048, 200))
kable(df,"latex", align="cr", booktabs = T, linesep = "") %>%
kable_styling() %>%
column_spec(1, width = "3em") %>%
column_spec(2, width = "3em")
I'm using the R package TraMineR. I would like to plot frequent event sequences by using the command seqpcplot. I previously coded the states in the alphabet as to keep them in alphabetical order so that when I compute the sequences by using the seqdef command without specifying the labels and states options I obtain the following output:
[>] state coding:
[alphabet] [label] [long label]
1 a.sin a.sin a.sin
2 b.co0 b.co0 b.co0
3 c.co1 c.co1 c.co1
4 d.co2+ d.co2+ d.co2+
5 e.ma0 e.ma0 e.ma0
6 f.ma1 f.ma1 f.ma1
7 g.ma2+ g.ma2+ g.ma2+
8 h.sin0 h.sin0 h.sin0
9 i.lp1 i.lp1 i.lp1
10 l.lp2+ l.lp2+ l.lp2+
11 m.lp1_18 m.lp1_18 m.lp1_18
12 n.lp2_18 n.lp2_18 n.lp2_18
I then convert the state-sequence objet in an event-sequece objet by using seqecreate. When plotting the event sequences by seqpcplot I obtain a very nice graph where the states are ordered alphabetically on the y-axis according to the alphabet.
However, I would like to use longer labels in the graphs, so that I specified the labels and states options in the seqdef command as
lab<-c("single", "cohabNOchildren","cohab1child","cohab2+children","marrNOchildren","marr1child","marr2+children","singleNOchildren","loneMother1child","loneMother2+children","loneMother1child_over18","loneMother2+children_over18")
obtaining:
[>] state coding:
[alphabet] [label] [long label]
1 a.sin single single
2 b.co0 cohabNOchildren cohabNOchildren
3 c.co1 cohab1child cohab1child
4 d.co2+ cohab2+children cohab2+children
5 e.ma0 marrNOchildren marrNOchildren
6 f.ma1 marr1child marr1child
7 g.ma2+ marr2+children marr2+children
8 h.sin0 singleNOchildren singleNOchildren
9 i.lp1 loneMother1child loneMother1child
10 l.lp2+ loneMother2+children loneMother2+children
11 m.lp1_18 loneMother1child_over18 loneMother1child_over18
12 n.lp2_18 loneMother2+children_over18 loneMother2+children_over18
As before, I then computed the event sequences and plot them by using seqpcplot:
seqpcplot(example.seqe,
filter = list(type = "function",
value = "cumfreq",
level = 0.8),
order.align = "last",
ltype = "non-embeddable",
cex = 1.5, lwd = .9,
lcourse = "downwards")
This time the states on the y-axis were the states are ordered alphabetically but following the order given by the labels and states labels rather than the alphabet, as I wished.
Is there a way to keep the alphabetical order given in the alphabet when plotting with seqpcplot when the labels and states options are specified and may follow a different alphabetical order from the alphabet?
Thanks.
I agree with the solution above. As a supplement, here a number of possible solutions:
Using seqecreate and the alphabet argument in seqpcplot:
dat <- data.frame(id = factor(1, 1, 1),
timestamp = c(0, 20, 22),
event = factor(c("A", "B", "C")))
dat.seqe <- seqecreate(dat)
seqpcplot(dat.seqe, alphabet = c("C", "A", "B"))
Using seqecreate only
dat <- data.frame(id = factor(1, 1, 1),
timestamp = c(0, 20, 22),
event = factor(c("A", "B", "C"),levels = c("C", "A", "B")))
dat.seqe <- seqecreate(dat)
seqpcplot(dat.seqe)
Using seqdef (here the original categories are different than the labels to be shown in the y-axis)
dat <- data.frame(id = factor(1),
ev.0 = factor("AA", levels = c("CC", "AA", "BB")),
ev.20 = factor("BB", levels = c("CC", "AA", "BB")),
ev.22 = factor("CC", levels = c("CC", "AA", "BB")))
dat.seq <- seqdef(dat, var = 2:4, alphabet = c("CC", "AA", "BB"),
states = c("C", "A", "B"))
seqpcplot(dat.seq)
The last solution may be the one you're looking for. Hope it helps.
The alphabet argument of the seqpcplot function is there to control that order. Something like
seqpcplot(example.seqe,
alphabet = lab,
filter = list(type = "function",
value = "cumfreq",
level = 0.8),
order.align = "last",
ltype = "non-embeddable",
cex = 1.5, lwd = .9,
lcourse = "downwards")
should give you the expected plot.
Using the wordcloud package in R I would like to color different words according to a categorical variable in the dataset. Say my data is as follows:
name weight group
1 Aba 10 x
2 Bcd 20 y
3 Cde 30 z
4 Def 5 x
And here as a dput:
dat <- structure(list(name = c("Aba", "Bcd", "Cde", "Def"), weight = c(10,
20, 30, 5), group= c("x", "y", "z", "x")), .Names = c("name",
"weight", "group"), row.names = c(NA, -4L), class = "data.frame")
Is there a way in wordcloud() to color the names by their group (x, y, z) or should I use different software/packages?
It will automatically choose from a color list based on frequency or by word order if ordered.colors is specified.
name = c("Aba","Bcd","Cde","Def")
weight = c(10,20,30,5)
colorlist = c("red","blue","green","red")
wordcloud(name, weight, colors=colorlist, ordered.colors=TRUE)
The example above works for independent variables. In a data frame, your color specification will be stored as a factor, and it will have to be converted to text by wrapping it in as.character like this:
wordcloud(df$name, df$weight, colors=as.character(df$color), ordered.colors=TRUE)
If you just have factors and not a list of colors, you can generate a parallel colorlist with a couple of lines.
#general solution for any number of categories
basecolors = rainbow(length(unique(group)))
# solution for known categories
basecolors = c("red","green","blue")
group = c("x","y","z","x")
# find position of group in list of groups, and select that matching color...
colorlist = basecolors[ match(group,unique(group)) ]