У меня проблемы с использованием grid.edit()
из Rscript
. Я использую grid.edit()
, чтобы увеличить толщину полых точек в легенде и на диаграмме. Я взял это из этого сообщения (Изменить толщину маркера в ggplot2 ). Просто выглядит лучше ИМО. Я знаю, что из исходных файлов и Rscripts вы можете получить ggplot
объектов для построения с помощью print(p)
, но мне нужно использовать grid.edit()
, поэтому я не знаю, как это исправить. Рабочий пример ниже.
Мой R-скрипт под названием test.r
library(ggplot2)
library(grid)
library(gtable)
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point(aes(shape = factor(cyl))) + scale_shape(solid = FALSE)
lwd = 2 # Set line width
g = ggplotGrob(p); dev.off() # Get the plot grob
# Get the indices for the legend: t = top, r = right, ...
indices <- c(subset(g$layout, name == "guide-box", select = t:r))
# Get the row number of the legend in the layout
rn <- which(g$layout$name == "guide-box")
# Extract the legend
legend <- g$grobs[[rn]]
# Get the legend keys
pointGrobs = which(grepl("points", legend$grobs[[1]]$grobs))
# Check them out - no line width set
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]])
# Set line width
for (n in pointGrobs) legend$grobs[[1]]$grobs[[n]]$gp$lwd = lwd
# Check them out - line width set
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]])
# Put the modified legend back into the plot grob
g$layout$clip[g$layout$name == "panel"] <- "off"
g = gtable_add_grob(g, legend, t=indices$t, l=indices$l)
###g$grobs[[4]]$children[[2]]$gp$lwd = gpar(lwd = lwd) # Alternative for setting lwd for points in the plot
grid.newpage()
grid.draw(g)
grid.edit("geom_point.points", grep = TRUE, gp = gpar(lwd = lwd))
dev.print(cairo_pdf,filename="Aplot.pdf",
width=11,
height=8.5)
Мой командный файл.
...\R-3.2.3\bin\x64\Rscript.exe test.r
PAUSE
Скрипт запускается, и я получаю следующую ошибку.
Error in editDLfromGPath(gPath,specs,strict,grep,global,redraw):
'gPath' (geom_point.points) not found
Calls: grid.edit -> editDLfromGPath
Execution halted
Кроме того, PDF-файл печатается в моем рабочем каталоге с именем Rplots
. Этот график имеет размер по умолчанию, и, что интересно, точки в легенде толстые, а точки на графике — нет. Похоже, что сценарий терпит неудачу в grid.edit()
, но grid.draw()
завершается успешно.
source()
в начале сеанса R, чтобы получить тот же результат, или использованиеsource()
приведет к тем же проблемам? - person CCurtis   schedule 12.03.2016grid.force()
непосредственно перед строкойgrid.edit(....
.grid.force()
делает grobs видимыми для функций редактирования сетки. Безgrid.force()
функции редактирования видят только один гроб. - person Sandy Muspratt   schedule 12.03.2016