在 scale_y_continuous 中指定限制和中断的问题

问题描述

我正在尝试为主要 y 轴和次要 y 轴指定限制和中断。即使我指定了限制和范围,它也不会反映在图中。

这是我的数据:

    structure(c(39.571,36.571,34,36.143,36,38.286,34.429,34.714,33,30.857,29.429,26.429,25.571,23.714,22,21.714,24.286,29.857,38,48.429,58.714,70.857,79,87,92,99.714,98.714,99.571,101.571,104.714,108.857,112.286,109,109.714,112.714,112.143,114.857,110.857,107.143,107.286,108.143,107.857,104,101.286,105.143,103.143,103.286,96,92.714,91.286,84,83.143,81.429,81.286,82.571,82.857,80.429,77.857,75.429,75.143,73.286,74.571,74.429,77,86.286,94.857,96.286,125.286,137.714,150.571,155,159.286,177.714,175.429,181.286,191.143,181.429,181.714,186.857,176.857,174,158.714,137.143,137.429,131.571,116.571,103.571,90.857,81.571,67.143,54.286,46.571,44,42.571,37,32.857,30,27,50779.714,56225.286,44278.714,45877.857,47517.571,54886.857,53798.714,41703.429,34562.286,46345.429,42748.571,37479,29665,28040.714,26415.857,29700,28627.286,26928.429,26814,24773,22764.143,22051.571,16954.286,15219.571,14114.571,12616,11874.143,11393.143,11292,11069.571,10708.714,10678.143,10497.714,10555.571,10725,10590.286,11400.714,11464.571,11619.571,11902.143,10061.857,12137.714,12106.429,11766.143,12201.143,12847.714,13207.143,15696.857,13885.286,14280.714,14310.143,13927,14401.286,15009.429,12961.571,12393.429,11985.714,11933.286,13572.714,13333.286,13202.714,13256,13780.714,13920.429,15253.429,15070.286,12243.429,15389.857,17777.143,18269.857,18231.571,18068,17882.286,20907.571,18093.286,15252.286,18318.143,18315.571,18917.714,19338.429,19521.143,19757,23133.286,20423.429,20495.143,20244.857,20255.429,17110.571,20420.714,20438,20552.143,20564.714,20735.286,20657.429,20560.857,20434,19940.143,20008.714,20002.286,19366.429,19262.429,22270.429,15850.143,12969.286,10271.857,18361.714,17048.571,16244.571,18427.143,18974,19465.143,19474.857,19754,17078.286,19794.571,18497.571,18808.571,18728.714,18802.714,17188.857,18340,18294.857,18484.286,18384.714,18432.143,18322.429,18488.714,18470.857,17062.143,18342.143,18043.571,17790.143,17786.286,17617.714,17584.286,18783.714,17439.143,17673.429,16980.714,18735.143,19837,18584.857,19227.286,19486.429,20792.571,22399.286,21780.143,22322.857,22465,23116.714,24210.286,23845.286,24647.143,24631,23450.286,25279.571,25612.143,25757,25226.714,20842.429,19674.571,20525.286,19775,17391.143,19574.857,18560.571,18557.286,18185.714,17893.857,17653.286,17493.714,14296.429,18005.857,20428.714,22131.714,20787.714,20700,21965.857,21809.143,18382,16884.286,15740.429,17245.571,16895.143,14755.571,15246.571,13380,14490.429,14149.429,13948.571,13610.571,13204,11294.429,12238.143,11733.143,12460,11127.143,11095.857,10853.429,10403.571,10139.143,9854,8584.571,9430.571,9142.286,8862,8399,7955.571,7721.571,7414.714,7153,7221.857,26971.286,27864.429,28658.714,29754.857,30715.286,31865.714,32252.714,32683.571,33662,34006.714,34503.143,34946,34580.286,34775,35072.571,34658.429,34846.857,34589.571,34066.286,33711.286,32905.571,32273.429,30992.857,29788.857,28763.857,27492.857,26285.857,25187.143,24251.286,23697,22962.286,22136.286,21531.714,20779.857,20528.571,20143.286,19499,18362.714,17474,16704.857,16540.571,16404.857,16168.571,16168.857,16856.429,17032.857,16557.857,16044,15639,15473.857,15256.286,14733.429,14705.714,15225.143,14382.286,13501.571,13174.714,12873.857,13201.571,13978.429,14431.714,14634.571,15392.857,15709.571,16304.571,16893.714,16170.714,15502,16666.429,17292.143,17539.571,17377.714,16727.286,16559.714,16361.857,15838.143,14969.429,14440.143,13905,13590,13134.571,12775.857,12350.286,12219.429,12181.429,12194,12425.857,12469.429,12459.429,12371.143,12317,12226.429,12093.143,11806.143,11705.571,11798.429,11958,11964.286,12101.571,12068,12279.571,12233.857,12245.857,12163,12073.714,20339.571,20375.429,21129.286,20890.429,20450.571,20074.429,17407.429,19043,18700.571,18537,16520.857,18400.714,15376,14907.714,14019.857,12250.286,12117.286,11389.286,10825.857,10211.143,9367.286,8800,8672.714,8363.857,8062.714,7734.429,6554.857,6618,6307.714,6561.143,7079.429,8554.857,8741.286,9337.857,9523,9623.571,9670.143,9857,9932.857,8245,8594.857,9085.571,9704.143,10473.429,11347.286,12720.286,10332.286,13805.571,17442.143,21848.286,22096.571,27649.286,26723.286,28829.143,32095.571,33360.286,33747,34958.286,35342.286,36704.429,37010.714,36826.286,35475.143,34794.143,32774.714,31721.714,30480.429,29774.857,28410.143,23754.286,21945.429,20019.429,18289.857,16292.143,13885.857,22754.429,22347.857,22418.143,22573.286,22416.714,22853.857,22470.857,22818,22871.286,22556.143,23892.286,24462.857,24733.857,25362,25364.571,25308.714,24829.429,23317.857,22311.857,21318.286,20416.857,19566.857,18321.714,18124,17355.571,16576.571,16000.857,15071.857,14638,14945.571,14656.429,14284.143,14507.143,14462.571,15206.571,15541.714,15383,15439.714,16313.286,17085,17938.571,18105.571,18906,19789.286,21027.286,23093.857,24061.857,24875.429,27381.571,29258.714,31881.857,33894.286,34410.857,35017,36094,35581,36715.714,39050.571,40600.857,43012.429,45963.143,49258.143,52495.857,54990.571,56106.571,57866.143,57403,59507.857,59828.571,59809.857,58003,55797.429,53669.714,53100.714,51342.571,48675.429,46338.857,45101,43359,42120.429,40573.714,38350.286,37239.429,36016.714,33821.571,31923.429,29977.286,28667.857,27071.286,25606.714,24332.286,23821.429,23355.571,22476.429,21322.571,19902.429,19182.857,18427.714,17780.571,17142.714,16265.857,15238.571,14666.286,13950.857,13249,12625.857,35721.1428,38597.5428,40037.2572,42225.7716,43869.8856,46015.0284,46602.9144,48622.9716,49383.6,51783.2,53799.6572,56796,59100.8,60304.7428,62707.2,64033.2572,65546.9144,67076.4572,68091.8856,68753.6572,69353.6572,70009.6,70672.8,71233.8284,66688.6284,67262.9716,65909.3716,65532.0572,64746.4,65458.9144,66555.3144,72879.3144,74252.4,77641.3144,79969.7144,81933.0284,84010.4572,85141.2,85618.1716,85997.3716,86092.9144,86472.0572,86389.7716,85551.0284,86927.0284,87409.7716,88116.1144,86699.0856,86705.9428,87017.7716,86334.9716,85364.7428,82740.9716,73923.6,75936.6856,74116.0572,72719.7716,72813.4856,72999.4856,75328.3428,78506.3428,82702.8,85723.8284,86273.3716,88253.1428,89504.6284,91951.6,99934.4572,97745.4284,98015.4284,99739.6,99323.1428,97949.0856,95562.6284,92629.0856,89131.3716,87081.2572,83003.6572,80189.1428,77486.7428,75091.3144,72195.6,70445.5428,67798.1716,68283.3144,66546.9144,64826.8572,63430.5716,62063.7716,60461.0856,59365.1428,58407.6572,56569.9428,54798,52201.8856,50341.6,48165.8284,46890.6856,44341.4284,43251.3144,41721.8856,40702.7428,38757.2,37574.6856,36166.5144,34118.2856
),.Dim = c(106L,7L),.Dimnames = list(NULL,c("China","France","Germany","Italy","Spain","United Kingdom","US")),index = structure(c(18568,18569,18570,18571,18572,18573,18574,18575,18576,18577,18578,18579,18580,18581,18582,18583,18584,18585,18586,18587,18588,18589,18590,18591,18592,18593,18594,18595,18596,18597,18598,18599,18600,18601,18602,18603,18604,18605,18606,18607,18608,18609,18610,18611,18612,18613,18614,18615,18616,18617,18618,18619,18620,18621,18622,18623,18624,18625,18626,18627,18628,18629,18630,18631,18632,18633,18634,18635,18636,18637,18638,18639,18640,18641,18642,18643,18644,18645,18646,18647,18648,18649,18650,18651,18652,18653,18654,18655,18656,18657,18658,18659,18660,18661,18662,18663,18664,18665,18666,18667,18668,18669,18670,18671,18672,18673),class = c("IDate","Date")),class = "zoo")

Ant 这是我的代码 (data=n) :

ggplot(aes(x = Index,y = Value,group = Series,colour = Series,linetype = Series),data = fortify(n,melt = TRUE)) + geom_line(linetype = "solid",size= 1) + xlab("") + ylab("")+
 scale_y_continuous(labels = scales::label_number(accuracy =1,breaks = seq(from = 0,to= 80000,by=20000)),expand = c(0,0),sec.axis = sec_axis(~ . * 2.5,labels = scales::label_number(accuracy =1),breaks= seq(from = 0,to = 250000,by=50000)))+ 
  scale_x_date(date_label="%b/%y",date_breaks  ="2 months")+  
            theme_classic()

这是结果

enter image description here

正如您在图中看到的,主 y 轴的限制从 25 000 到 75 000 ,而不是从 0 到 80 000 和 20 000(序列增量)。次要 y 轴发生同样的事情,次要 y 轴的限制从 40 000 到 240 000 和 40 000(序列增量)而不是从 0 到 250 000 和 50 000(序列增量) .

是否有另一种方法可以指定中断和限制,以便在我的绘图中显示?提前致谢。

解决方法

您可以使用 coord_cartesian 指定 y 轴范围并修改主 y 轴上的中断。

df <- n %>% fortify(melt = TRUE) %>% as_tibble()

  df %>% 
  ggplot(aes(x = Index,y = Value,group = Series,colour = Series,linetype = Series)) + 
  geom_line(linetype = "solid",size= 1) +
  xlab("") + ylab("") +
  scale_y_continuous( breaks = seq(from = 0,to = 100000,by = 20000),## Modified breaks instructions
                      labels = scales::label_number(accuracy = 1),sec.axis = sec_axis(~ . * 2.5,labels = scales::label_number(accuracy = 1),breaks= seq(from = 0,to = 250000,by = 50000))) +
  scale_x_date(date_label="%b/%y",date_breaks  ="2 months") +  
  coord_cartesian(ylim = c(0,100000)) + ## Here is what you need to add
  theme_classic()

enter image description here