| autoplot.markovchain | R Documentation |
Creates a ggplot2 representation of a discrete-time Markov chain. Nodes are states and directed edges represent transitions with positive probability. Communicating classes are shown with different node fills.
autoplot.markovchain(
object,
threshold = 0,
show_probabilities = TRUE,
digits = 2,
node_size = 6,
edge_width = 1,
type = c("graph", "eigenvalues", "flow", "comparison"),
steps = 20,
initial = NULL,
other = NULL,
what = c("transition", "stationary"),
...
)
object |
An object of class 'markovchain'. |
threshold |
Minimum transition probability to display. Defaults to 0. |
show_probabilities |
Logical; whether to label edges with transition probabilities. Defaults to 'TRUE'. |
digits |
Number of digits used to format transition probabilities. |
node_size |
Size of state nodes in the ggplot2 plot. |
edge_width |
Minimum width multiplier for transition edges. |
type |
Type of plot: |
steps |
Number of steps of the |
initial |
Initial distribution of the |
other |
For |
what |
For |
... |
Currently unused, reserved for future extensions. |
A ggplot object.
if (requireNamespace("ggplot2", quietly = TRUE)) {
weather <- matrix(c(0.7, 0.2, 0.1,
0.3, 0.4, 0.3,
0.2, 0.45, 0.35),
nrow = 3, byrow = TRUE,
dimnames = list(c("sunny", "cloudy", "rain"),
c("sunny", "cloudy", "rain")))
mc <- new("markovchain", states = rownames(weather),
transitionMatrix = weather, name = "Weather")
ggplot2::autoplot(mc)
ggplot2::autoplot(mc, type = "eigenvalues")
ggplot2::autoplot(mc, type = "flow", steps = 10, initial = "rain")
# compare with a "stickier" version of the same chain
sticky <- lazyChain(mc, alpha = 0.5)
sticky@name <- "Lazy weather"
ggplot2::autoplot(mc, type = "comparison", other = sticky)
ggplot2::autoplot(mc, type = "comparison", other = sticky,
what = "stationary")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.