0

我正在尝试将我在 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 中的 NA 值,我认为这dput(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))
4

1 回答 1

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))

使用基础 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

ggplot(myspeed,aes(distance,speed))+geom_point(aes(color=id))+xlim(1e-5,100)+ylim(0,200)+scale_x_log10()

输出如下: 在此处输入图像描述

于 2013-07-05T17:57:09.217 回答