R中具有因子变量的线图 [英] Line plot with factor variables in R
本文介绍了R中具有因子变量的线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何让 R 根据因子变量在两个观察值之间画线?
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"
)
我只得到一种条形图:
plot(plotdata)
我也尝试将变量编码为 0 和 1,但随后我得到了一个连续的轴.
I also tried coding the variables as 0 and 1, but then I get a continuous axis with.
推荐答案
假设你的数据是
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
带基数 R
plot(as.numeric(d$x), d$y, type = "l", xaxt = "n")
axis(1, labels = as.character(d$x), at = as.numeric(d$x))
使用 ggplot2
library(ggplot2)
ggplot(d, aes(x = x, y = y)) + geom_line(aes(group = 1))
这篇关于R中具有因子变量的线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文