【问题标题】:scale of the y axis on ggplot for geom_line is bunched upgeom_line 的 ggplot 上 y 轴的比例被聚集在一起
【发布时间】:2017-07-17 16:54:04
【问题描述】:

我正在使用对冲基金数据为金融计量经济学课程的作业制作一个 R 降价文件。我的作业将于周二到期,但我的数字在 pdf_document 中的呈现方式存在一些问题。

```{r Q4}
library(dplyr)
library(ggplot2)
library(scales)
hedgefunds.long <- hedgefunds.long %>% group_by(Strategy) %>% mutate(RET = (log(NAV)- lag(log(NAV))) * 100)

ggplot(hedgefunds.long) + geom_line(aes(x = date, y = RET)) + scale_y_continuous(breaks=pretty_breaks(n=6)) + facet_wrap(~ Strategy, ncol = 2)

我遇到的另一个问题是,在编织 pdf 文档时,位被切断了。我想知道是否有办法确保下图中的图例不会在顶部和底部被切断。

watch the labels on the axes

编辑:(更改了 corrplot 一点,仍然没有弄清楚调整大小)

corrplot(hedgefundcormatrix, method = "color",addgrid.col = "gray50", tl.cex = 0.8,tl.offset = 0.5, tl.col = "black")

关注每个数据集,

    > head(hedgefunds, 10)
# A tibble: 10 × 15
         date `Hedge Fund Index` `Convertible Arbitrage` `Dedicated Short Bias`
       <date>              <dbl>                   <dbl>                  <dbl>
1  1993-12-31             100.00                  100.00                 100.00
2  1994-01-31             101.14                  100.36                  98.40
3  1994-02-28              97.00                  100.51                 100.37
4  1994-03-31              93.54                   99.54                 107.59
5  1994-04-30              91.91                   97.03                 108.97
6  1994-05-31              93.96                   96.04                 111.42
7  1994-06-30              93.20                   96.24                 118.49
8  1994-07-31              93.53                   96.37                 117.09
9  1994-08-31              96.12                   96.33                 110.46
10 1994-09-30              96.76                   95.18                 112.20
# ... with 11 more variables: `Emerging Markets` <dbl>, `Equity Market
#   Neutral` <dbl>, `Event Driven` <dbl>, `Event Driven Distressed` <dbl>, `Event
#   Driven Multi-Strategy` <dbl>, `Event Driven Risk Arbitrage` <dbl>, `Fixed
#   Income Arbitrage` <dbl>, `Global Macro` <dbl>, `Long/Short Equity` <dbl>,
#   `Managed Futures` <dbl>, `Multi-Strategy` <dbl>



 head(hedgefunds.long, 10)
Source: local data frame [10 x 4]
Groups: Strategy [1]

         date         Strategy    NAV        RET
       <date>            <chr>  <dbl>      <dbl>
1  1993-12-31 Hedge Fund Index 100.00         NA
2  1994-01-31 Hedge Fund Index 101.14  1.1335510
3  1994-02-28 Hedge Fund Index  97.00 -4.1794717
4  1994-03-31 Hedge Fund Index  93.54 -3.6321826
5  1994-04-30 Hedge Fund Index  91.91 -1.7579315
6  1994-05-31 Hedge Fund Index  93.96  2.2059322
7  1994-06-30 Hedge Fund Index  93.20 -0.8121438
8  1994-07-31 Hedge Fund Index  93.53  0.3534519
9  1994-08-31 Hedge Fund Index  96.12  2.7315170
10 1994-09-30 Hedge Fund Index  96.76  0.6636275

library(tidyr)
hedgefunds.long <- tidyr::gather(hedgefunds, Strategy, NAV, -date)

将尝试这个,看看它是否有效...如果有效,将编辑帖子。 R - change size of axis labels for corrplot

【问题讨论】:

  • SO 不是家庭作业服务。请展示一个可重现的示例,并为解决您自己的问题付出一些努力。
  • 我写了这段代码,刚刚开始学习这门课。我尝试使用 scale_y_continuous。离散的,然后尝试使用包装秤,因为我看到这可能是一个可能的解决方案,但这也不起作用。我想出了如何处理矩阵中的 na 元素以确保 corrplot 显示正确的成对相关性,并确保通过手动执行一对来检查所找到的相关性是否正确。我刚开始上这门课,并想出了如何完成这项作业的其他 5 页。这些是唯一剩下的几个问题。

标签: r ggplot2 knitr finance r-corrplot


【解决方案1】:

我没有可重复的数据来检查,但在我看来,图形尺寸太小,迫使你的图形堆积起来。

玩弄你的图形窗口大小,特别是 Q4 中的高度,还可以考虑 facet_wrap(~ Strategy, ncol = 2) 中的 ncol &gt; 2;以及 Q8 的宽度和高度。

fig.width和fig.height可以加```{r fig.width=x, fig.height=y}

{r Q4, fig.width=7, fig.height=10} # plot()

如果图形大小不能解决问题,您可以设置边距

# ggplot margins 
gg + theme(plot.margin = unit(c(t,r,b,l), "cm")) # replace t,r,b,l with appropriate values
# plot margins
par(mar=c(b,l,t,r)) # replace b,l,t,r with appropriate values
plot()

【讨论】:

  • 会尽快尝试并回复您。感谢您的洞察力。编辑代码
  • 在设置代码块中为页面宽度和长度创建变量,尝试编写 >``{r Q4 fig.width = paperwidth, fig.height=paperlength} .... 但出现错误在针织中……嗯……
  • 你在 Q4 和 fig.width 之间缺少逗号 ```{r Q4, fig.width=7, fig.height=10}
  • 对于 corrplot 也可以添加 tl.cex 参数来减小字体大小,默认为 1 corrplot(..., tl.cex=0.5)
  • 是的,一切都解决了。没有意识到我可以在一开始就指定尺寸。想出改变标签并在顶部放置一个灰色网格。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多