假设我们有两个无缝边界的shapefile.只是,他们没有.有没有办法迫使他们无间隙地相互粘连?
我有两个shapefile:一个用于欧洲地区 - REG
另一个用于邻国 - NEI
.两个shapefile都是从Eurostat存储库中获取的,应该很好地结合在一起; 但是差距很小.此外,我需要简化多边形,然后差距变得非常显着.
我尝试了几种方法但没有成功.我看到实现所需结果的唯一方法需要执行以下步骤:
NEI
;NEI
用REG
shapefile 剪掉扩展.显然,这是一个相当笨拙的解决方法.
# install dev version of ggplot2
devtools::dev_mode()
devtools::install_github("tidyverse/ggplot2")
library(tidyverse)
library(sf)
library(rmapshaper)
library(ggthemes)
# load data
source(file = url("https://gist.githubusercontent.com/ikashnitsky/4b92f6b9f4bcbd8b2190fb0796fd1ec0/raw/1e281b7bb8ec74c9c9989fe50a87b6021ddbad03/minimal-data.R"))
# test how good they fit together
ggplot() +
geom_sf(data = REG, color = "black", size = .2, fill = NA) +
geom_sf(data = NEI, color = "red", size = .2, …
Run Code Online (Sandbox Code Playgroud) 我正试图通过使用cowplot
包来以出版物的方式安排绘图.
我只是希望面板的尺寸和尺寸相同.
library(ggplot2)
library(cowplot)
gg1 <- ggplot(mtcars)+
geom_point(aes(x=mpg,y=hp))+
theme_bw()+
theme(aspect.ratio=1)
gg2 <- ggplot(mtcars)+
geom_point(aes(x=mpg,y=hp,fill=cyl))+
facet_wrap(~cyl,ncol=2)+
theme_bw()+
theme(aspect.ratio=1,
legend.position='none')
output <- plot_grid(gg1,gg2, labels = c('A','B'),label_size = 20)
print(output)
Run Code Online (Sandbox Code Playgroud)
如您所见,水平轴既不匹配,也不匹配面板的上边缘.
这个论点align
从cowplot
不方位图工作.
有任何想法吗?
我有6个图,我想以两步的方式整齐地排列(见图).最好,我想添加漂亮的箭头.
有任何想法吗?
UPD.当我的问题开始收集负面反馈时,我想澄清一下,我已经检查了所有(部分)相关的问题,并没有发现如何在"画布"上自由定位ggplots.而且,我想不出在绘图之间绘制箭头的单一方法.我不是要求现成的解决方案.请指出方向.
我正在使用俄语Win 8.1的机器上工作.
安装RStudio时我选择英语作为默认语言,但在处理日期(POSIXt)时,plot()
在时间轴上给我一个带有俄文标签的输出.
我该如何解决?我想要英语.
提前致谢!
ggplot
挑战我使用具有固定色标的地图在两个时刻比较变量的空间分布 - 以显示实际变化.将图例条附近的变量分布添加为抖动点会非常好.
所需的绘图应该看起来像图片:假设的红色抖动点是手动添加(我只是使用paint.net)到生成的图R
.
要重现地图,需要R
调用一个对象fortIT
.这是ggplot2::fortify
附加数据的意大利NUTS-2区域的强化(使用)SpatialPolygonsDataFrame.该RDATA文件可以下载这里 [89KB]
以及地图的代码:
require(dplyr)
require(ggplot2)
require(ggthemes)
require(gridExtra)
require(rgeos)
require(maptools)
require(cowplot)
require(viridis)
# load the data
load(url("https://ikashnitsky.github.io/share/1602-so-q-map-jitter/fortIT.RData"))
# produce the first map
gIT1 <- ggplot()+
geom_polygon(data = fortIT, aes(x=long, y=lat, group=group, fill=tsr03),
color='grey30',size=.1)+
scale_fill_viridis('TSR\n2003',limits=range(fortIT[,9:10]))+ # !!! limits fix the color scale
coord_equal(xlim=c(4000000, 5500000), ylim=c(1500000,3000000))+
guides(fill = guide_colorbar(barwidth = 1.5, barheight = 15))+
theme_map()+
theme(panel.border=element_rect(color = 'black',size=.5,fill = NA),
legend.position = …
Run Code Online (Sandbox Code Playgroud) 考虑一个字符向量
test <- c('ab12','cd3','ef','gh03')
Run Code Online (Sandbox Code Playgroud)
我需要test
包含4个字符(nchar(test[i])==4
)的所有元素.如果元素的实际长度小于4,则剩余的位置应填充零.所以,结果应该是这样的
> 'ab12','cd30','ef00','gh03'
Run Code Online (Sandbox Code Playgroud)
我的问题与此类似.但是,我需要使用角色向量.
我希望收到一个具有相等坐标的方形图,无论我的数据点云的形状如何。
这是我的问题的原始说明。
xy <- data.frame(x = rnorm(100),
y = rnorm(100, mean = 1, sd = 2))
gg <- ggplot(xy,aes(x = x, y = y))+
geom_point()
gg + coord_equal() #now we have square coordinate grid
gg + theme(aspect.ratio = 1) #now the plot is square
# if we use both, aspect.ratio overpowers coord_equal
gg + coord_equal() + theme(aspect.ratio = 1)
Run Code Online (Sandbox Code Playgroud)
有什么办法可以让绘图和坐标网格都平方吗?当然,剧情中也会有一些空白的地方。我不介意这个。
另外,我想知道确保 x 轴和 y 轴上的相同标签的最简单方法。
我需要一个十进制变量ceiling
和floor
函数.类似于signif
选择在哪个方向上进行选择的选项.
例如,我需要将数字2.63转换为2.7,而不是3(ceiling
)或2.6(signif(2.63,2)
).
我能想到的唯一解决方案是乘以10,然后乘以10 ceiling
除以10.
ceiling(2.63*10)/10
我敢肯定,必须有一个更优雅的解决方案.
我的问题与此问题非常相似.
这是我之前类似问题的更新,同样的任务我只需要在sf
框架内完成.
我需要识别这个地图中多边形,红线之间的内部边界.
在sp
框架内我习惯使用自编写的函数来包装@Spacedman的答案.这里是:
identify_borders <- function(SPolyDF){
require(rgeos)
require(sp)
borders <- gDifference(
as(SPolyDF,"SpatialLines"),
as(gUnaryUnion(SPolyDF),"SpatialLines"),
byid=TRUE)
df <- data.frame(len = sapply(1:length(borders),
function(i) gLength(borders[i, ])))
rownames(df) <- sapply(1:length(borders),
function(i) borders@lines[[i]]@ID)
SLDF <- SpatialLinesDataFrame(borders, data = df)
return(SLDF)
}
Run Code Online (Sandbox Code Playgroud)
或者可以使用raster::boundaries()
.
# dev version of ggplot2 for geom_sf()
devtools::install_github("tidyverse/ggplot2")
library(tidyverse)
library(sf)
load(url("https://ikashnitsky.github.io/share/1712-so-q-identify-borders/geodata.Rdata"))
ggplot() +
geom_sf(data = gd_nuts0) +
geom_sf(data = gd_borders, color = "red") +
coord_sf(datum = NA) +
theme_void()
Run Code Online (Sandbox Code Playgroud) 我试图使我的研究可重现,将数据存储在figshare。
当我下载并解压缩 R 中的数据时,发生了一些奇怪的事情。
这是 zip
如果我手动下载它,它打开正常;但是当我尝试使用R
脚本获取它时,下载的存档已损坏。任何想法问题出在哪里?
重现我的错误的代码
url <- 'https://ndownloader.figshare.com/files/4797355'
path <- 'test/missing_data_raw.zip'
ifelse(file.exists(path1), yes = 'file alredy exists', no = download.file(url1, path1))
unzip(zipfile = path1,exdir = 'test')
Run Code Online (Sandbox Code Playgroud)