在绘制y1 = x时,我不知道如何强制R绘制线而不是水平条," x"作为定性变量
"类型"参数似乎无法理解
如何指定?
这是我的数据
x,y1,y2,y3
Jan,20.4,30.9,25.7
Feb,20.3,32.7,6.3
Mar,21.5,34.2,12.0
Apr,23.2,34.9,76.4
May,23.8,33.7,184.3
Jun,23.8,32.9,125.9
Jul,23.3,32.7,149.8
Aug,23.4,32.5,135.1
Sep,23.1,32.1,198.6
Oct,22.9,31.5,219.9
Nov,22.6,30.2,259.9
Dec,21.6,29.7,98.4
这是我的代码
plot(x,y1,type="l",col="blue")
答案 0 :(得分:1)
df <- read.table(text = "x,y1,y2,y3
Jan,20.4,30.9,25.7
Feb,20.3,32.7,6.3
Mar,21.5,34.2,12.0
Apr,23.2,34.9,76.4
May,23.8,33.7,184.3
Jun,23.8,32.9,125.9
Jul,23.3,32.7,149.8
Aug,23.4,32.5,135.1
Sep,23.1,32.1,198.6
Oct,22.9,31.5,219.9
Nov,22.6,30.2,259.9
Dec,21.6,29.7,98.4",
sep = ',', header = TRUE
)
df$x <- factor(df$x, ordered = TRUE, levels = as.character(df$x))
plot(y1 ~ x, data = df)
points(y1 ~ as.numeric(x), data = df, type = 'l')
答案 1 :(得分:0)
你可以像下面那样绘制
data <- structure(list(x = structure(1:12, .Label = c("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
), class = c("ordered", "factor")), y1 = c(20.4, 20.3, 21.5,
23.2, 23.8, 23.8, 23.3, 23.4, 23.1, 22.9, 22.6, 21.6), y2 = c(30.9,
32.7, 34.2, 34.9, 33.7, 32.9, 32.7, 32.5, 32.1, 31.5, 30.2, 29.7
), y3 = c(25.7, 6.3, 12, 76.4, 184.3, 125.9, 149.8, 135.1, 198.6,
219.9, 259.9, 98.4)), .Names = c("x", "y1", "y2", "y3"), row.names = c(NA,
-12L), class = "data.frame")
matplot(data, type = c("b"),lty = 'solid',xaxt="n", pch=1,col = 1:3)
axis(1, at=1:12,lty = 1, labels=data$x)