我正在尝试将我在Excel中制作的图形重新创建为R.这是图表的excel版本......
http://i.imgur.com/FJXbLy8.png
我的数据集分为5个段。为简单起见,我将把我的x和y标签称为时间和距离。我想要在形状图上绘制5个“汽车”。
包含数据的CSV文件如下所示(不是实数!):
A B C D E F ...
1 4 1 8 7 15
3 5 5 10 12 20
5 6 7 14 20 40
其中(A,B)给出了汽车1的时间/距离,(C,D)给出了汽车2的时间/距离,(E,F)给出了汽车3的时间/距离等等。
我的代码如下所示:
speed = read.csv (file = "c:/users/XXX/desktop/speed", header = TRUE, sep = ',')
plot (A ~ B, data = speed, xlim = range (1e-5:100), ylim = range (0:200), log = "x")
par(new = TRUE)
plot (C ~ D, data = speed, xlim = range (1e-5:100), ylim = range (0:200), log = "x")
par (new = TRUE)
plot (E ~ F, data = speed, xlim = range (1e-5:100), ylim = range (0:200), log = "x")
par(new = TRUE )
plot(G ~ H, data = speed, xlim = range (1e-5:100), ylim = range (0:200), log = "x")
par (new = TRUE)
plot(I ~ J, data = speed, xlim = range (1e-5:100), ylim = range (0:200), log = "x")
我收到一条错误消息,说我的ylim值无效......我尝试更改数字而没有运气。
任何提示都将不胜感激!
这是dput(speed)
,如果它对任何人都有帮助......
- 我注意到输入中的NA值,我认为这是因为并非每个“汽车”的每个数据集都不相等?不知道如何解决这个问题...我可以将每个“汽车”的每个数据集放入不同的文件中,那会更好吗?
structure(list(A = c(3.59e-05, 3.75e-05, 9.67e-05, 3.92e-05,
2.14e-05, 9.8e-05, 0.000228481, 0.000228481, 0.000415583, 0.000859052,
0.002014948, 0.004079371, 0.00406138, 0.004353728, 0.008455587,
0.007780939, 0.018260469, NA, NA, NA, NA, NA, NA), B = c(4.76,
6.28, 10.5, 10.6, 12.3, 12.8, 16, 16.8, 20.7, 25.2, 34.4, 36.4,
37.7, 43.6, 48.7, 49.2, 66.5, NA, NA, NA, NA, NA, NA), C = c(1.734691244,
2.016976959, 1.707373272, 1.461511521, 0.805880184, 0.417509677,
0.427070968, 0.220364977, 0.21763318, 0.170282028, 0.169826728,
0.095612903, 0.094247005, 0.048717051, 0.044072995, 0.034921475,
0.023721106, 0.022901567, 0.018485161, 0.015252535, 0.008240922,
0.003942894, 0.002868387), D = c(176, 175, 169, 169, 169, 162,
161, 146, 146, 143, 143, 121, 117, 90, 90, 77.7, 70.3, 69.2,
67, 59.6, 50.4, 36.1, 33.7), E = c(0.0235, 0.044636324, 0.075155479,
0.072909589, 0.09736484, 0.0988621, 0.199428082, 0.202422603,
0.362878995, 0.370365297, 0.355392694, 1.438410959, 0.727212329,
0.722221461, 1.40597032, NA, NA, NA, NA, NA, NA, NA, NA), F = c(69L,
90L, 111L, 114L, 114L, 116L, 143L, 146L, 161L, 163L, 164L, 170L,
172L, 175L, 180L, NA, NA, NA, NA, NA, NA, NA, NA), G = c(35.29300714,
17.47300714, 4.351007143, 3.182292857, 3.182292857, 1.411864286,
1.435007143, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), H = c(180L, 180L, 180L, 180L, 177L, 175L, 171L,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), I = c(0.021, 0.0258, 0.029929032, 0.034574194, 0.064612903,
0.088870968, 0.17816129, 0.163967742, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), J = c(67, 68.7, 75.2, 84.6,
115, 121, 152, 155, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA)), .Names = c("A", "B", "C", "D", "E", "F", "G",
"H", "I", "J"), class = "data.frame", row.names = c(NA, -23L))
答案 0 :(得分:5)
我不确定你在看什么。我认为这可能有所帮助。
speed <- structure(list(A = c(3.59e-05, 3.75e-05, 9.67e-05, 3.92e-05,
2.14e-05, 9.8e-05, 0.000228481, 0.000228481, 0.000415583, 0.000859052,
0.002014948, 0.004079371, 0.00406138, 0.004353728, 0.008455587,
0.007780939, 0.018260469, NA, NA, NA, NA, NA, NA), B = c(4.76, 6.28, 10.5,
10.6, 12.3, 12.8, 16, 16.8, 20.7, 25.2, 34.4, 36.4, 37.7, 43.6, 48.7, 49.2,
66.5, NA, NA, NA, NA, NA, NA), C = c(1.734691244, 2.016976959, 1.707373272,
1.461511521, 0.805880184, 0.417509677, 0.427070968, 0.220364977, 0.21763318,
0.170282028, 0.169826728, 0.095612903, 0.094247005, 0.048717051, 0.044072995,
0.034921475, 0.023721106, 0.022901567, 0.018485161, 0.015252535, 0.008240922,
0.003942894, 0.002868387), D = c(176, 175, 169, 169, 169, 162, 161, 146, 146,
143, 143, 121, 117, 90, 90, 77.7, 70.3, 69.2, 67, 59.6, 50.4, 36.1, 33.7),
E = c(0.0235, 0.044636324, 0.075155479, 0.072909589, 0.09736484, 0.0988621,
0.199428082, 0.202422603, 0.362878995, 0.370365297, 0.355392694, 1.438410959,
0.727212329, 0.722221461, 1.40597032, NA, NA, NA, NA, NA, NA, NA, NA),
F = c(69L, 90L, 111L, 114L, 114L, 116L, 143L, 146L, 161L, 163L, 164L, 170L,
172L, 175L, 180L, NA, NA, NA, NA, NA, NA, NA, NA), G = c(35.29300714,
17.47300714, 4.351007143, 3.182292857, 3.182292857, 1.411864286, 1.435007143,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), H = c(180L, 180L, 180L, 180L, 177L, 175L, 171L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA ), I = c(0.021, 0.0258, 0.029929032, 0.034574194,
0.064612903, 0.088870968, 0.17816129, 0.163967742, NA, NA, NA, NA, NA,
NA,NA, NA, NA, NA, NA, NA, NA, NA, NA), J = c(67, 68.7, 75.2, 84.6, 115,
121, 152, 155, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)),
.Names = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),
class = "data.frame", row.names = c(NA, -23L))
使用Base R
绘图
plot(speed$A,speed$B,xlim=range(1e-5:100),ylim=range(0:200),xlab="Time",ylab="Distance",pch=19,log="x")
points(speed$C,speed$D,col="red",pch=21)
points(speed$E,speed$F,col="blue",pch=18)
points(speed$G,speed$H,col="yellow",pch=19)
points(speed$I,speed$J,col="green",pch=20)
legend("top", legend = c("Car 1","Car 2","Car 3","Car 4","Car 5"), col = c("black","red","blue","yellow","green"),
ncol = 2,pch=c(19,21,18,19,20))
输出图如下:
使用ggplot2的选项 ggplot2会自动删除缺少值的点。
library(ggplot2)
ggplot(speed,aes(A,B))+geom_point()+xlim(1e-5,100)+ylim(0,200)+scale_x_log10()+
geom_point(data=speed,aes(C,D),col="red")+geom_point(data=speed,aes(E,F),col="blue")+
geom_point(data=speed,aes(G,H),col="green")+geom_point(data=speed,aes(I,J),col="orange",shape=2)+
xlab("Distance")+ylab("Distance")
使用长格式数据和ggplot2
的另一个选项## Separate the data by cars
car1 <- speed[,c(1,2)]
car1$id <- "Car1"
car2 <- speed[,c(3,4)]
car2$id <- "Car2"
car3 <- speed[,c(5,6)]
car3$id <- "Car3"
car4 <- speed[,c(7,8)]
car4$id <- "Car4"
car5 <- speed[,c(9,10)]
car5$id <- "Car5"
names(car1)<- names(car2) <- names(car3) <- names(car4) <- names(car5)<- c("distance","speed","id")
myspeed <- rbind(car1,car2,car3,car4,car5)
ggplot(myspeed,aes(distance,speed))+geom_point(aes(color=id))+xlim(1e-5,100)+ylim(0,200)+scale_x_log10()
输出如下: