5

I'm generating some graphs in markdown file, but the graph does not look right in the html file.

In RStudio, it works well, and looks like this:

Plot in R

But in the markdown html file, the legend box on the right side is cut off:

Plot in html

I tried adjusting fig.width, fig.align, fig.show='asis', fig.crop. They either didn't make any change, or just made the figure as a whole look narrower with the legend box still being incomplete.

Is there any way to make it look normal in markdown html file?

Dmitriy
  • 5,525
  • 12
  • 25
  • 38
LovelyVV
  • 75
  • 1
  • 5
  • Could you put up the output of par()$mai and par()$oma please? – andrnev Jan 28 '16 at 23:22
  • @andrnev yeah, here is the output: > par()$mai [1] 1.360000 1.093333 1.093333 0.560000 > par()$oma [1] 0 0 0 0 – LovelyVV Jan 29 '16 at 02:17
  • Could you try to place some outer margins by setting say par(oma=c(0,0,0,2))? Experiment by changing the last 2 to some higher values. This value will control the margin width on the right hand side of the plot. It will squeeze your graph though. – andrnev Jan 29 '16 at 14:47
  • @andrnev Thanks! I just tried as you suggested, but the plot only changed in RStudio not in the markdown file. I guess it has something to do with markdown setting, but the fig. options I have tried did not work. Will keep looking for solutions. – LovelyVV Jan 29 '16 at 15:55
  • Strange. I do see a plot change in the html too. What process are you using to create the html? Is it knitr? – andrnev Jan 29 '16 at 18:29
  • @andrnev I still haven't found out the solution, but thanks for your previous help. I'll let you know once I figure it out. :) – LovelyVV Feb 02 '16 at 04:42
  • Did you found any solutions? – Kush Patel Mar 06 '19 at 16:08
  • Unfortunately, no :( Did you come across the same problem? – LovelyVV Apr 11 '19 at 16:10

1 Answers1

2

I had this issue too in Rmarkdown html reports, I solved it by changing the plot margins in ggplot2: theme(plot.margin=unit(c(1,2,1,1),"cm")). The second 'unit' number was increased from 1 to 2 to allow for the legend on the right. I found this answer here: ggplot2: Exclude legend from aspect ratio

LeeRoyAus
  • 21
  • 2