如何使用自动绘图(动物园对象)添加辅助 y 轴 [英] How to add a secondary y-axis using autoplot (zoo object)

查看:89
本文介绍了如何使用自动绘图(动物园对象)添加辅助 y 轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试向绘图中添加辅助 y 轴时遇到问题.事情就是这样,我有 6 个时间序列.在这两种情况下,y 尺度都是百分比.但是我想在我的图的左侧获得主要 y 轴,范围从 0% 到 0.80% ,在右侧(次要 y 轴)从 0% 到 2.4% 的范围缩放(仅针对一个系列)百分比ISRA").

I have trouble trying to add a secondary y-axis to my plot. Here is the thing, I have 6 time-series. In both cases the y-scale is in percentage. But I want to get the primary y axis in the left side of my plot with range scale from 0% to 0.80% , and in the right side(secondary y axis) a range scale from 0% to 2.4% (for just one serie "percentISRA").

这是我的数据

structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.000113454034966788, 
0.000193020234641067, 0.00028445645011757, 0.00038617294713707, 
0.000487915939727557, 0.000589658932318044, 0.000729396573704041, 
0.00074661869484566, 0.00149766215004617, 0.0015524549908475, 
0.00151377145720633, 0.00147508792356515, 0.00143640438992398, 
0.0013901166274095, 0.00153218587904237, 0.000772955292406844, 
0.000700092472192302, 0.000684168634029051, 0.000668244795865801, 
0.000652294462131563, 0.000601581939262242, 0.000411502713000743, 
0.000395181441272686, 0.0003967976711029, 0.00041876249945121, 
0.000440727327799521, 0.000462692156147832, 0.000504634645020421, 
NA, NA, NA, NA, NA, NA, NA, NA, 0.000236727931586416, 0.000377435080193467, 
0.000481619718431735, 0.000474768763872362, 0.000438198511659961, 
0.00046717781073689, 0.000443473985379896, 0.000493018083595849, 
0.000507877732422363, 0.000508713214685701, 0.000543696050597761, 
0.000604196901924348, 0.000613136562142067, 0.000626778793956288, 
0.0006552209972925, 0.000705958641598936, 0.000774468187192666, 
0.000845961598012603, 0.000919316940733693, 0.000931503046317525, 
0.000951685910707594, 0.000978421343134416, 0.00101577933576654, 
0.00107464502894916, 0.00106674375383016, 0.00108178243457025, 
0.00111024850882827, 0.00113987232279406, 0.00116272873042681, 
0.00115339519999924, 0.00109404015291951, 0.00109634369687414, 
0.00107495535093269, 0.00106611117440221, 0.00106180247301556, 
0.0011068588379313, 0.00116445937225801, 0.00121747668959727, 
0.00128044818133116, 0.00131713778815261, 0.00134916062976027, 
0.00134125935464127, 0.00127428948350427, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 0.0000160762588918086, 0.0000157123929498129, 
0.0000361881218675691, 0.000131454841226435, 0.000141659626963313, 
0.000224141420269331, 0.000275049573426725, 0.00043415822624483, 
0.000619531384330623, 0.000762249530397923, 0.000919208066286054, 
0.00111791194933769, 0.00123200046151433, 0.00129134368878162, 
0.00132012217692128, 0.00132384353314624, 0.00138947171759345, 
0.00139053023669744, 0.00137341199806265, 0.00131989062586728, 
0.00126269751552996, 0.00117098675878606, 0.00105474812967944, 
0.000904570731801228, 0.000768203700357852, 0.000621086084264604, 
0.000559956606009332, 0.000554118211576402, 0.000616868547209654, 
0.000729336202008315, 0.000825677979832168, 0.000962838900603534, 
0.00112948950203755, 0.00124946602673012, 0.0013096527614084, 
0.00134699863854596, 0.00136249601980278, 0.00142564330010002, 
0.00145018771182373, 0.00146029326139461, 0.00142476671396703, 
0.00137880382974858, 0.00132974808502317, 0.00126729545788791, 
0.00123551240400009, 0.00115785280276624, 0.00108017847095552, 
0.00100251886972166, 0.000924844537910937, 0.00084718493667708, 
0.000769510604866358, 0.000691851003632501, 0.000711840396438712, 
0.000731829789244923, 0.000751819182051134, 0.000771808574857345, 
0.000791797967663556, 0.000811787360469766, 0.000831791483852842, 
0.0010979730078088, 0.00136416926234162, 0.00163035078629757, 
0.00189654704083039, 0.00216272856478634, 0.00242892481931916, 
0.00269510634327512, 0.00265909008283946, 0.00274488096250296, 
0.00296737159547644, 0.00325984719913695, 0.00356562451370683, 
0.00377064468251806, 0.00386536229176194, 0.00395542503871637, 
0.00421468319154564, 0.00437931211859222, 0.00457010255015157, 
0.00488956457062932, 0.00533699111233591, 0.00532246676354672, 
0.00547625398602045, 0.00540439823207152, 0.00529452285923325, 
0.00530203545343455, 0.00532279083623776, 0.00556689122547263, 
0.00577970386944561, 0.0059295580278963, 0.00606456376486676, 
0.00639558928818376, 0.00653953648531146, 0.00655699221889684, 
0.00643590687706409, 0.00634680161760589, 0.00635385756392437, 
NA, NA, 0.00017495026255787, 0.000384987857816134, 0.000454999382527596, 
0.000577037681514372, 0.000650260660906437, 0.000699075980501147, 
0.000638602143867448, 0.000653006862510075, 0.000712033580806984, 
0.000771060299103892, 0.000842216829764506, 0.000913373360425121, 
0.000984529891085735, 0.00101796466902574, 0.00105140246808963, 
0.00102528485217824, 0.00108424208462599, 0.00114274916961616, 
0.00126866961278409, 0.00139353266259529, 0.00165145507129434, 
0.00190937747999338, 0.00193812347367724, 0.0021457169797195, 
0.00225702726782121, 0.00241299278793985, 0.00245215561674949, 
0.00249131542443526, 0.00253047825324491, 0.00275373326546238, 
0.00269606001066209, 0.00276104136411936, 0.00294722718637353, 
0.00319449711228442, 0.00339025083385511, 0.00340254378690815, 
0.00338081284286527, 0.00350767587668264, 0.00373208495822089, 
0.00378793043307596, 0.0039015277119521, 0.00400283505889909, 
0.00409498537937674, 0.00398782611548119, 0.00398167812839273, 
0.0038886305341238, 0.00385605375536468, 0.00408285858813691, 
0.00435371442815078, 0.00440014305988314, 0.00449945948619559, 
0.000156668463203005, 0.000343451492438752, 0.00054567679204072, 
0.000742148669581035, 0.000905468858662011, 0.000890497128390052, 
0.000861751406267889, 0.00115536842508712, 0.00148658587507507, 
0.00182199540953917, 0.00209217097638973, 0.00234893615055384, 
0.00244819872225693, 0.00256664649689423, 0.00270265397232193, 
0.00275109821384477, 0.00265600633843173, 0.0025053051791514, 
0.00237361811731647, 0.00235755558955327, 0.00227499719119646, 
0.00195404607053785, 0.0016190001352375, 0.00142287046867483, 
0.00144043016946523, 0.00146949671437893, 0.00147024530089253, 
0.00150311894293253, 0.00183354503003468, 0.00228594794248109, 
0.00274102437819036, 0.00307998435154752, 0.00341115902516327, 
0.00354085698569064, 0.003657443988137, 0.00383171492850261, 
0.00392481770260809, 0.0039073007781899, 0.00385519915684348, 
0.0037850245182402, 0.00376260969920446, 0.00371316021293479, 
0.00348075618274177, 0.00317208188090617, 0.00286075544399383, 
0.00257577925236013, 0.00232910930203654, 0.0022111320674935, 
0.00210592358005383, 0.00206524325008631, 0.00208237518715465, 
0.00217485770386315), .Dim = c(52L, 6L), .Dimnames = list(NULL, 
    c("percentCanada", "percentGer", "percentIta", "percentUK", 
    "percentUS", "percentISRA")), index = structure(c(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), class = c("IDate", 
"Date")), class = "zoo")

这是我的代码:

filledstarter <- na.fill(z, list(NA, "extend", NA))

autoplot.zoo(filledstarter, facets = NULL) + theme_classic()+ scale_y_continuous(labels = function(x) paste0(x*100), limits=c(0,0.024), expand = c(0, 0),
                                                                                   sec.axis = sec_axis(~ . /2))

这是我的输出:

这是我的 这是我想要的输出:正如您在这里看到的,比例是正确的.以色列在情节的右侧有一个次要轴.有没有办法使用 autoplot 做类似的事情?先感谢您.

This is my this is my desired output: As you can see here the scale is correct. Israel has a secondary axis on the right side of the plot. Is there a way to do something similar using autoplot? Thank you in advance.

推荐答案

以下是我为满足您的要求所做的工作.一些需要理解的原则:

Here is what I did to do what you want. Some principle to understand:

  • ggplot 中的第二个 Y 轴只是一个参考 - 图中的任何绘图都基于它与主 Y 轴的值.
  • 要绘制对第二个 Y 轴的参考,我们需要根据第二个 Y 轴和第二个 Y 轴之间的差异来调整它的值.主 Y 轴.在您的情况下,第二个 Y 轴比主 Y 轴大 3 倍,因此任何引用第二个 Y 轴的内容都需要除以 3.

希望这个解释足够清楚.如果需要更多指导,请随时发表评论或在消息中问我.

Hope this explaination is clear enough. If need more guidance feel free to comment or ask me in message.

library(scales)
library(zoo)
library(ggplot2)

# convert percentISRA to 3 times lower than its actual values.
data[, "percentISRA"] <- data[, "percentISRA"] / 3
# Draw the plot
autoplot.zoo(data, facets = NULL) +
  scale_y_continuous(labels = percent, expand = c(0, 0),
                     # Here the 2nd Y-Axis trans formula is main axis times 3.
                     sec.axis = sec_axis(~ . * 3, labels = percent)) +
  # I used coord_cartesian for setting the limits of main axis to between 0 - 0.8%
  coord_cartesian(ylim = c(0, 0.008)) + 
  # Added the X-Axis label format just to ensure it consistent in different system
  scale_x_date(date_labels = "%m/%Y") +
  theme_classic()

这是绘图输出

最后一点:这张图真的很容易混淆,一切都是线,它们是相同的单位.第二个 Y 轴仅在您在一个图上绘制两个不同的东西时效果最佳.尽管总体上第二个 Y 轴在很多情况下并不是很好的做法.

One last note: This graph really confuse with everything is line and they are same units. 2nd Y-Axis only work best in case you draw two different thing on one plot. Though overall 2nd Y-Axis is in many case not really good practice.

这篇关于如何使用自动绘图(动物园对象)添加辅助 y 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆