Granig verge ggplot2

I use facet_wrap to build some data. Here is an example:

 library (ggplot2) library (reshape) # generate some dummy data x = seq(0,1,0.05) precision = sqrt(x) recall = 1 - precision fmeasure = 2 * (precision * recall) / (precision + recall) # prepare it for plotting df = data.frame(x=x, precision=precision, recall=recall, fmeasure=fmeasure) df = melt(df, id.vars=c(x)) # plot it p = ggplot(df, aes(x=x, y=value, group=variable)) p = p + geom_line() + facet_wrap(~variable, ncol=3) p = p + coord_cartesian(xlim=c(0,1), ylim=c(0,1)) # second plot is without this line print (p) 

Figure 1: Plot for the code above. Plot with xlim and ylim

However, what you see in Figure 1 is that the first and last labels of subsequent faces overlap. This could be fixed by increasing the space between the faces. Another option is to remove the xlim and ylim , as shown in Figure 2, but this saves unnecessary space in the cell itself.

Figure 2: The graph with the line p = p + coord_cartesian(xlim=c(0,1), ylim=c(0,1)) deleted. Plot without xlim and ylim

I tried to increase the space between the faces, but so far I have not been able to do this. Do you have any tips?

I am using ggplot2 version 0.9.1.

+4
source share
1 answer

for 0.9.1 use: p + opts(panel.margin = unit(2, "lines")) , but you have a lot of extra space and IMO will lose part of the cut effect (note 0.9.2 now uses theme instead of opts )

The ggplot2 API has changed over the years, as of 2018-02-01 this is an updated solution:

 p + theme(panel.spacing = unit(2, "lines")) 
+3
source

Source: https://habr.com/ru/post/1437404/


All Articles