ggplot2中的多边形之间出现的难看的线条打印在pdf上但不在图形设备

时间:2018-03-26 21:37:33

标签: r pdf ggplot2

当我将地图打印到pdf时,我似乎无法摆脱ggplot2中多边形之间的线条。我无法在新数据集中,在MWE中重现我的错误。

这是我的地图代码:

map <- ggplot(USA_fort_premade) + 
  aes(long,lat,group=group) + 
  geom_polygon(aes(x=long,y=lat, group=group, fill=avg_coveragelevel), 
               data=fips_fort)+
  geom_path(color="black", lwd = .1) +
  coord_fixed(1.3) +
  scale_fill_gradient2(low = "yellow", mid = "lightblue",
                       high = "blue", midpoint = .7, 
                       space = "Lab", width = 6),
                       na.value = "grey80", guide = "colourbar") +
  theme_map()

USA_fort_premade是外部shapefile,fips_fort是内部shapefile。在R图形设备上,它看起来很好:

enter image description here

当我将其打印为PDF时,它看起来像这样:

enter image description here

导出它并不重要,它在evince和okular中看起来一样 - 它不是观众的遗物。

当我尝试指定多边形边框的颜色与填充相同时,我得到了这个,这不是我想要的

enter image description here

我认为难度与我将一个shapefile与另一个形状文件重叠的事实有关。

有没有解决方法?

OK FINE 我做了一个MWE,但我无法重现我的错误!我现在更加困惑。有人知道线路可能来自哪里吗?

library(maptools)
library(raster)
library(tidyverse)
library(ggplot2)
library(broom)

theme_map <- function (base_size = 12, base_family = "") {
  theme_gray(base_size = base_size, base_family = base_family) %+replace% 
    theme(
      axis.line=element_blank(),
      axis.text.x=element_blank(),
      axis.text.y=element_blank(),
      axis.ticks=element_blank(),
      axis.ticks.length=unit(0.3, "lines"),
      axis.ticks.margin=unit(0.5, "lines"),
      axis.title.x=element_blank(),
      axis.title.y=element_blank(),
      legend.background=element_rect(fill="white", colour=NA),
      legend.key=element_rect(colour="white"),
      legend.key.size=unit(1.2, "lines"),
      legend.position="right",
      legend.text=element_text(size=rel(0.8)),
      legend.title=element_text(size=rel(0.8), face="bold", hjust=0),
      panel.background=element_blank(),
      panel.border=element_blank(),
      panel.grid.major=element_blank(),
      panel.grid.minor=element_blank(),
      panel.spacing=unit(0, "lines"),
      plot.background=element_blank(),
      plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
      plot.title=element_text(size=rel(1.2), vjust = 2),
      strip.background=element_rect(fill="white", colour="white"),
      strip.text.x=element_text(size=rel(.8), margin=margin(b=3)),
      strip.text.y=element_text(size=rel(0.8), angle=-90) 
    )   
}


Kaz_shp <- getData('GADM', country='Kazakhstan', level=1)
fips_shp <- getData('GADM', country='Kazakhstan', level=2)
fips_shp$plotvar <- runif(nrow(fips_shp@data))

Kaz_fort <- tidy(Kaz_shp, region = "NAME_1")
fips_fort <- tidy(fips_shp, region = "NAME_2")
fips_fort <- merge(fips_fort, fips_shp@data[,c("NAME_2","plotvar")], by.x = "id", by.y = "NAME_2")

map <- ggplot(Kaz_fort) + 
  aes(long,lat,group=group) + 
  geom_polygon(aes(x=long,y=lat, group=group, fill=plotvar), 
               data=fips_fort)+
  geom_path(color="black", lwd = .1) +
  coord_fixed(1.3) +
  scale_fill_gradient2(low = "yellow", mid = "lightblue",
                       high = "blue", midpoint = .7,
                       space = "Lab", name = "wowee wow",
                       na.value = "grey60", guide = "colourbar") +
  ggtitle("Potassium Production")+theme_map()
map
dev.copy2pdf(file = "verynice.pdf")
system("evince verynice.pdf")

enter image description here enter image description here

0 个答案:

没有答案