Last active
June 12, 2017 17:03
-
-
Save sebastiansauer/1ba64a684f55d6dec2782ed07b93affa to your computer and use it in GitHub Desktop.
Create a graph with DiagrammeR, easy example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plot path diagram with {DiagrammeR} | |
data(Wage, package = "ISLR") #load data | |
lm_wage <- lm(wage ~ health + age + health:age, data = Wage) # run linear model | |
summary(lm_wage) | |
library(semPlot) # plot path diagram with {semPlot} | |
semPaths(lm_wage, whatLabel = "est", vsize.man = 16, edge.label.cex=0.6) | |
library(DiagrammeR) # now plot with {DiagrammeR} | |
# define edges: | |
edges_set <- data.frame( | |
from = c("ref", "age", "ref_age", "error"), | |
to = c("wage", "wage", "wage", "wage"), | |
stlye = c("solid", "solid", "solid", "dashed"), | |
label = c("-1.8ns"," 0.51***", "0.43**", "78.66***") | |
) | |
# define nodes: | |
nodes_set <- data.frame( | |
nodes = c("ref", "age", "ref_age", "wage", "error"), | |
shape = c("square", "square", "circle", "square", "square") | |
) | |
# define visual attributes of nodes | |
node_attrs <- c("style = filled", "fillcolor = '#00998a'", | |
"color = gray", "shape = circle", "fontname = Helvetica", | |
"width = 1") | |
# now define graph object | |
my_graph <- create_graph( | |
nodes = nodes_set, | |
edges_df = edges_set, | |
node_attrs = node_attrs, | |
graph_attrs = "rankdir = LR") | |
# print it | |
render_graph(my_graph, width = 500, height = 500) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment