2017-11-19 1 views
2

Ich habe einen Faktor comp_id, der 4 Stufen hat (comp1 bis comp4). Ich möchte jedes Level von der höchsten zur niedrigsten in einer geom_line Handlung bestellen.Geom_Linien von der höchsten zur niedrigsten in jeder Facette bestellen

ich diese Handlung bekam

enter image description here

mit diesem Skript

library(data.table) 
library(ggplot2) 
dat <- as.data.table(df) 
dat[, ord := sprintf("%02i", frank(dat, comp_id, -value, ties.method = "first"))] 


ggplot(dat, aes(x = ord, y = value , group = comp_id , colour = comp_id))+ 
    geom_line()+ 
    facet_wrap(~comp_id, ncol = 1, scales = "free_x", labeller = label_parsed, drop = TRUE)+ 
    theme(axis.text.x=element_text(angle=35, vjust=1, hjust=1, 
           )) 

x-Achse

Etiketten
+scale_x_discrete(labels = dat[, setNames(as.character(predictor), ord)]) 

ersetzen Wie Sie sehen können, es funktionierte für alle Ebenen fein außer comp3 wo Variablen geordnet waren (100 bis 105) gezeichnet am Anfang der Facette, wo sie am Ende gezeichnet werden sollten. Ich frage mich, was falsch gelaufen ist. Irgendwelche Vorschläge werden geschätzt.

DATA

> dput(df) 
structure(list(predictor = c("c_C2", "c_C3", "c_C4", "d_D2", 
"d_D3", "d_D4", "d_D5", "h_BF", "h_BFI", "h_ER", "h_f", "h_PET", 
"h_QuFl", "h_Ra", "l_Da", "l_NaCo", "l_ShBe", "m_a", "m_DrDe", 
"m_ElRa", "m_MeElm", "m_MeSlPe", "Mr_Co", "Mr_GRAv", "Mr_GREy", 
"Mr_Mu", "Mr_Sa", "s_SaLo", "s_SiLo", "s_sSiLo", "s_Stl", "Sr_Li", 
"Sr_SaCoCoTe", "Sr_SaLoSi", "Sr_SaMubcl", "c_C2", "c_C3", "c_C4", 
"d_D2", "d_D3", "d_D4", "d_D5", "h_BF", "h_BFI", "h_ER", "h_f", 
"h_PET", "h_QuFl", "h_Ra", "l_Da", "l_NaCo", "l_ShBe", "m_a", 
"m_DrDe", "m_ElRa", "m_MeElm", "m_MeSlPe", "Mr_Co", "Mr_GRAv", 
"Mr_GREy", "Mr_Mu", "Mr_Sa", "s_SaLo", "s_SiLo", "s_sSiLo", "s_Stl", 
"Sr_Li", "Sr_SaCoCoTe", "Sr_SaLoSi", "Sr_SaMubcl", "c_C2", "c_C3", 
"c_C4", "d_D2", "d_D3", "d_D4", "d_D5", "h_BF", "h_BFI", "h_ER", 
"h_f", "h_PET", "h_QuFl", "h_Ra", "l_Da", "l_NaCo", "l_ShBe", 
"m_a", "m_DrDe", "m_ElRa", "m_MeElm", "m_MeSlPe", "Mr_Co", "Mr_GRAv", 
"Mr_GREy", "Mr_Mu", "Mr_Sa", "s_SaLo", "s_SiLo", "s_sSiLo", "s_Stl", 
"Sr_Li", "Sr_SaCoCoTe", "Sr_SaLoSi", "Sr_SaMubcl", "c_C2", "c_C3", 
"c_C4", "d_D2", "d_D3", "d_D4", "d_D5", "h_BF", "h_BFI", "h_ER", 
"h_f", "h_PET", "h_QuFl", "h_Ra", "l_Da", "l_NaCo", "l_ShBe", 
"m_a", "m_DrDe", "m_ElRa", "m_MeElm", "m_MeSlPe", "Mr_Co", "Mr_GRAv", 
"Mr_GREy", "Mr_Mu", "Mr_Sa", "s_SaLo", "s_SiLo", "s_sSiLo", "s_Stl", 
"Sr_Li", "Sr_SaCoCoTe", "Sr_SaLoSi", "Sr_SaMubcl"), comp_id = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("comp1", 
"comp2", "comp3", "comp4"), class = "factor"), value = c(0.0633325075111356, 
-0.0193713154441617, 0.000785081075580719, 0.287610195287972, 
-0.0913783988809322, -0.122928438782758, 0.305621459875726, 0.0356570047659489, 
0.367574915852176, -0.240835821698893, 0.0035597425358522, 0.295952594554233, 
-0.0439920206129066, -0.235580426938533, 0.191947159509267, -0.132931615006652, 
0.065155805120025, 0.038311284807646, 0.187182963731454, 0.120969596703282, 
-0.118935354491654, -0.173851183397175, 0.125870264508295, 0.158977975187947, 
-0.209351605852615, -0.0231602829054583, 0.078383405846316, 0.0959455355349004, 
0.238306328058919, -0.188667962455942, -0.138302814516594, -0.0586994514783439, 
0.019524606432138, 0.210636138928319, -0.204454169255484, -0.149879080476447, 
0.282741114373524, -0.272911905666994, 0.102508662574812, -0.35056583225677, 
0.257262737814283, 0.202117594283655, 0.191773977367133, 0.298513575892895, 
0.139576016330362, 0.165641757285727, -0.071542760140058, 0.116819894570386, 
0.145104320521166, 0.126636637925691, 0.0810830011112734, -0.0949935353116725, 
0.0785254958291791, 0.0326439188223452, 0.065833153228218, 0.155405435626813, 
0.128737420120173, 0.214943178842044, -0.0210359058420932, 0.0117832135586799, 
0.0762824228178598, -0.29145271973574, -0.17089908579109, -0.0992003952524557, 
0.163749177828358, 0.196561728687348, 0.0951493527111932, 0.17238711709624, 
0.0638301486629609, -0.0351097560634362, 0.0647994534663104, 
-0.154895398844537, 0.186448424833243, 0.240881706707846, -0.241364320964797, 
-0.089459273670017, 0.0491598702691844, -0.200660845431752, -0.0339722426751736, 
0.131396251991635, -0.195471026941394, -0.05919918680627, -0.184160478394361, 
0.129464190293723, 0.193021703469902, 0.178985522376368, -0.245966624042807, 
-0.23478025602535, 0.198620462933836, -0.157573246492692, -0.00808698000885529, 
0.0413693509741982, -0.121020524702316, 0.105148862728949, 0.214386790903084, 
-0.204515275979768, -0.0906160054540168, -0.276985960928353, 
0.0768294557774406, -0.074181085595352, 0.138680723918144, -0.119684214245213, 
-0.0919678069134681, 0.322602153170851, 0.228878715511945, -0.433082572929477, 
0.05754301130056, 0.130719232236558, 0.253999327778221, 0.0469683234741709, 
-0.0258294537417061, -0.258318910865727, -0.00406472629347961, 
-0.165003562015847, -0.0292142578447021, 0.00862320222199929, 
0.0875367120866572, 0.0331716236283754, -0.0418387105725687, 
-0.12523142839593, -0.200857915084298, 0.138378222132672, 0.00992811008724002, 
-0.0201043482518474, -0.148894977354092, -0.323240591170999, 
-0.0556713655820164, 0.379033571103569, -0.264420286734383, 0.127560649906739, 
-0.00546455207923468, -0.203293330594455, -0.122085266718802, 
-0.0970860819632599, -0.173818516285048, -0.0585031143296301, 
0.125084378608705, 0.0655074180474436, 0.254339734692359, 0.00114212078410835 
)), class = "data.frame", .Names = c("predictor", "comp_id", 
"value"), row.names = c(NA, -140L)) 
+0

Wäre es nicht einfacher, eine numerische x-Skala zu verwenden und sie einfach umzukehren? – joran

Antwort

2

Hier ist ein Ansatz tidyverse und kontinuierliche Skala

library(tidyverse) 
df %>% 
    arrange(comp_id, desc(value)) %>% #arrange by comp_id and descending value 
    mutate(ord = 1:n()) -> dat #create the x scale 

ggplot(dat, aes(x = ord, y = value , group = comp_id , colour = comp_id))+ 
    geom_line()+ 
    facet_wrap(~comp_id, ncol = 1, scales = "free_x", drop = TRUE)+ 
    theme(axis.text.x=element_text(angle=35, vjust=1, hjust=1)) + 
    scale_x_continuous(labels = dat$predictor, breaks = dat$ord, expand = c(0.02, 0.02)) 

enter image description here

+0

Danke für Ihre Zeit und Hilfe. Ich frage mich, warum sie zwei h_BFI-Variablen in der comp1 sind, während nur eine Variable da sein sollte. Auch fehlt die Zeile für eine Anzahl von Variablen in den verschiedenen Facetten wie h_ER in comp2 und die letzte Variable. – aelwan

+0

@aelwan Der Grund war die kontinuierliche Skala, ich machte eine Bearbeitung mit 'expand = (c0.02, 0.02)'. Sieht fest aus, bitte überprüfen. – missuse

+0

Vielen Dank für Ihre Zeit und Hilfe – aelwan

0

Neben der nette Antwort von @missuse, war es eine andere Art und Weise, gab mir was ich wollte. Verwendung als Faktor/als Numerisch/as.character mit der x-Achse

aes(x = as.factor(as.numeric(as.character(ord))) 

und als numerische/als Zeichen verwendet, während der X-Achse ersetzt

as.numeric(as.character(ord)) 

Die endgültige Skript Etiketten

ggplot(dat, aes(x = as.factor(as.numeric(as.character(ord))), y = value , group = comp_id , colour = comp_id))+ 
    geom_line()+ 
    facet_wrap(~comp_id, ncol = 1, scales = "free_x", labeller = label_parsed, drop = TRUE)+ 
    theme(axis.text.x=element_text(angle=35, vjust=1, hjust=1, 
))+ 
    scale_x_discrete(labels = dat[, setNames(as.character(predictor), as.numeric(as.character(ord)))]) 
ist
Verwandte Themen