1

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.

MERose
  • 4,048
  • 7
  • 53
  • 79
  • 3
    Please see [how to make a great R reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – hrbrmstr Jul 29 '14 at 09:07
  • add a group aesthetic to `geom_lines` – EDi Jul 29 '14 at 09:19
  • I added a reproducible example for this question, and I tried to be as stupid as I was half a year ago. Please consider upvoting, if you have downvoted it. (maybe @hrbrmstr?) – MERose Dec 04 '14 at 16:28
  • I have not pressed the down-vote button – hrbrmstr Dec 04 '14 at 16:58
  • Okay, thank you. In this case I will have to live with the shame of two downvotes. – MERose Dec 04 '14 at 18:36

1 Answers1

6

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))
konvas
  • 14,126
  • 2
  • 40
  • 46