将 R 降价(使用 plot() 函数)编织到 PDF 会出错
Knitting R markdown (with plot() function) to PDF gives error
在将 RMD 编织成 PDF 时,出现以下错误:
Quitting from lines 47-76 (ABC.Rmd)
Error in (function (file = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", :
cannot open file '~/Read and visualize the "governorate" shapefile-1.pdf'
如果我尝试查看它对 HTML 是否有效,则会收到以下错误:
Quitting from lines 47-76 (ABC.Rmd)
Error in png(..., res = dpi, units = "in") : unable to start png() device
Calls: <Anonymous> ... in_dir -> plot2dev -> do.call -> <Anonymous> -> png
In addition: Warning messages:
1: In png(..., res = dpi, units = "in") :
unable to open file '~/Read and visualize the "governorate" shapefile-1.png' for writing
2: In png(..., res = dpi, units = "in") : opening device failed
现在我不知道它是否与绘图功能有关,因为我从来没有遇到过“ggplot”这个错误
```{r Read and visualize the "governorate" shapefile, echo = FALSE}
# Import the Iraqi regions shapefile
Iraq_Regions = readOGR(dsn = "~/governorates.shp")
# Import the Baghdad neighborhood shapefile
Baghdad_Neigh = readOGR(dsn = "~/baghdad_neighborhoods.shp")
# Plot the Wiki Leaks data frame
plot(x=Wiki_Leaks$Longitude, y=Wiki_Leaks$Latitude, pch=20, col='red', cex=0.4)
plot(Iraq_Regions, border='blue', add=T)
plot(Baghdad_Neigh, border='yellow', add=T)
# Merge neighborhoods to make the Baghdad city outline
Greater_Baghdad <- gUnaryUnion(Baghdad_Neigh)
# Plot the shapefiles
plot(x=Wiki_Leaks$Longitude, y=Wiki_Leaks$Latitude, pch=20, col='red', cex=0.4)
plot(Iraq_Regions, border='blue', add=T)
plot(Greater_Baghdad, border='yellow', add=T)
# view the names of different regions and the numbers of records in each region
#table(Wiki_Leaks$Region)
# Retrieve all events near Baghdad
Wiki_Leaks_MND_N_df = Wiki_Leaks[Wiki_Leaks$Region == "MND-N", ]
# Now make a basic plot
plot(x = Wiki_Leaks_MND_N_df$Long, y = Wiki_Leaks_MND_N_df$Lat,
pch = 20, col = 'red', cex = 0.4,
ylab = "Longitude", xlab = "Latitude",
main = "Death Events in Baghdad Neihborhood MND-N")
```
您需要在命名代码块时删除标点符号,这些标点符号将用于命名您的文件 (more information here, for example)。
对于您的情况,删除 "governorate"
即可解决问题。
在将 RMD 编织成 PDF 时,出现以下错误:
Quitting from lines 47-76 (ABC.Rmd)
Error in (function (file = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", :
cannot open file '~/Read and visualize the "governorate" shapefile-1.pdf'
如果我尝试查看它对 HTML 是否有效,则会收到以下错误:
Quitting from lines 47-76 (ABC.Rmd)
Error in png(..., res = dpi, units = "in") : unable to start png() device
Calls: <Anonymous> ... in_dir -> plot2dev -> do.call -> <Anonymous> -> png
In addition: Warning messages:
1: In png(..., res = dpi, units = "in") :
unable to open file '~/Read and visualize the "governorate" shapefile-1.png' for writing
2: In png(..., res = dpi, units = "in") : opening device failed
现在我不知道它是否与绘图功能有关,因为我从来没有遇到过“ggplot”这个错误
```{r Read and visualize the "governorate" shapefile, echo = FALSE}
# Import the Iraqi regions shapefile
Iraq_Regions = readOGR(dsn = "~/governorates.shp")
# Import the Baghdad neighborhood shapefile
Baghdad_Neigh = readOGR(dsn = "~/baghdad_neighborhoods.shp")
# Plot the Wiki Leaks data frame
plot(x=Wiki_Leaks$Longitude, y=Wiki_Leaks$Latitude, pch=20, col='red', cex=0.4)
plot(Iraq_Regions, border='blue', add=T)
plot(Baghdad_Neigh, border='yellow', add=T)
# Merge neighborhoods to make the Baghdad city outline
Greater_Baghdad <- gUnaryUnion(Baghdad_Neigh)
# Plot the shapefiles
plot(x=Wiki_Leaks$Longitude, y=Wiki_Leaks$Latitude, pch=20, col='red', cex=0.4)
plot(Iraq_Regions, border='blue', add=T)
plot(Greater_Baghdad, border='yellow', add=T)
# view the names of different regions and the numbers of records in each region
#table(Wiki_Leaks$Region)
# Retrieve all events near Baghdad
Wiki_Leaks_MND_N_df = Wiki_Leaks[Wiki_Leaks$Region == "MND-N", ]
# Now make a basic plot
plot(x = Wiki_Leaks_MND_N_df$Long, y = Wiki_Leaks_MND_N_df$Lat,
pch = 20, col = 'red', cex = 0.4,
ylab = "Longitude", xlab = "Latitude",
main = "Death Events in Baghdad Neihborhood MND-N")
```
您需要在命名代码块时删除标点符号,这些标点符号将用于命名您的文件 (more information here, for example)。
对于您的情况,删除 "governorate"
即可解决问题。