简体   繁体   English

使用ggplot和scale_x_discrete在x轴上显示一半的值

[英]Show half of the values on x-axis with ggplot and scale_x_discrete

I have a dataframe: 我有一个数据帧:

dput(df):

structure(list(Date = c("2008-11-28", "2008-12-31", "2009-01-30", 
"2009-02-27", "2009-03-31", "2009-04-30", "2009-05-29", "2009-06-30", 
"2009-07-31", "2009-08-31", "2009-09-30", "2009-10-30", "2009-11-30", 
"2009-12-31", "2010-01-29", "2010-02-26", "2010-03-31", "2010-04-30", 
"2010-05-31", "2010-06-30", "2010-07-30", "2010-08-31", "2010-09-30", 
"2010-10-29", "2010-11-30", "2010-12-31", "2011-01-31", "2011-02-28", 
"2011-03-31", "2011-04-29", "2011-05-31", "2011-06-30", "2011-07-29", 
"2011-08-31", "2011-09-30", "2011-10-31", "2011-11-30", "2011-12-30", 
"2012-01-31", "2012-02-29", "2012-03-30", "2012-04-30", "2012-05-31", 
"2012-06-29", "2012-07-31", "2012-08-31", "2012-09-28", "2012-10-31", 
"2012-11-30", "2012-12-31", "2013-01-31", "2013-02-28", "2013-03-29", 
"2013-04-30", "2013-05-31", "2013-06-28", "2013-07-31", "2013-08-30", 
"2013-09-30", "2013-10-31", "2013-11-29", "2008-11-28", "2008-12-31", 
"2009-01-30", "2009-02-27", "2009-03-31", "2009-04-30", "2009-05-29", 
"2009-06-30", "2009-07-31", "2009-08-31", "2009-09-30", "2009-10-30", 
"2009-11-30", "2009-12-31", "2010-01-29", "2010-02-26", "2010-03-31", 
"2010-04-30", "2010-05-31", "2010-06-30", "2010-07-30", "2010-08-31", 
"2010-09-30", "2010-10-29", "2010-11-30", "2010-12-31", "2011-01-31", 
"2011-02-28", "2011-03-31", "2011-04-29", "2011-05-31", "2011-06-30", 
"2011-07-29", "2011-08-31", "2011-09-30", "2011-10-31", "2011-11-30", 
"2011-12-30", "2012-01-31", "2012-02-29", "2012-03-30", "2012-04-30", 
"2012-05-31", "2012-06-29", "2012-07-31", "2012-08-31", "2012-09-28", 
"2012-10-31", "2012-11-30", "2012-12-31", "2013-01-31", "2013-02-28", 
"2013-03-29", "2013-04-30", "2013-05-31", "2013-06-28", "2013-07-31", 
"2013-08-30", "2013-09-30", "2013-10-31", "2013-11-29"), Variables = c("Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS HEDGNAV Index", "Korrelation Fond VS HEDGNAV Index", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX", "Korrelation Fond VS OMXN40 INDEX", 
"Korrelation Fond VS OMXN40 INDEX"), Value = c(0.543281082364437, 
0.527712775086864, 0.408808506706841, 0.429295911936136, 0.413869805084106, 
0.420679623586744, 0.486699140272165, 0.489646951704357, 0.482167209693269, 
0.476479729549409, 0.470318758286474, 0.445944951970354, 0.443399322259001, 
0.450016066763416, 0.426480550286595, 0.423371569969272, 0.45695040799057, 
0.431834238396871, 0.467511327603749, 0.468313995662446, 0.497800239085846, 
0.483892272485929, 0.461639835904741, 0.510817807699252, 0.607556423633874, 
0.635324016630525, 0.677123446245495, 0.64944374391215, 0.699596537117267, 
0.719111914210672, 0.646823640421409, 0.704957947272873, 0.711475445475265, 
0.586745307486821, 0.470189153059282, 0.496157837064293, 0.42998988517485, 
0.410086396037883, 0.434712079537519, 0.451209820436262, 0.439204849230259, 
0.458968925612488, 0.413432511226888, 0.352720907440874, 0.345869742666471, 
0.334881763959464, 0.364193270299568, 0.349099808200996, 0.344120170458358, 
0.321312279605764, 0.36780909986839, 0.45673262559758, 0.441745479560392, 
0.472657754486699, 0.460230404194416, 0.439767509233639, 0.451917786933249, 
0.502203230669925, 0.506491068871301, 0.326404602073815, 0.370217685029334, 
0.718957249305725, 0.692112042544696, 0.653190037272512, 0.600562794023062, 
0.577038569091474, 0.732404727204883, 0.701073092028334, 0.70024367927988, 
0.72760615710821, 0.760476468119078, 0.76494346961595, 0.74516226659605, 
0.739882810581542, 0.747631786072444, 0.726091015017154, 0.701735263628067, 
0.702742836045239, 0.710111045287843, 0.709534887923479, 0.660085062138124, 
0.68714771636425, 0.692372537364393, 0.653723068520449, 0.60219048444321, 
0.611238847460719, 0.615532490318692, 0.629099995821728, 0.696641041953563, 
0.699678945834638, 0.586283865406429, 0.624290876602801, 0.670695507419494, 
0.659774721518298, 0.71774709033755, 0.744569380783438, 0.816379470202153, 
0.837499193100422, 0.836198005977033, 0.84080909869627, 0.855061699261408, 
0.853368178821269, 0.851320045495211, 0.851777606811136, 0.814170256202011, 
0.815730122501554, 0.816104542127919, 0.791916561887601, 0.789829046533777, 
0.798421955468353, 0.768092583652463, 0.775993885935737, 0.782511954882627, 
0.792300623324247, 0.788792623730908, 0.777178875667974, 0.77530100406966, 
0.784668152948575, 0.723029746336548, 0.694308712557347, 0.657753481204441, 
0.656521122619321)), .Names = c("Date", "Variables", "Value"), row.names = c(NA, 
-122L), class = "data.frame")

I then use ggplot: 然后我用ggplot:

 CorrelationsBenchPlot<-ggplot(corres24mDfMelt, aes(x=Date,y=Value,group=Variables))+
  geom_line(aes(linetype=Variables,colour=Variables), size=1)+
  scale_y_continuous(breaks=seq(0.3,0.9,0.1),limits=c(0.3,0.9))+
  scale_x_discrete(breaks = corres24mDfMelt$Date[c(TRUE,FALSE)])+
  scale_colour_manual(values=c("black", "red", "green"))+theme_gdocs()+
  theme(axis.text.x  = element_text(angle=-90, hjust=0.5, size=8,colour="black",face="bold"),
        axis.text.y  = element_text(size=8,colour="black",face="bold"),
        legend.text = element_text(colour="black", size = 12, face = "bold"),
        legend.background = element_rect(fill="gray90", size=.5, linetype="dotted"), 
        plot.title = element_text(size = rel(1.6),face = "bold"),
        axis.title.y = element_blank(),
        axis.title.x=element_blank(),
        legend.title=element_blank(),
        legend.position=c(0.3, 0.11), 
        legend.position="none",
        legend.box="vertical")+
  labs(title = "2 Year Rolling Correlation\n")

So I want to show each second character value on x-axis by using scale_x_discrete(breaks = corres24mDfMelt$Date[c(TRUE,FALSE)]) , but it doesnt work and I get all the x-axis values. 所以我想通过使用scale_x_discrete(breaks = corres24mDfMelt$Date[c(TRUE,FALSE)])在x轴上显示每个第二个字符值,但它不起作用,我得到所有的x轴值。

What am I doing wrong? 我究竟做错了什么?

Best Regards! 最好的祝福!

The values in corres24mDfMelt$Date are not unique but duplicated (you have two values per date). corres24mDfMelt$Date中的值不是唯一的但是重复的(每个日期有两个值)。 Hence, the command corres24mDfMelt$Date[c(TRUE,FALSE)] still returns all possible values. 因此,命令corres24mDfMelt$Date[c(TRUE,FALSE)]仍然返回所有可能的值。

You have to use the unique dates as a basis: 您必须使用unique日期作为基础:

scale_x_discrete(breaks = unique(corres24mDfMelt$Date)[c(TRUE,FALSE)])

在此输入图像描述

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

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