0

I'm attempting to build a chart that, among other things, is sorted by the values rather than the labels. However, I have found no way to do this consistently - sometimes sorting the data behind the chart is enough, but mostly it doesn't seem to help.

It's made worse in that I want to run two sets of charts, ideally from the same file, pulling out different points for each.

Here's a (simplified - the real version has several different columns of values etc) version of my code:

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

What should I be doing to get the points to appear in order of PointValue?

Margaret
  • 5,749
  • 20
  • 56
  • 72
  • PointValue is y; x is CustomerID, filtered by either "Level" or "Territory". The charts as the final lines are pretty much what I want except they aren't sorted. I've edited the code to include x= and y= to make that clearer. – Margaret Oct 15 '15 at 22:43

1 Answers1

1

reorder a factor variable based on another ordinal one.

territory6table <- subset(datatable, TerritoryID=="6")
territory6table$CID <- as.factor(territory6table$CustomerID)
territory6table$CID <- reorder(territory6table$CID, territory6table$PointValue)

ggplot()+
  geom_point(aes(CID, PointValue),data=territory6table)+
  ggtitle("Territory 6 PointValues")

enter image description here

Paulo E. Cardoso
  • 5,778
  • 32
  • 42
  • You can also do the reordering on the fly inside ggplot: `aes(reorder(CID, Pointvalue), PointValue)`. – eipi10 Oct 15 '15 at 23:08