Mark Miller
Mark Miller

Reputation: 13123

Crop lines in R plot with InkScape

I have created some plots in R using the following code. The data files are pasted below. I have not been able to create a picture file to upload here, but the figure can be recreated with this code, the provided data and InkScape instructions below. I am using Windows.

c.low <- read.csv('c.low.csv', header = TRUE, stringsAsFactors = FALSE, na.strings = "NA")
e.high <- read.csv('e.high.csv', header = TRUE, stringsAsFactors = FALSE, na.strings = "NA")

my.plot <- 'small_plot_with_inkscape_to_crop_lines.svg'

library(Cairo)
CairoSVG(my.plot, onefile = TRUE, pointsize = 12, width=6, height=9, bg="white")
par(mfrow=c(3,2), oma=c(0.5,0.5,0.5,0.5), cex.lab=1.0, asp=1)

plot(c.low$TT, c.low$point, xlab = 'TT', ylab = 'C', type = 'l', col = 'green', lwd = 2, lty = 1,
     ylim = c(0, (max(c.low$point)+0.1)), cex.lab = 1.5)
     lines(c.low$TT,  c.low$lower95CI, type = 'l', col = 'brown', lwd = 2, lty = 3)
     lines(c.low$TT,  c.low$upper95CI, type = 'l', col = 'brown', lwd = 2, lty = 3)

plot(e.high$TT, e.high$point, xlab = 'TT', ylab = 'E', type = 'l', col = 'green', lwd = 2, lty = 1,
     ylim = c(0, (max(e.high$point)+0.1)), cex.lab = 1.5)
     lines(e.high$TT, e.high$lower95CI, type = 'l', col =  'blue', lwd = 2, lty = 3)
     lines(e.high$TT, e.high$upper95CI, type = 'l', col =  'blue', lwd = 2, lty = 3)

dev.off()

I then open the svg file with InkScape and save the file as an emf file. When saving the emf file I select Convert texts to paths in the EMF Output window.

The problem is that some of the lines in the emf file are extending beyond the plot frame when I open the emf file in MSWord. These lines are not visible outside the plot frame when viewed in R or in InkScape.

How can I crop the lines in the emf file or otherwise prevent them from being visible outside the plot frame when viewed in MSWord?

I have located a similar question here:

https://bugs.launchpad.net/inkscape/+bug/1405292

Here are the contents of the c.low.csv file:

point,lower95CI,upper95CI,TT
0.5082851,0.363055,0.6521302,-3.99994974874372
0.5087905,0.3633061,0.6528011,-3.95356783919598
0.5092959,0.363551,0.6534776,-3.90718592964824
0.5098013,0.3637897,0.6541595,-3.8608040201005
0.5103066,0.3640223,0.6548468,-3.81442211055276
0.5108119,0.3642487,0.6555395,-3.76804020100503
0.5113172,0.3644691,0.6562376,-3.72165829145729
0.5118225,0.3646834,0.6569409,-3.67527638190955
0.5123277,0.3648916,0.6576495,-3.62889447236181
0.512833,0.3650939,0.6583633,-3.58251256281407
0.5133382,0.3652902,0.6590821,-3.53613065326633
0.5138433,0.3654805,0.6598061,-3.48974874371859
0.5143485,0.3656649,0.660535,-3.44336683417086
0.5148536,0.3658434,0.6612689,-3.39698492462312
0.5153587,0.3660161,0.6620078,-3.35060301507538
0.5158637,0.366183,0.6627514,-3.30422110552764
0.5163688,0.3663441,0.6634999,-3.2578391959799
0.5168738,0.3664994,0.664253,-3.21145728643216
0.5173787,0.366649,0.6650109,-3.16507537688442
0.5178836,0.3667929,0.6657734,-3.11869346733668
0.5183885,0.3669312,0.6665404,-3.07231155778895
0.5188934,0.3670639,0.6673119,-3.02592964824121
0.5193982,0.3671911,0.6680879,-2.97954773869347
0.5199029,0.3673126,0.6688682,-2.93316582914573
0.5204077,0.3674287,0.6696529,-2.88678391959799
0.5209123,0.3675394,0.6704418,-2.84040201005025
0.521417,0.3676446,0.6712349,-2.79402010050251
0.5219216,0.3677444,0.6720322,-2.74763819095477
0.5224261,0.3678389,0.6728335,-2.70125628140704
0.5229306,0.3679281,0.6736388,-2.6548743718593
0.5234351,0.3680121,0.6744481,-2.60849246231156
0.5239395,0.3680908,0.6752613,-2.56211055276382
0.5244439,0.3681643,0.6760783,-2.51572864321608
0.5249482,0.3682327,0.6768991,-2.46934673366834
0.5254524,0.368296,0.6777235,-2.4229648241206
0.5259567,0.3683542,0.6785517,-2.37658291457286
0.5264608,0.3684074,0.6793834,-2.33020100502513
0.5269649,0.3684557,0.6802186,-2.28381909547739
0.527469,0.368499,0.6810573,-2.23743718592965
0.527973,0.3685374,0.6818994,-2.19105527638191
0.5284769,0.3685709,0.6827448,-2.14467336683417
0.5289808,0.3685996,0.6835935,-2.09829145728643
0.5294846,0.3686236,0.6844454,-2.05190954773869
0.5299884,0.3686428,0.6853004,-2.00552763819096
0.5304921,0.3686573,0.6861585,-1.95914572864322
0.5309957,0.3686672,0.6870197,-1.91276381909548
0.5314993,0.3686725,0.6878838,-1.86638190954774
0.5320028,0.3686732,0.6887508,-1.82

Here are the contents of the e.high.csv file:

point,lower95CI,upper95CI,TT
0.2048022,0.1050293,0.3611121,-11.05
0.2021779,0.1039115,0.356411,-11.0036180904523
0.1995788,0.1028025,0.3517418,-10.9572361809045
0.1970049,0.1017022,0.3471054,-10.9108542713568
0.1944561,0.1006105,0.3425023,-10.864472361809
0.1919324,0.09952735,0.3379333,-10.8180904522613
0.1894337,0.09845275,0.3333989,-10.7717085427136
0.1869601,0.09738664,0.3288999,-10.7253266331658
0.1845114,0.09632895,0.3244367,-10.6789447236181
0.1820876,0.09527966,0.32001,-10.6325628140704
0.1796886,0.09423869,0.3156203,-10.5861809045226
0.1773143,0.09320602,0.3112681,-10.5397989949749
0.1749648,0.09218158,0.3069539,-10.4934170854271
0.1726398,0.09116533,0.3026782,-10.4470351758794
0.1703394,0.09015722,0.2984413,-10.4006532663317
0.1680634,0.0891572,0.2942438,-10.3542713567839
0.1658117,0.08816522,0.2900859,-10.3078894472362
0.1635842,0.08718124,0.2859681,-10.2615075376884
0.1613809,0.0862052,0.2818907,-10.2151256281407
0.1592016,0.08523705,0.2778539,-10.168743718593
0.1570463,0.08427675,0.2738582,-10.1223618090452
0.1549147,0.08332425,0.2699036,-10.0759798994975
0.1528068,0.0823795,0.2659906,-10.0295979899497

Upvotes: 1

Views: 103

Answers (1)

Mark Miller
Mark Miller

Reputation: 13123

Apparently the problem described in the original post is related to a long-time bug. I have been able to jerry-rig a solution by cropping lines within R. Below I use grconvertY to identify the position of the top border, then use an ifelse statement to set elements above that value to NA.

People commonly suggest using "usr" to identify the position of the top border, but I could not get that option to work given that I am creating two panels in the figure.

The post-processed figure is shown below (after format conversion within InkScape and importation to PowerPoint [then saved as a jpeg for Stack Overflow]).

T   <- 1:20
x   <- 5 + 0.5 * T
x.u <- 6 + 0.5 * T
x.l <- 4 + 0.5 * T

T2   <- 20:40
x2   <- 40 + 2 * T2
x.u2 <- 60 + 2 * T2
x.l2 <- 20 + 2 * T2

library(Cairo)
CairoSVG('my.plot.svg', onefile = TRUE, pointsize = 12, width=6, height=9, bg="white")

par(mfrow=c(2,1), xpd=NA)

plot(T, x, xlab = 'x', ylab = 'y', type = 'l', col = 'green', lwd = 2, lty = 1,
     ylim = c(0, (max(x)+0.1)), cex.lab = 1.5)

     top.y <- grconvertY(1, from='npc')
     x.u = ifelse(x.u >= top.y, NA, x.u)

     lines(T,  x.u, type = 'l', col = 'brown', lwd = 2, lty = 3)
     lines(T,  x.l, type = 'l', col = 'brown', lwd = 2, lty = 3)

plot(T2, x2, xlab = 'x2', ylab = 'y2', type = 'l', col = 'green', lwd = 2, lty = 1,
     ylim = c(0, (max(x2)+0.1)), cex.lab = 1.5)

     top.y <- grconvertY(1, from='npc')
     x.u2 = ifelse(x.u2 >= top.y, NA, x.u2)

     lines(T2,  x.u2, type = 'l', col = 'brown', lwd = 2, lty = 3)
     lines(T2,  x.l2, type = 'l', col = 'brown', lwd = 2, lty = 3)

dev.off()

enter image description here

Upvotes: 1

Related Questions