如何在R中将数据帧分割成不同的图?
我有一个表格的csv如何在R中将数据帧分割成不同的图?,r,R,我有一个表格的csv latency1, latency2, test-type 1.3233831,1.0406423,A 1.6799337,1.1520619,A 1.6301824,1.1536479,B 2.3465363,1,2346457,C 1.2452355,1.9987547,C ... 我想绘制三个不同的图:一个是A型的latency1 vs latency2,一个是B型的latency1 vs latency2,还有一个是C型的latency1 vs latency2。
latency1, latency2, test-type
1.3233831,1.0406423,A
1.6799337,1.1520619,A
1.6301824,1.1536479,B
2.3465363,1,2346457,C
1.2452355,1.9987547,C
...
我想绘制三个不同的图:一个是A型的latency1 vs latency2,一个是B型的latency1 vs latency2,还有一个是C型的latency1 vs latency2。我知道如何在同一个图上绘制不同的数据集,但不知道如何将一个数据帧分割成这样的多个图。对不起,我是个新手。提前感谢。:) lattice包中的绘图函数有一个公式界面,其
|
运算符允许您指示用于将数据拆分为单独绘图的“晶格”或“网格”的条件变量
试试这个,例如:
## Read in your data
df <- read.table(text="latency1, latency2, testType
1.3233831,1.0406423,A
1.6799337,1.1520619,A
1.6301824,1.1536479,B
2.3465363,1.2346457,C
1.2452355,1.9987547,C", header=T, sep=",")
library(lattice)
xyplot(latency2 ~ latency1 | testType, data = df, type = "b")
##读入数据
lattice软件包中的df绘图函数有一个公式界面,其|
运算符允许您指示用于将数据拆分为单独绘图的“晶格”或“网格”的条件变量
试试这个,例如:
## Read in your data
df <- read.table(text="latency1, latency2, testType
1.3233831,1.0406423,A
1.6799337,1.1520619,A
1.6301824,1.1536479,B
2.3465363,1.2346457,C
1.2452355,1.9987547,C", header=T, sep=",")
library(lattice)
xyplot(latency2 ~ latency1 | testType, data = df, type = "b")
##读入数据
多面图
代码
如果要使用ggplot2
执行此操作:
library(ggplot2)
df = read.table(text='latency1,latency2,testtype
1.3233831,1.0406423,A
1.6799337,1.1520619,A
1.6301824,1.1536479,B
2.3465363,1.2346457,C
1.2452355,1.9987547,C',
header=TRUE, sep=',')
p = ggplot(data = df,
aes(x = latency1, y = latency2, colour = testtype)) +
geom_point() +
facet_grid( . ~ testtype )
p
平面图
代码
如果要使用ggplot2
执行此操作:
library(ggplot2)
df = read.table(text='latency1,latency2,testtype
1.3233831,1.0406423,A
1.6799337,1.1520619,A
1.6301824,1.1536479,B
2.3465363,1.2346457,C
1.2452355,1.9987547,C',
header=TRUE, sep=',')
p = ggplot(data = df,
aes(x = latency1, y = latency2, colour = testtype)) +
geom_point() +
facet_grid( . ~ testtype )
p