出于某种原因,当我将ggplot2对象渲染到plotly中时,它会改变绘图的尺寸。对于某些情节来说这没什么大不了的,但对于地图来说,它会导致大规模的扭曲。我知道在使用ggplotly()
时通过宽度和高度选项更改尺寸的唯一方法。但是,我需要添加覆盖绘图默认选择的其他选项,因此我使用plotly_build()
。渲染后,如何切换尺寸?这是一些可重现的代码:
#Load dependencies
library(rgeos)
library(stringr)
library(rgdal)
library(maptools)
library(ggplot2)
library(plotly)
#Function to download and import shapefile
dlshape=function(shploc, shpfile) {
temp=tempfile()
download.file(shploc, temp)
unzip(temp)
shp.data <- sapply(".", function(f) {
fp <- file.path(temp, f)
return(readOGR(".",shpfile))
})
}
shape <- dlshape(shploc="http://biogeo.ucdavis.edu/data/gadm2.8/shp/IRN_adm_shp.zip",
"IRN_adm1")[[1]]
shape.ft <- fortify(shape, region="ID_1")
data<-merge(shape.ft, shape, region="id", by.x = "id", by.y = "ID_1")
gg<-ggplot(data, aes(x = long, y = lat, fill = example1, group = group, label = example2, label2=example3)) +
geom_polygon() +
coord_equal() +
scale_fill_gradient(low = "lightgrey", high = "darkred", name='Index') +xlab("")+ylab("") +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank()) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))
ggpotly(gg, width = 1000, height = 750)
虽然有效但我需要添加自定义工具提示。因此,我需要依赖以下内容:
plotly_build(gg)
另一方面,正如您所看到的那样具有与前一代码相同的尺寸,如果以前的代码没有包括宽度和高度选项。如何使用plotly_build切换宽度和高度选项?提前谢谢!