简体   繁体   English

ggplot上的geom_line的y轴比例已汇总

[英]scale of the y axis on ggplot for geom_line is bunched up

I am making an R markdown file using hedgefund data for an assignment for a financial econometrics class. 我正在使用对冲基金数据为金融计量经济学课程的作业制作R降价文件。 My assignment is due on Tuesday, but I am having a few problems with how my figures are being rendered in the pdf_document. 我的作业定于星期二进行,但我在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)

以上代码块的y轴成束的压缩图的视图

Another issue I am having is that bits are cut off when the pdf document is knitted. 我遇到的另一个问题是,在编织pdf文档时会截断位。 I was wondering if there was a way to make sure that the legend in the picture below is not cut off at the top and bottom. 我想知道是否有一种方法可以确保下面图片中的图例在顶部和底部没有被剪掉。

watch the labels on the axes 注意轴上的标签

edit: (changed corrplot a bit, still haven't figure out resizing) 编辑:(更改了Corrplot一点,仍然没有弄清楚调整大小)

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

head on each dataset, 放在每个数据集上

    > 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)

Will try this and see if it works...will edit post if it does. 尝试一下,看看是否有效...如果有效,将编辑帖子。 R - change size of axis labels for corrplot R-更改Corrplot的轴标签的大小

I don't have a reproducible data to check but it looks to me like the figure size is too small and forcing your figures to bunch up. 我没有可复制的数据要检查,但在我看来,数字太大了,迫使您的数字聚集在一起。

Play around with your figure window size, specifically the height in Q4, also consider ncol > 2 in facet_wrap(~ Strategy, ncol = 2) ; facet_wrap(~ Strategy, ncol = 2)一下您的图形窗口大小,特别是Q4中的高度,还要在facet_wrap(~ Strategy, ncol = 2)facet_wrap(~ Strategy, ncol = 2)考虑facet_wrap(~ Strategy, ncol = 2) ncol > 2 and both width and height for Q8. 以及Q8的宽度和高度

fig.width and fig.height can be added ```{r fig.width=x, fig.height=y} 可以添加fig.width和fig.height```{r fig.width = x,fig.height = y}

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

If figure size does not fix it, you could set your margins 如果图形大小不能解决问题,则可以设置边距

# 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()

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

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