我遇到了一些问题...我在R中很新,我想这个问题对你们这些人来说可能很容易,但我不知道如何解决它。
有人可以帮我解决这个问题吗?
P.S:这是表:xy是你可以看到的矩阵,x值重复自己,所以每次重新开始时都是一条新曲线。
x y
[1,] -10 1.14259527
[2,] -9 1.15024188
[3,] -8 1.10517450
[4,] -7 1.00961311
[5,] -6 0.71238360
[6,] -5 0.20355333
[7,] -4 0.04061895
[8,] -10 1.11022461
[9,] -9 1.11083317
[10,] -8 1.07867942
[11,] -7 0.98422000
[12,] -6 0.73539660
[13,] -5 0.36134577
[14,] -4 0.18124645
[15,] -10 2.13212408
[16,] -9 1.14529425
[17,] -8 1.25102307
[18,] -7 1.16045169
[19,] -6 0.50321380
[20,] -5 0.15422609
[21,] -4 0.10198811
答案 0 :(得分:1)
这可能会让你入门。我假设您的矩阵称为xy
。
my.data <- data.frame(xy)
library(ggplot2) # you may have to install the package
创建一些标签:
my_labels <- parse(text = paste("x^", seq(-10, -4, 1), sep = ""))
然后绘制......
ggplot(my.data) +
geom_point(aes(x = x, y = y, color = as.factor(x))) +
geom_line(aes(x = x, y = y, color = as.factor(x))) +
scale_x_continuous("x", breaks = seq(-10, -4, 1), labels = my_labels)
答案 1 :(得分:1)
您可以创建一个额外的列来标记新曲线的开头(示例中的A,B和C),然后是以下代码
library(ggplot2)
ggplot(data=df, aes(x=x, y=y, group=z, colour=z)) +
geom_line() +
geom_point()
数据:
df <- structure(list(x = c(-10L, -9L, -8L, -7L, -6L, -5L, -4L, -10L,
-9L, -8L, -7L, -6L, -5L, -4L, -10L, -9L, -8L, -7L, -6L, -5L,
-4L), y = c(1.14259527, 1.15024188, 1.1051745, 1.00961311, 0.7123836,
0.20355333, 0.04061895, 1.11022461, 1.11083317, 1.07867942, 0.98422,
0.7353966, 0.36134577, 0.18124645, 2.13212408, 1.14529425, 1.25102307,
1.16045169, 0.5032138, 0.15422609, 0.10198811), z = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor")), .Names = c("x",
"y", "z"), class = "data.frame", row.names = c(NA, -21L))
输出:
x y z
1 -10 1.14259527 A
2 -9 1.15024188 A
3 -8 1.10517450 A
4 -7 1.00961311 A
5 -6 0.71238360 A
6 -5 0.20355333 A
7 -4 0.04061895 A
8 -10 1.11022461 B
9 -9 1.11083317 B
10 -8 1.07867942 B
11 -7 0.98422000 B
12 -6 0.73539660 B
13 -5 0.36134577 B
14 -4 0.18124645 B
15 -10 2.13212408 C
16 -9 1.14529425 C
17 -8 1.25102307 C
18 -7 1.16045169 C
19 -6 0.50321380 C
20 -5 0.15422609 C
21 -4 0.10198811 C