Reputation: 4421
How can I make R draw lines between two observations according with factor variables?
I have two 'time' points, early and late, coded as categorical
plotdata <- structure(list(
x = structure(1:2, .Label = c("early", "late"), class = "factor"),
y = 1:2
),
.Names = c("x", "y"), row.names = c(NA, -2L), class = "data.frame"
)
I only get kind of a bar plot:
plot(plotdata)
I also tried coding the variables as 0 and 1, but then I get a continuous axis with.
Upvotes: 1
Views: 8355
Reputation: 14346
Let's say your data is
d <- structure(list(x = structure(1:2, .Label = c("early", "late"), class = "factor"),
y = 1:2), .Names = c("x", "y"), row.names = c(NA, -2L), class = "data.frame")
d
# x y
# early 1
# late 2
With base R
plot(as.numeric(d$x), d$y, type = "l", xaxt = "n")
axis(1, labels = as.character(d$x), at = as.numeric(d$x))
With ggplot2
library(ggplot2)
ggplot(d, aes(x = x, y = y)) + geom_line(aes(group = 1))
Upvotes: 6