R中的因子变量的线图

MER*_*ose 1 plot r

如何根据因子变量在两个观测值之间绘制R线?

我有两个'时间'点,早期和晚期,编码为分类

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"
)
Run Code Online (Sandbox Code Playgroud)

我只得到一个酒吧情节:

plot(plotdata)
Run Code Online (Sandbox Code Playgroud)

我也尝试将变量编码为0和1,但随后我得到一个连续的轴.

kon*_*vas 5

假设你的数据是

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
Run Code Online (Sandbox Code Playgroud)

基地R

plot(as.numeric(d$x), d$y, type = "l", xaxt = "n")
axis(1, labels = as.character(d$x), at = as.numeric(d$x))
Run Code Online (Sandbox Code Playgroud)

用ggplot2

library(ggplot2)
ggplot(d, aes(x = x, y = y)) + geom_line(aes(group = 1))
Run Code Online (Sandbox Code Playgroud)