Multiple Line Plot in R from a Table

Hi guys,

I've recently been using a package known as 'randomLCA' for random effects latent class analysis.

Within the randomLCA summary stats, you get a table of 'conditional probabilities' (for all classes)

However, because classes are unordered, the summary stats and in-built plots don't correspond to how I'd like to visually represent them.

Therefore, I wondered if there was a way to take my ordered table and plot it in a similar style to the multiple line plot produced by the randomLCA package?

The table is given

Conditional outcome probabilities 
             MR     SC    Anx   Pain    Act    Mob
Class  1 0.8884 0.8367 0.5446 0.2954 0.1522 0.0175
Class  2 0.9843 0.9785 0.7621 0.8115 0.8026 1.0000
Class  3  0.0304 0.0164 0.3054 0.2371 0.0027 0.0212

I would like a plot of the table to be presented similar to this:

Conditional1

Which was generated using the following code from randomLCA


plot(trial.lca3, graphtype = "conditional", type = "a", pch = 1:2, cex = 0.6,
xlab = "Health Domain", ylab = "Conditional Outcome Probability",
scales = list(x = list(at = 1:6, labels = names(trial)[1:6])), 
key = list(space = "right", adj = 1, border = TRUE, cex = 0.7,
text = list(c("Class 1", "Class 2", "Class 3")),
col = trellis.par.get()$superpose.symbol$col[1:3]))	 

PS, for clarity, my initial data frame is called 'trial', and my randomLCA output is called 'trial.lca3'

Would appreciate anyone's help on the matter :slight_smile:

Can you post the output of

dput(trial.lca3)

Then we can work with the exact object that you have.

I clearly asked for the wrong thing. I thought I was asking for the 3x6 table in your first post that holds the data that are plotted. Is that available on its own or does the plot() function go straight from that big data set to drawing the plot?

Hi FJCC,

No problem.

The plotting procedure in RandomLCA takes the conditional probabilities (very similar to the ones I expressed in table form), and plots them.

However, it just so happens that given the randomLCA presents unordered classes; the output plots aren't all that usual for inference.

For example, you might wish to say that class 1 is the 'best state' of some latent process; yet randomLCA might identify this class, and given it a label Class 3.

So, that's why I re-ordered the table to ensure there was a sufficient rank-order. I just didn't know if it was possible to take a data table like I have and produce an equivalent multi-line plot?

It doesn't seem particularly simple in GGplot for some reason. Would appreciate any help, as always.

Best,

Andrew

Is this the sort of thing you are looking for? I manually made the table in a cumbersome way but I suppose you have that object already. If it is not a table, in the sense that class(TAB) returns "table", you may have to transpose the object explicitly.

#Manually construct the Table
DAT <- c(0.8884, 0.8367, 0.5446, 0.2954, 0.1522, 0.0175,
0.9843, 0.9785, 0.7621, 0.8115, 0.8026, 1.0000,
0.0304, 0.0164, 0.3054, 0.2371, 0.0027, 0.0212)

MAT <- matrix(DAT, nrow = 3, byrow = TRUE)

TAB <- as.table(MAT)

dimnames(TAB) <- list(c("Class 1", "Class 2", "Class 3"), 
                      c("MR", "SC", "Anx", "Pain", "Act", "Mob"))
TAB
#>             MR     SC    Anx   Pain    Act    Mob
#> Class 1 0.8884 0.8367 0.5446 0.2954 0.1522 0.0175
#> Class 2 0.9843 0.9785 0.7621 0.8115 0.8026 1.0000
#> Class 3 0.0304 0.0164 0.3054 0.2371 0.0027 0.0212

#Transform the table to  a data frame
#This automatically transposes the table.
DF <- as.data.frame(TAB)
DF
#>       Var1 Var2   Freq
#> 1  Class 1   MR 0.8884
#> 2  Class 2   MR 0.9843
#> 3  Class 3   MR 0.0304
#> 4  Class 1   SC 0.8367
#> 5  Class 2   SC 0.9785
#> 6  Class 3   SC 0.0164
#> 7  Class 1  Anx 0.5446
#> 8  Class 2  Anx 0.7621
#> 9  Class 3  Anx 0.3054
#> 10 Class 1 Pain 0.2954
#> 11 Class 2 Pain 0.8115
#> 12 Class 3 Pain 0.2371
#> 13 Class 1  Act 0.1522
#> 14 Class 2  Act 0.8026
#> 15 Class 3  Act 0.0027
#> 16 Class 1  Mob 0.0175
#> 17 Class 2  Mob 1.0000
#> 18 Class 3  Mob 0.0212

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
ggplot(DF, aes(x = Var2, y = Freq, group = Var1, color = Var1)) + geom_line()

Created on 2021-07-04 by the reprex package (v0.3.0)

1 Like

Yeah, thanks.

That's an amazing help.

Can I just ask if there is a way to rename var1, var2, and freq, in DF?

One way to rename the columns of a data frame is with the colnames() function.

colnames(DF) <- c("NewName1","NewName2","NewName3")

Thank you. Appreciate your help :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.