Reputation: 47
This is from an online example on Hidden Markov Models. There are the codes
library(ggplot2)
library(gridExtra)
library(reshape2)
ggplot(hmm1$draws, aes(x = as.numeric(roll), y = state, fill = state, col = state)) +
geom_bar(stat = "identity", alpha = I(0.7)) +
scale_fill_manual(values = mycols, name = "State:\nPerson that\nrolled the\ndice", labels = c("Alice", "Bob")) +
scale_color_manual(values = mycols, name = "State:\nPerson that\nrolled the\ndice", labels = c("Alice", "Bob")) +
theme(axis.ticks = element_blank(), axis.text.y = element_blank()) +
labs(y = "Actual State")
They are supposed to produce this outcome below
But I get that the bars are "overlapping", being positioned on two separate lines
I do not know if this depends on coding (I have tried different options but nothing changed) or on some parameters set on my R. Any suggestions?
Upvotes: 2
Views: 44
Reputation: 173928
You need to have state represented as a number on the y axis for this to work properly under the current version of ggplot:
ggplot(draws, aes(x = roll, y = as.numeric(as.factor(state)),
fill = state, col = state)) +
geom_bar(stat = "identity", alpha = I(0.7)) +
scale_fill_manual(values = mycols,
name = "State:\nPerson that\nrolled the\ndice",
labels = c("Alice", "Bob")) +
scale_color_manual(values = mycols,
name = "State:\nPerson that\nrolled the\ndice",
labels = c("Alice", "Bob")) +
labs(y = "State") +
theme(axis.ticks = element_blank(), axis.text.y = element_blank())
Data
draws <- structure(list(roll = 1:100, state = c("alice", "alice", "alice",
"alice", "alice", "alice", "alice", "alice", "alice", "bob",
"bob", "bob", "bob", "bob", "bob", "bob", "bob", "alice", "alice",
"alice", "alice", "alice", "bob", "bob", "alice", "alice", "alice",
"alice", "alice", "alice", "alice", "alice", "bob", "bob", "bob",
"bob", "bob", "bob", "alice", "alice", "alice", "alice", "alice",
"bob", "bob", "alice", "alice", "alice", "alice", "bob", "alice",
"alice", "alice", "alice", "alice", "alice", "alice", "alice",
"alice", "bob", "bob", "bob", "bob", "bob", "bob", "bob", "bob",
"bob", "bob", "bob", "bob", "bob", "bob", "bob", "bob", "bob",
"bob", "bob", "bob", "bob", "alice", "alice", "alice", "alice",
"bob", "alice", "alice", "alice", "alice", "bob", "bob", "bob",
"bob", "bob", "bob", "bob", "bob", "bob", "bob", "bob"), obs = c(8L,
9L, 3L, 7L, 3L, 5L, 10L, 4L, 7L, 8L, 7L, 15L, 17L, 14L, 14L,
12L, 11L, 6L, 4L, 2L, 4L, 5L, 10L, 14L, 3L, 4L, 4L, 6L, 3L, 4L,
2L, 6L, 12L, 14L, 15L, 12L, 8L, 12L, 2L, 3L, 7L, 3L, 4L, 8L,
14L, 1L, 3L, 2L, 2L, 15L, 4L, 4L, 3L, 7L, 7L, 4L, 3L, 6L, 4L,
13L, 18L, 9L, 12L, 13L, 13L, 10L, 10L, 12L, 13L, 15L, 12L, 11L,
12L, 12L, 9L, 11L, 13L, 16L, 7L, 7L, 1L, 4L, 5L, 6L, 7L, 1L,
5L, 3L, 3L, 12L, 12L, 12L, 10L, 12L, 6L, 6L, 10L, 12L, 7L, 5L
), dice = c(5L, 9L, 8L, 9L, 6L, 8L, 8L, 10L, 4L, 7L, 7L, 9L,
7L, 7L, 7L, 5L, 3L, 8L, 10L, 11L, 10L, 2L, 7L, 2L, 5L, 10L, 12L,
9L, 6L, 6L, 8L, 4L, 10L, 5L, 5L, 7L, 6L, 2L, 7L, 9L, 11L, 7L,
3L, 9L, 2L, 7L, 7L, 12L, 2L, 4L, 7L, 6L, 6L, 10L, 5L, 10L, 6L,
11L, 2L, 6L, 10L, 6L, 9L, 7L, 10L, 11L, 8L, 7L, 12L, 9L, 10L,
5L, 8L, 7L, 5L, 5L, 6L, 11L, 9L, 4L, 6L, 7L, 6L, 2L, 4L, 12L,
7L, 7L, 2L, 7L, 6L, 9L, 6L, 6L, 7L, 8L, 6L, 7L, 11L, 7L)),
class = "data.frame", row.names = c(NA, -100L))
Upvotes: 1