删除RMarkdown Plots上的Pixelation

时间:2017-09-30 18:09:59

标签: r knitr r-markdown

我正在尝试使用以下代码在RMarkdown上创建绘图,但由于某种原因,我绘制的线变得像素化(见下文)。我似乎无法找到解决方案,因为更改代码块的dpi无效。

指向数据的链接:https://catalog.data.gov/dataset/2013-cartographic-boundary-file-state-for-united-states-1-20000000

```{r}
library(nycflights13)
library(maps)
library(maptools)
library(rgdal)

usa <- readShapePoly(
"data/census_bureau/cb_2013_us_state_20m/cb_2013_us_state_20m.shp")

conterm <- usa[usa$STATEFP != "02" & usa$STATEFP != "15" & usa$STATEFP != "72",]
conterm_projected <- spTransform(conterm, CRS(conterm_proj4)

proj4string(conterm) <- CRS("+proj=longlat")
conterm_proj4 <- "+proj=aea +lat_1=29.5 +lat_2=45.5 +lon_0=97.2w"


sfo <- airports[airports$faa == "SFO",]
jfk <- airports[airports$faa == "JFK",]

latlon <- data.frame(lon=c(sfo$lon, jfk$lon), lat=c(sfo$lat, jfk$lat))
coordinates(latlon) <- c("lon", "lat")
proj4string(latlon) <- CRS("+proj=longlat")
latlon_projected <- spTransform(latlon, CRS(conterm_proj4))


par(mfrow=c(2,1), mar = rep(1,4))
plot(conterm, border="#cccccc", lwd=0.5, main = "Mercator Projection")
points(c(sfo$lon, jfk$lon), c(sfo$lat, jfk$lat), col="red", lwd=2, pch=19)
lines(c(sfo$lon, jfk$lon), c(sfo$lat, jfk$lat), col="red", lwd=2)

plot(conterm_projected, border="#cccccc", lwd=0.5, main = "Albers Projection")
points(latlon_projected@coords, col="red", lwd=2, pch=19)
lines(latlon_projected@coords, col="red", lwd=2)
```

这是上面代码的输出,注意像素化的红线

This is the output of the code above, notice the pixelated red line

1 个答案:

答案 0 :(得分:0)

你的身材太小了。对我来说,这段代码在pdf和html中都给出了流畅的界限:

```{r, fig.width=20, fig.height=25}
library(nycflights13)
library(maps)
library(maptools)
library(rgdal)

usa <- readShapePoly("~/d/cb_2013_us_state_20m.shp")
conterm_proj4 <- "+proj=aea +lat_1=29.5 +lat_2=45.5 +lon_0=97.2w"
conterm <- usa[usa$STATEFP != "02" & usa$STATEFP != "15" & usa$STATEFP != "72",]
proj4string(conterm) <- CRS("+proj=longlat")

sfo <- airports[airports$faa == "SFO",]
jfk <- airports[airports$faa == "JFK",]

latlon <- data.frame(lon=c(sfo$lon, jfk$lon), lat=c(sfo$lat, jfk$lat))
coordinates(latlon) <- c("lon", "lat")
proj4string(latlon) <- CRS("+proj=longlat")
latlon_projected <- spTransform(latlon, CRS(conterm_proj4))


par(mfrow=c(2,1), mar = rep(1,4))
plot(conterm, border="#cccccc", lwd=0.5, main = "Mercator Projection")
points(c(sfo$lon, jfk$lon), c(sfo$lat, jfk$lat), col="red", lwd=2, pch=19)
lines(c(sfo$lon, jfk$lon), c(sfo$lat, jfk$lat), col="red", lwd=2)

plot(conterm, border="#cccccc", lwd=0.5, main = "Albers Projection")
points(latlon_projected@coords, col="red", lwd=2, pch=19)
lines(latlon_projected@coords, col="red", lwd=2)
```

请注意,您使用par(mfrow=c(2,1))在图像画布上挤压两个大致二次的数字,因此每个图像大约是标准尺寸的1/4。

或者,分别插入两张图像。这样,这两个都应该是全尺寸的。