繁体   English   中英

R 中 scale_x_discrete 的问题

[英]An issue with scale_x_discrete in R

在以下情节中:

在此处输入图片说明

我想在 x 轴上显示每隔一个元素,例如 1, 3, 5, ...., 100。要生成此图:

  thePlot <- ggplot(data=m, aes(x=Time, y=yValues, group=Technique, color=Technique)) + 
    geom_line(size=1)+
    scale_x_discrete(labels = seq(1, 100, by=1)) +
    theme(legend.position="top", axis.text.x = element_text(angle = 60),text = element_text(size=17),legend.title=element_blank())+
    scale_colour_discrete(name="Technique",
                          breaks=c("statements_0.01","predicates_0.01","predicates_binary_0.01","default","fitness_based"),
                          labels=c("Statement-based Sharing","Predicate-based Sharing","Predicate Binary-based Sharing","No Sharing","Fitness-based Sharing")) +
    geom_point(aes(shape=Technique, colour = Technique), show.legend = T,size=3) + 
    labs(x = "Generation", y = "Phenotypic Diversity")+
    scale_shape_discrete(labels=c("Statement-based Sharing","Predicate-based Sharing","Predicate Binary-based Sharing","No Sharing","Fitness-based Sharing"),
                         breaks=c("statements_0.01","predicates_0.01","predicates_binary_0.01","default","fitness_based")) +
    guides(shape=guide_legend(override.aes=list(size=3, linetype=0)))

我找到了一种解决方案,建议将scale_x_discrete(labels = seq(1, 100, by=1))更改为scale_x_discrete(limits=Time,breaks=Time[seq(1,length(Time),by=2)])但是这导致:

在此处输入图片说明

你知道如何解决这个问题吗?

可重现的数据:

structure(list(Technique = structure(c(3L, 5L, 7L, 2L, 1L), .Label = c("fitness_based", 
"default", "predicates_0.01", "predicates_0.1", "predicates_binary_0.01", 
"predicates_binary_0.1", "statements_0.01", "statements_0.1"), class = "factor"), 
    X1 = c(0.757322086076667, 0.747185783254167, 0.752789369573333, 
    0.754711748036667, 0.755798780023333), X2 = c(0.76881662217, 
    0.76491858546, 0.768799689715, 0.768471238696667, 0.771855250406667
    ), X3 = c(0.773396676423333, 0.778557707249167, 0.778845072068333, 
    0.78127458226, 0.78293776366), X4 = c(0.78133971783, 0.785436248786667, 
    0.787175126311667, 0.792108599456667, 0.789121243226667), 
    X5 = c(0.79003092836, 0.791450999573333, 0.794838536548333, 
    0.798332792036667, 0.79596220183), X6 = c(0.793882901243333, 
    0.794714737163333, 0.801487379675, 0.80309360915, 0.8024938263
    ), X7 = c(0.796313305506667, 0.805929151840833, 0.803535915145, 
    0.807766587856667, 0.8094057865), X8 = c(0.79856306344, 0.8167806698675, 
    0.809872485215, 0.812411981456667, 0.81058225709), X9 = c(0.802129860003333, 
    0.817388270930833, 0.813609635233333, 0.818074517276667, 
    0.811993537653333), X10 = c(0.80618219699, 0.8218142573775, 
    0.81478610582, 0.821231137956667, 0.813869646123333), X11 = c(0.810673806455, 
    0.823456414240833, 0.815810373553333, 0.822499645356667, 
    0.813869646123333), X12 = c(0.814517892475833, 0.828719696970833, 
    0.817771157866667, 0.82729526034, 0.814702979456667), X13 = c(0.814929415109167, 
    0.831034677724167, 0.82008613862, 0.82963191105, 0.82078528653
    ), X14 = c(0.8191979182825, 0.834142278790833, 0.821943739686667, 
    0.830435590546667, 0.824945628083333), X15 = c(0.820001597779167, 
    0.834142278790833, 0.82314472008, 0.832244172003333, 0.825141706516667
    ), X16 = c(0.821442532175833, 0.8351420367175, 0.823929033803333, 
    0.832832407296667, 0.827883642), X17 = c(0.8226680223725, 
    0.8359508602475, 0.826183935763333, 0.833028485726667, 0.832491485136667
    ), X18 = c(0.823501355705833, 0.836980272010833, 0.826183935763333, 
    0.834033387683333, 0.83540300777), X19 = c(0.8241669177025, 
    0.838176108374167, 0.82841432792, 0.835425302473333, 0.83721673326
    ), X20 = c(0.826709934909167, 0.838764343670833, 0.829785496105, 
    0.8364250604, 0.838313672036667), X21 = c(0.82805402093, 
    0.838764343670833, 0.830177652968333, 0.8364250604, 0.84304271745
    ), X22 = c(0.828250099363333, 0.840256279154167, 0.831000698238333, 
    0.836621138833333, 0.84635154098), X23 = c(0.8316775187175, 
    0.8414572595475, 0.833151235871667, 0.838679962363333, 0.846743697843333
    ), X24 = c(0.834638060964167, 0.8414572595475, 0.836868419481667, 
    0.839757046996667, 0.848016860906667), X25 = c(0.8369174727275, 
    0.842754932414167, 0.838945750415, 0.84418217507, 0.849462939336667
    ), X26 = c(0.837334139394167, 0.843147089274167, 0.839337907278333, 
    0.84457433193, 0.85029627267), X27 = c(0.838510609984167, 
    0.843147089274167, 0.839730064141667, 0.844770410363333, 
    0.851962939336667), X28 = c(0.8389221326175, 0.846720376610833, 
    0.840141586775, 0.844966488793333, 0.853163919726667), X29 = c(0.841367110604167, 
    0.846916455040833, 0.841906292658333, 0.846976292713333, 
    0.85335999816), X30 = c(0.8436465223675, 0.847308611904167, 
    0.842322959325, 0.846976292713333, 0.854868459566667), X31 = c(0.8457247116675, 
    0.8483135138675, 0.843398228141667, 0.846976292713333, 0.857333661696667
    ), X32 = c(0.8469747116675, 0.848730180534167, 0.846166455146667, 
    0.847387815346667, 0.85858745899), X33 = c(0.847391378334167, 
    0.849318415830833, 0.84657797778, 0.84822114868, 0.85983745899
    ), X34 = c(0.8498423587275, 0.849729938464167, 0.84697013464, 
    0.848637815346667, 0.862386478596667), X35 = c(0.850259025394167, 
    0.852241801235, 0.847166213073333, 0.85010524103, 0.862386478596667
    ), X36 = c(0.852019778081667, 0.853687879665, 0.848808369936667, 
    0.850717986126667, 0.86317079232), X37 = c(0.852853111415, 
    0.857556722971667, 0.849225036603333, 0.851526809653333, 
    0.86317079232), X38 = c(0.853269778081667, 0.857556722971667, 
    0.849225036603333, 0.853708182203333, 0.86317079232), X39 = c(0.853269778081667, 
    0.857556722971667, 0.849617193463333, 0.85412484887, 0.86317079232
    ), X40 = c(0.853950050188333, 0.857556722971667, 0.849617193463333, 
    0.85673539309, 0.86442079232), X41 = c(0.855506672838333, 
    0.857968245605, 0.849813271893333, 0.8574156652, 0.864812949183333
    ), X42 = c(0.855702751268333, 0.858997657368333, 0.851743544, 
    0.8574156652, 0.865401184476667), X43 = c(0.856119417935, 
    0.859193735798333, 0.852135700866667, 0.8574156652, 0.865401184476667
    ), X44 = c(0.858807589976667, 0.859193735798333, 0.852135700866667, 
    0.858490934016667, 0.865401184476667), X45 = c(0.85921911261, 
    0.859193735798333, 0.852135700866667, 0.858907600683333, 
    0.865401184476667), X46 = c(0.85921911261, 0.859389814231667, 
    0.853508249883333, 0.859769241113333, 0.865401184476667), 
    X47 = c(0.859611269473333, 0.860240154365, 0.853508249883333, 
    0.860841670086667, 0.865817851143333), X48 = c(0.860022792106667, 
    0.861068343665, 0.853900406746667, 0.860841670086667, 0.865817851143333
    ), X49 = c(0.864374390766667, 0.861537827231667, 0.853900406746667, 
    0.862134687296667, 0.866498123253333), X50 = c(0.865624390766667, 
    0.861954493898333, 0.854317073413333, 0.86296802063, 0.866694201683333
    ), X51 = c(0.865624390766667, 0.862758173398333, 0.85512075291, 
    0.864432606463333, 0.867933913616667), X52 = c(0.865624390766667, 
    0.862954251828333, 0.85580102502, 0.864432606463333, 0.868350580283333
    ), X53 = c(0.865624390766667, 0.862954251828333, 0.85580102502, 
    0.86560907705, 0.86876724695), X54 = c(0.86738909665, 0.863150330258333, 
    0.85580102502, 0.86560907705, 0.86876724695), X55 = c(0.868590077043333, 
    0.863150330258333, 0.856212547653333, 0.86600123391, 0.86876724695
    ), X56 = c(0.868590077043333, 0.863561852891667, 0.858859606476667, 
    0.86600123391, 0.86876724695), X57 = c(0.868786155473333, 
    0.864412193029167, 0.859055684906667, 0.868084567243333, 
    0.86876724695), X58 = c(0.868786155473333, 0.8646082714625, 
    0.860501763336667, 0.868476724106667, 0.868963325383333), 
    X59 = c(0.868786155473333, 0.865024938129167, 0.861577032153333, 
    0.869726724106667, 0.868963325383333), X60 = c(0.868786155473333, 
    0.8671754757625, 0.861577032153333, 0.869726724106667, 0.868963325383333
    ), X61 = c(0.872732233906667, 0.8671754757625, 0.861577032153333, 
    0.869726724106667, 0.869159403813333), X62 = c(0.872928312336667, 
    0.868842142429167, 0.862189777253333, 0.869726724106667, 
    0.869159403813333), X63 = c(0.873124390766667, 0.869258809095833, 
    0.862189777253333, 0.869922802536667, 0.869355482246667), 
    X64 = c(0.873124390766667, 0.869258809095833, 0.862189777253333, 
    0.869922802536667, 0.869355482246667), X65 = c(0.8735359134, 
    0.869258809095833, 0.86326504607, 0.869922802536667, 0.871218227346667
    ), X66 = c(0.8735359134, 0.869258809095833, 0.86326504607, 
    0.869922802536667, 0.871634894013333), X67 = c(0.8735359134, 
    0.869454887525833, 0.86326504607, 0.871172802536667, 0.872995438226667
    ), X68 = c(0.873928070263333, 0.870726234775833, 0.864098379403333, 
    0.871589469203333, 0.872995438226667), X69 = c(0.874124148696667, 
    0.87458377779, 0.864509902036667, 0.871589469203333, 0.87382877156
    ), X70 = c(0.87453567133, 0.87458377779, 0.86611726103, 0.871589469203333, 
    0.87382877156), X71 = c(0.875123906623333, 0.875000444456667, 
    0.86611726103, 0.871589469203333, 0.87382877156), X72 = c(0.875804178733333, 
    0.87539260132, 0.866730006126667, 0.871589469203333, 0.87450904367
    ), X73 = c(0.876000257163333, 0.87539260132, 0.86712216299, 
    0.87200613587, 0.87450904367), X74 = c(0.876196335596667, 
    0.87539260132, 0.868589588673333, 0.874150993813333, 0.87538539421
    ), X75 = c(0.876196335596667, 0.87539260132, 0.86900625534, 
    0.874347072243333, 0.87538539421), X76 = c(0.876196335596667, 
    0.875804123953333, 0.86900625534, 0.875597072243333, 0.875777551073333
    ), X77 = c(0.876196335596667, 0.876637457286667, 0.870761796266667, 
    0.875597072243333, 0.87660059634), X78 = c(0.877005159126667, 
    0.876637457286667, 0.870761796266667, 0.878930405576667, 
    0.877017263006667), X79 = c(0.877005159126667, 0.876637457286667, 
    0.870761796266667, 0.878930405576667, 0.877017263006667), 
    X80 = c(0.877005159126667, 0.876637457286667, 0.874487286463333, 
    0.879959817343333, 0.877017263006667), X81 = c(0.877005159126667, 
    0.876637457286667, 0.87490395313, 0.880793150676667, 0.877017263006667
    ), X82 = c(0.877612760193333, 0.877250202386667, 0.87490395313, 
    0.881262634243333, 0.877017263006667), X83 = c(0.877612760193333, 
    0.877446280816667, 0.875320619796667, 0.881262634243333, 
    0.878092531823333), X84 = c(0.877808838623333, 0.877446280816667, 
    0.87652160019, 0.88167930091, 0.878288610253333), X85 = c(0.877808838623333, 
    0.877446280816667, 0.876717678623333, 0.881875379343333, 
    0.87870527692), X86 = c(0.87822550529, 0.877446280816667, 
    0.87733042372, 0.881875379343333, 0.879121943586667), X87 = c(0.878642171956667, 
    0.877446280816667, 0.87733042372, 0.881875379343333, 0.880482487803333
    ), X88 = c(0.878642171956667, 0.877446280816667, 0.87733042372, 
    0.88229204601, 0.880482487803333), X89 = c(0.878642171956667, 
    0.877446280816667, 0.87813924725, 0.882708712676667, 0.880482487803333
    ), X90 = c(0.879058838623333, 0.877915764383333, 0.87813924725, 
    0.882708712676667, 0.88087464467), X91 = c(0.879058838623333, 
    0.877915764383333, 0.87813924725, 0.882708712676667, 0.883350134866667
    ), X92 = c(0.879058838623333, 0.877915764383333, 0.87813924725, 
    0.88604069927, 0.883350134866667), X93 = c(0.879867662153333, 
    0.877915764383333, 0.87813924725, 0.886457365936667, 0.883350134866667
    ), X94 = c(0.88045589745, 0.877915764383333, 0.87813924725, 
    0.887539594603333, 0.883350134866667), X95 = c(0.88181644167, 
    0.877915764383333, 0.87813924725, 0.887735673033333, 0.8835462133
    ), X96 = c(0.882233108336667, 0.878307921246667, 0.87833532568, 
    0.887735673033333, 0.8835462133), X97 = c(0.882233108336667, 
    0.878307921246667, 0.87833532568, 0.887735673033333, 0.8835462133
    ), X98 = c(0.882233108336667, 0.879383190063333, 0.87833532568, 
    0.887735673033333, 0.8835462133), X99 = c(0.882233108336667, 
    0.879383190063333, 0.87833532568, 0.887735673033333, 0.88374229173
    ), X100 = c(0.882233108336667, 0.879383190063333, 0.87833532568, 
    0.887735673033333, 0.88374229173)), row.names = c(3L, 5L, 
7L, 2L, 1L), class = "data.frame")

如果您将数据框重塑为更长的格式并将 x 轴转换为数字格式,您可以执行以下操作:

library(tidyr)
library(dplyr)
D <- d %>% pivot_longer(-Technique, names_to = "X", values_to = "Values") %>% 
  mutate(Time = as.numeric(sub("X","",X)))

然后,您可以使用scale_x_continuousbreaks参数:

library(ggplot2)
ggplot(data=D, aes(x=Time, y=Values, group=Technique, color=Technique)) + 
  geom_line(size=1)+
  scale_x_continuous(breaks = seq(1, 100, by=2)) +
  expand_limits(x = 0) +
  theme(legend.position="top", 
        axis.text.x = element_text(angle = 60, hjust = 1),
        text = element_text(size=17),legend.title=element_blank())+
  scale_colour_discrete(name="Technique",
                        breaks=c("statements_0.01","predicates_0.01","predicates_binary_0.01","default","fitness_based"),
                        labels=c("Statement-based Sharing","Predicate-based Sharing","Predicate Binary-based Sharing","No Sharing","Fitness-based Sharing")) +
  geom_point(aes(shape=Technique, colour = Technique), show.legend = T,size=3) + 
  labs(x = "Generation", y = "Phenotypic Diversity")+
  scale_shape_discrete(labels=c("Statement-based Sharing","Predicate-based Sharing","Predicate Binary-based Sharing","No Sharing","Fitness-based Sharing"),
                       breaks=c("statements_0.01","predicates_0.01","predicates_binary_0.01","default","fitness_based")) +
  guides(shape=guide_legend(override.aes=list(size=3, linetype=0)))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM