Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R markdown figure legend is cut off

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?

like image 202
LovelyVV Avatar asked Oct 31 '22 10:10

LovelyVV


1 Answers

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

like image 126
LeeRoyAus Avatar answered Nov 15 '22 07:11

LeeRoyAus