I am using ggplot to chart some data and I noticed that legends text are very long and not fitting the window.
+ opts(legend.position = 'bottom', legend.direction = 'horizontal', size=0.1) +
guides(colour = guide_legend(nrow = 3), size=1)
Is there an option in ggplot to wrap the legend text to fit the window.
Not as far as I know, so I have resorted to a workaround using
strwidth(), which calculates the width of text in base graphics.Use
par("din")to get the width of the device, andstrwidth()to calculate the text size:Use it in a function and plot:
The plot:
If you want to save the plot to file, then you can replace
par("din")with the actual saved plot dimensions.