cjvanlissa / tidySEM

54 stars 7 forks source link

Edge labeled with Greek letter and subscripted number? #44

Open Jinsong-Chen opened 2 years ago

Jinsong-Chen commented 2 years ago

Hi,

Nice package! Can the edges be labeled with Greek letters and/or subscripted numbers (e.g., \lambda_{11} for factor one to item one)?

Best Jinsong

cjvanlissa commented 2 years ago

I'm not sure. The plot is a ggplot, so it can do anything ggplot can do. I am not very familiar with plotting Greek letters, so please let me know if you knowmore. To help you on the way: the edge labels are taken from a column of a data.frame, with class "character". If it is somehow possible to parse these character strings, then it might be possible. Alternatively, you could just add the labels you want to the plot by hand, e.g.:

p <- graph_sem(model)
p + whatever the geom is to get Greek letters
Jinsong-Chen commented 2 years ago

Thanks for reminding. It seems Greek letter can be labeled using unicode. Meanwhile, I have two more questions: 1) The diagram doesn't look good if it's drew horizontally (e.g., factors on left + items on right). Can it be improved? 2) Can the shape of nodes be triangle (e.g., for intercept) or NA (e.g., no shape or border line)?

cjvanlissa commented 2 years ago

Point 1. can be addressed just by altering the layout and using coord_fixed. Point 2: You can make the line color of a node white, which would achieve your goal; triangles are not implemented yet; the main issue would be that the code for where to connect edges would have to be completely refactored. Currently, they are connected at 4 extremes of a node. A triangle has only 3 extremes, and they are in different positions. I will consider it, but it's not a priority atm.

Jinsong-Chen commented 2 years ago

I'm not sure. The plot is a ggplot, so it can do anything ggplot can do. I am not very familiar with plotting Greek letters, so please let me know if you knowmore. To help you on the way: the edge labels are taken from a column of a data.frame, with class "character". If it is somehow possible to parse these character strings, then it might be possible. Alternatively, you could just add the labels you want to the plot by hand, e.g.:

p <- graph_sem(model)
p + whatever the geom is to get Greek letters

Seems it might work with something like: p <- graph_sem(edges,layout,nodes) p+geomtext(aes(x,y,label=expression(lambda{11})),parse = TRUE)

But then one needs to obtain the x and y coordinates of the edges. I am using the edges, nodes and layout arguments in graph_sem() for drawing (i.e., no model or data). Do you know how I can obtain the coordinates?

Edit: Actually, it might be better to have additional "label" arguments for the edges and nodes in graph_sem so that one can vectorize the label (i.e., not as data.frame) and parse it or use expression for any math or special symbols.