[英]Line plot with factor variables in 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"
)
我只得到一種條形圖:
plot(plotdata)
我也嘗試將變量編碼為0和1,但隨后得到一個連續軸。
假設您的數據是
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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.