问:如何用ggplot结合两种类型的线?

问题描述 投票:1回答:2

我试图绘制以下图表。

enter image description here

这个图是在R中用命令绘制的,但是,我需要改变x轴。如你所见,x轴从0开始,到46结束。我希望X轴从1972年开始,到2018年结束。seq(1972, 2018). 本图使用的数据如下:

对于制度一

structure(c(0.996336942021931, 0.982749831853788, 0.25257000136794, 
0.707797489518183, 0.339372705184362, 0.999209103898399, 0.348786927897612, 
0.821500770877589, 0.569473419352121, 0.544946043345147, 0.15347485404411, 
0.987921203799956, 0.00247541125926418, 0.999925918450173, 0.996940249283586, 
0.0141234625702467, 0.105466117156579, 0.999992944275275, 0.991723355647765, 
0.0958472062267191, 0.0362729940372193, 0.999999790503447, 0.0750715811130157, 
0.999975836828039, 0.998991768987905, 0.327943641159186, 5.05723080618291e-05, 
0.999999999869691, 0.995538324405397, 0.123355227931813, 0.999776636825943, 
0.00875781169836433, 0.696284480883101, 0.854839147672286, 0.113243492249383, 
0.00984853715078062, 0.442061195271808, 0.999959859676686, 0.0249739384218217, 
0.715262186931097, 0.269481397703521, 0.708458897302807, 0.0444979324520481, 
0.000133950914911277, 0.997976154782607, 0.191386380576805, 0.99775339928206, 
0.97921531595208, 0.27690132186733, 0.671995422154737, 0.458800347851363, 
0.999155966774432, 0.417000082142666, 0.838969001100901, 0.576424593247709, 
0.439169303472056, 0.227227711549776, 0.978527102362448, 0.00408165810824898, 
0.999955057843957, 0.994643622809094, 0.00847570472458959, 0.163000467960203, 
0.999995704786608, 0.987482614312069, 0.0569007267419926, 0.0585312256476362, 
0.999999671060746, 0.118213072794827, 0.99998536150034, 0.998897081324845, 
0.212968271334585, 8.35316288758489e-05, 0.999999999920876, 0.993537683112221, 
0.188538497918178, 0.999604116439039, 0.00905848219612739, 0.769430430615986, 
0.794457999021984, 0.0665707154963958, 0.00776458004359329, 0.5668500474175, 
0.999931021995446, 0.0265573724408095, 0.661699294173752, 0.296009575623967, 
0.587638579198176, 0.0251758869152202, 0.000220356219397782, 
0.997352716237698, 0.191386380576805), .Dim = c(46L, 2L))

对于制度2。

structure(c(0.00366305797806813, 0.0172501681462116, 0.74742999863206, 
0.292202510481817, 0.660627294815638, 0.000790896101601132, 0.651213072102388, 
0.178499229122411, 0.430526580647879, 0.455053956654853, 0.846525145955889, 
0.0120787962000438, 0.997524588740736, 7.40815498269273e-05, 
0.00305975071641352, 0.985876537429753, 0.894533882843421, 7.05572472485335e-06, 
0.00827664435223535, 0.904152793773281, 0.963727005962781, 2.09496553467159e-07, 
0.924928418886985, 2.41631719608902e-05, 0.00100823101209502, 
0.672056358840815, 0.999949427691938, 1.30308744399533e-10, 0.00446167559460289, 
0.876644772068187, 0.00022336317405711, 0.991242188301636, 0.303715519116899, 
0.145160852327714, 0.886756507750617, 0.990151462849219, 0.557938804728191, 
4.01403233139628e-05, 0.975026061578178, 0.284737813068903, 0.730518602296479, 
0.291541102697193, 0.955502067547952, 0.999866049085089, 0.00202384521739295, 
0.808613619423195, 0.00224660071793958, 0.0207846840479196, 0.72309867813267, 
0.328004577845263, 0.541199652148637, 0.000844033225568314, 0.582999917857334, 
0.161030998899099, 0.423575406752291, 0.560830696527944, 0.772772288450224, 
0.0214728976375518, 0.995918341891751, 4.49421560426429e-05, 
0.00535637719090558, 0.99152429527541, 0.836999532039797, 4.29521339242403e-06, 
0.0125173856879312, 0.943099273258007, 0.941468774352364, 3.28939253926857e-07, 
0.881786927205173, 1.46384996596921e-05, 0.00110291867515508, 
0.787031728665414, 0.999916468371124, 7.91243531099699e-11, 0.00646231688777926, 
0.811461502081822, 0.00039588356096145, 0.990941517803873, 0.230569569384014, 
0.205542000978016, 0.933429284503604, 0.992235419956407, 0.4331499525825, 
6.89780045536876e-05, 0.973442627559191, 0.338300705826248, 0.703990424376033, 
0.412361420801824, 0.97482411308478, 0.999779643780602, 0.00264728376230197, 
0.808613619423195), .Dim = c(46L, 2L))

我知道,红线可以绘制使用 geom_line 但我不知道如何才能绘制黑条,也许使用 geom_bar我如何才能合并这些地块?

谢谢你的帮助

r ggplot2 geom-bar
2个回答
1
投票

对于一个 ggplot2 解决方案,你将需要一个有4列的data.frame或tibble(Regime, Year, SmoothedFitted). 根据你提供的数据,这将有92行。

现在假设你使用这些列名(并将数据存储到变量 example.dat), a ggplot2 解决办法是

example.dat %>%
  ggplot( aes(x=Year) ) +
  geom_line( aes(y=Smoothed), color="red" ) +
  geom_linerange( aes(ymax=Fitted), ymin=0 ) +
  facet_wrap( ~ Regime, ncol=1 )

那么你可能需要调整一些尺度来得到最好的图。


3
投票

它实际上是用base R(好时光)绘制的,使用你的第一个For regime one的数据。

plot(Regime1[,1],type="h",xaxt="n",ylab="",cex.axis=0.6,xlab="",xlim=c(0,46))
lines(Regime1[,2],col="red")
mtext("Smoothed Probabilities",2,padj=-5,col="red",cex=0.7)
mtext("Fitted Probabilities",4,padj=1,cex=0.7)
axis(side=1,at=c(0,20,46),labels=c(1972,1992,2018))

enter image description here

你的x轴值实际上是0: 46,所以你用xaxt="n "关闭x轴刻度,然后用xaxt="n"。axis()你把它放在0,20,46,标签是1972... ...

这也取决于你的绘图设备,所以可能需要改变你的 padj 的参数来调整轴的标签。我想你可以查看 这样发 为基础R绘图函数。

在ggplot2中,我想你只需要创建一个data.frame,将Index作为你需要的年份,然后调用 geom_segment() 绘制垂直线 。

library(ggplot2)
Regime1 = data.frame(Regime1)
colnames(Regime1) = c("Fitted","Smoothed")
Regime1$index = 1:nrow(Regime1)+1972

ggplot(Regime1,aes(x=index))+
geom_segment(aes(xend=index,y=0,yend=Fitted,col="Fitted")) + 
geom_line(aes(y=Smoothed,col="Smoothed")) + theme_minimal() +
scale_color_manual(values=c("black","red"))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.