【发布时间】:2015-10-15 22:36:49
【问题描述】:
我正在尝试构建一个图表,除其他外,它按值而非标签排序。但是,我发现无法始终如一地执行此操作 - 有时对图表后面的数据进行排序就足够了,但大多数情况下似乎没有帮助。
更糟糕的是,我想运行两组图表,最好是从同一个文件中提取不同的点。
这是我的代码的(简化版 - 真实版本有几个不同的值列等)版本:
library(ggplot2)
texttable <- "CustomerID Level TerritoryID PointValue
1 1 2 1.07008411325505
2 1 6 0.818345721216575
3 4 6 0.963128455248954
4 1 6 1.00707326033957
5 4 5 1.21104485305313
6 5 1 0.997679907969959
8 4 6 0.979654115606137
9 4 6 1.06385914268472
11 2 6 0.835519424818177
12 4 3 0.868668911128272
13 4 6 0.885912959934727
14 6 6 1.24296543210132
15 1 2 0.89646750726414
16 5 1 1.21236715108711
17 2 6 0.931486514531414
18 2 2 1.03713079535294
19 2 7 0.999841149204272
20 1 4 0.946128993266485
22 2 3 0.862294861210271
23 2 6 0.82423674978601
24 5 2 0.93637131146921
27 2 1 0.992294385756429
28 6 2 0.916891653017615
30 4 2 1.02938833174225
33 4 2 1.05472249469147
34 3 2 0.910270389167454
35 4 3 0.868004861090004
36 1 1 1.00459731116181
41 2 4 0.819587910554718
42 2 3 0.774525504141426"
datatable <- read.table(textConnection(texttable), header=TRUE)
for (i in 1:nrow(datatable)) #Identify the categories as not numeric
{
datatable$CustomerID[i] <- toString(datatable$CustomerID[i]);
datatable$Level[i] <- toString(datatable$Level[i]);
datatable$TerritoryID[i] <- toString(datatable$TerritoryID[i]);
}
datatable <- datatable[order(datatable$PointValue),];
level1table <- subset(datatable, Level=="1") ;
ggplot() + geom_point(aes(x=CustomerID, y=PointValue),data=level1table)+ggtitle("Level 1 PointValues");
territory6table <- subset(datatable, TerritoryID=="6")
ggplot()+ geom_point(aes(x=CustomerID, y=PointValue),data=territory6table)+ggtitle("Territory 6 PointValues");
我应该怎么做才能让点按 PointValue 的顺序出现?
【问题讨论】:
-
PointValue 为 y; x 是 CustomerID,按“级别”或“区域”过滤。作为最后一行的图表几乎是我想要的,只是它们没有排序。我已经编辑了代码以包含 x= 和 y= 以使其更清晰。