Legend
Legend

Reputation: 116790

How can I sort one column based on another?

I have the following data:

points <- structure(list(A = structure(c(1L, 1L, 2L, 2L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 8L, 9L, 9L, 10L, 11L, 12L, 12L, 13L, 
14L, 15L, 16L, 16L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 21L, 21L, 
22L, 23L, 24L, 24L, 25L, 26L, 26L, 27L, 28L, 29L, 30L, 30L, 31L, 
32L, 32L, 33L, 34L, 34L, 35L, 35L, 36L, 36L, 37L, 37L, 38L, 38L, 
39L, 39L, 40L, 41L), .Label = c("00017dd3-f55e-e011-854c-00237de2db9e", 
"0005f624-565a-e011-854c-00237de2db9e", "0007b82f-bfe0-4b55-963e-be5a2a1e7f7b", 
"00095b52-fd0a-e011-9264-00237de2db9e", "00098835-9554-4898-8d4b-82d42b8b4464", 
"000a727f-8334-e011-854c-00237de2db9e", "000c0a31-f459-4365-aa3a-1978deb89f67", 
"000e36a4-6e56-4851-8d36-2caf0bdd63ec", "000f05a6-cf94-4518-8de7-1773cbea8198", 
"00105574-a775-43e8-8472-c8b294e46786", "00112a96-3c47-409c-83bd-6f30d8d77100", 
"0012f133-f68e-e011-986b-78e7d1fa76f8", "0012f899-1c45-4917-90b7-11bea31e467e", 
"0014606b-17b7-46d6-957f-e23b43fcc773", "001478e2-3e50-486c-ae3b-d1ceb36f0fd0", 
"00159bab-ce82-454a-9343-f7d8f1500a68", "0015b84e-a48d-443e-936e-cabdb80604dc", 
"0018f8ba-c289-4483-bf74-5cd0e6c6ae9e", "0019487f-f31e-4e3e-b499-fd48077f71f9", 
"00199523-c42f-47fd-a44a-066fb726f6dd", "0019dace-41e1-439f-8b73-328d02537fe7", 
"001a346e-2a15-45d4-9fb1-6b4e2448d362", "001b0c90-5c86-4290-bad3-0d6794a6bfe8", 
"001c0d0d-3059-e011-854c-00237de2db9e", "001c9cbb-8c79-4cbf-bc50-219a70ab20b8", 
"001dcf83-7492-e011-986b-78e7d1fa76f8", "001dd5cf-3e3b-4ceb-823c-346c15f88878", 
"001e0ef7-b977-436a-ab20-8c4af4f5b230", "001fc407-da48-4c42-9325-7756b160cbbd", 
"001fdaa1-9471-e011-81d2-78e7d1fa76f8", "0020029f-2667-4c03-b99f-d803eccd27d4", 
"00218e00-896e-e011-81d2-78e7d1fa76f8", "002196af-60c7-4baf-abdb-589b3a481686", 
"0021a908-7ff6-df11-9264-00237de2db9e", "0021bced-909a-e011-986b-78e7d1fa76f8", 
"0021f0fb-cb9f-e011-986b-78e7d1fa76f8", "00228254-9b20-4d40-a4a5-a7c608f81dfa", 
"002357ba-5656-4308-bb92-6cc97f50d7aa", "0025eafd-a64f-e011-854c-00237de2db9e", 
"0026b36c-ebc2-43f0-a0f7-72f43b70530b", "00277e09-543e-449a-8571-38f71a21cee2"
), class = "factor"), B = structure(c(10L, 10L, 27L, 
27L, 28L, 23L, 23L, 38L, 38L, 24L, 24L, 19L, 35L, 26L, 26L, 28L, 
5L, 36L, 36L, 21L, 11L, 1L, 14L, 14L, 4L, 4L, 9L, 9L, 16L, 16L, 
3L, 7L, 7L, 13L, 37L, 17L, 17L, 29L, 15L, 15L, 12L, 31L, 32L, 
8L, 8L, 2L, 30L, 30L, 39L, 6L, 6L, 22L, 22L, 20L, 20L, 34L, 34L, 
18L, 18L, 33L, 33L, 25L, 29L), .Label = c("Aashu", "Actonica Studio", 
"appyminds", "blackink", "BroeckiE", "Challenge Solutions LLC", 
"CPP_MSP", "Datentechnik Innovation GmbH", "DerekM", "Dimension Srl", 
"Dmitry Kazarin", "edg3", "fruitymo", "Geckosan", "Genera Interactive SL", 
"HandyWare", "Infinite Square", "JTO.C Sq.", "JuJuZ", "Kitten Flavour", 
"Krofita", "Mark Agholor", "MCTronix.com", "Michael Snow", "michaloxo", 
"mobilewares.net", "NotoMedia LLC", "OKR", "P.F. CHAUVET", "Panoylhs", 
"Pratik Gandhi", "raavr", "ReadBooks", "RGP", "Seesmic", "The KeitaCorp", 
"viileetek", "Violineage", "Yalla Apps"), class = "factor"), 
    Date = structure(c(1302926400, 1302926400, 1302408000, 
    1302408000, 1327467600, 1292994000, 1292994000, 1322370000, 
    1322370000, 1297486800, 1297486800, 1326949200, 1321333200, 
    1314763200, 1314763200, 1328418000, 1327381200, 1307505600, 
    1307505600, 1325221200, 1324530000, 1327381200, 1326862800, 
    1326862800, 1326171600, 1326171600, 1325566800, 1325566800, 
    1327122000, 1327122000, 1320379200, 1324702800, 1324702800, 
    1327726800, 1327986000, 1301544000, 1301544000, 1332302400, 
    1308369600, 1308369600, 1325912400, 1331611200, 1325912400, 
    1304481600, 1304481600, 1325653200, 1304395200, 1304395200, 
    1322542800, 1294117200, 1294117200, 1309147200, 1309147200, 
    1309320000, 1309320000, 1313208000, 1313208000, 1325739600, 
    1325739600, 1300334400, 1300334400, 1325826000, 1321938000
    ), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("A", 
"B", "Date"), row.names = c(NA, -63L), class = "data.frame")

I am trying to draw a scatter plot with the Y-axis sorted based on the date field. However, I am only able to do the following where I treat A as a factor. Any suggestions on how to achieve this?

p = ggplot(points, aes(Date, factor(A))) +
    geom_point(aes(colour=factor(A)), size=1.8) + 
    scale_shape(solid=FALSE) + 
    scale_y_discrete("", breaks=NA)

Upvotes: 0

Views: 178

Answers (1)

IRTFM
IRTFM

Reputation: 263301

The error comes when you ask ggplot to consider a date value as a discrete variable after just swapping the x and y positions. It goes away when you remove that:

p = ggplot(points, aes(x=A, y=Date)) +
     geom_point(aes(colour=factor(A)), size=1.8) + 
     scale_shape(solid=FALSE) 
p

Or you can get rid of the lines by applying the discrete axis call to the x-axis:

 p = ggplot(points, aes(x=A, y=Date)) +
    geom_point(aes(colour=factor(A)), size=1.8) + 
    scale_shape(solid=FALSE) + 
    scale_x_discrete("", breaks=NULL)
 P

Unfortunately the title of the question doesn't seem to have a very clear connection to the text of the question, so I cannot tell if this was what you were asking for.

Upvotes: 1

Related Questions