多年来我一直用它ggplot2来绘制气候网格数据.这些通常是预计的NetCDF文件.单元格在模型坐标中是方形的,但取决于模型使用的投影,在现实世界中可能不是这样.
我通常的方法是首先在合适的常规网格上重新映射数据,然后绘图.这引入了对数据的小修改,通常这是可以接受的.
但是,我已经确定这已经不够好了:我想直接绘制投影数据而不重新映射,因为其他程序(例如ncl)可以,如果我没有弄错的话,可以不触及模型输出值.
但是,我遇到了一些问题.我将从下面逐步详细介绍可能的解决方案,从最简单到最复杂,以及它们的问题.我们能克服它们吗?
#Load packages
library(raster)
library(ggplot2)
#This gives you the starting data, 's'
load(url('https://files.fm/down.php?i=kew5pxw7&n=loadme.Rdata'))
#If you cannot download the data, maybe you can try to manually download it from http://s000.tinyupload.com/index.php?file_id=04134338934836605121
#Check the data projection, it's Lambert Conformal Conic
projection(s)
#The data (precipitation) has a 'model' grid (125x125, units are integers from 1 to 125)
#for each point a lat-lon value is also assigned
pr …Run Code Online (Sandbox Code Playgroud) 我想在R中做一个小程序,用于交互式可视化和修改一些栅格数据集,看作彩色图像.用户应该打开一个文件(从终端可以),绘制它,用鼠标点击选择要编辑的点,然后插入新值.
到目前为止,我很容易实现.我使用包中的plot()函数raster来显示图,然后click()选择点并通过终端编辑它们的值.
我想添加在鼠标上显示值的功能.我已经搜索了如何做到这一点的方法,但这似乎不适用于标准的R包.它是否正确?
在这种情况下,我可能被迫使用外部包,例如gGobi,iPlots,Shiny或Plotly.但是,我更喜欢KISS并且只使用"标准"图形工具,例如栅格plot()函数或格子图形对象(例如来自rasterVis).
我理解一个Shiny应用程序可能是最好的,但它需要大量的时间来学习和完善.
我正在尝试使用R和ggplot2绘制分发CDF.但是,在我转换Y轴以获得直线后,我发现在绘制CDF函数时遇到困难.这种情节经常在Gumbel纸质图中使用,但在这里我将使用正态分布作为例子.
我生成数据,并绘制数据的累积密度函数和函数.他们很合适.但是,当我应用Y轴变换时,它们不再适合.
sim <- rnorm(100) #Simulate some data
sim <- sort(sim) #Sort it
cdf <- seq(0,1,length.out=length(sim)) #Compute data CDF
df <- data.frame(x=sim, y=cdf) #Build data.frame
library(scales)
library(ggplot2)
#Now plot!
gg <- ggplot(df, aes(x=x, y=y)) +
geom_point() +
stat_function(fun = pnorm, colour="red")
gg
Run Code Online (Sandbox Code Playgroud)
现在我尝试根据使用的分布变换Y轴.
#Apply transformation
gg + scale_y_continuous(trans=probability_trans("norm"))
Run Code Online (Sandbox Code Playgroud)
点被正确转换(它们位于一条直线上),但功能不是!
但是,如果我这样做,一切似乎都能正常工作,用ggplot计算CDF:
ggplot(data.frame(x=sim), aes(x=x)) +
stat_ecdf(geom = "point") +
stat_function(fun="pnorm", colour="red") +
scale_y_continuous(trans=probability_trans("norm"))
Run Code Online (Sandbox Code Playgroud)
为什么会这样?为什么不手动计算CDF使用比例变换?
我有一个使用R rgl包生成的3D场景.
我可以保存为PLY格式,然后导出到.u3d(例如使用Meshlab),但它给我以下错误:
Error in if (sum(normals[1:3, it[j, i]] * normal) < 0) normals[, it[j, :
missing value where TRUE/FALSE needed
Run Code Online (Sandbox Code Playgroud)哪个我真的不知道怎么解决.
这是一个重现问题的示例文件.要重现只需在工作目录中下载文件,执行R并运行:
library(rgl)
load("alps3d.Rdata") #This loads the alps3d variable
plot3d(alps3d)
writePLY("alps3d.ply")
Run Code Online (Sandbox Code Playgroud)
如何以可以使用LaTeX在.pdf中迭代的格式保存3d场景?
我有一个ggplot地图,例如:
library(ggmap)
ggmap(get_map())
Run Code Online (Sandbox Code Playgroud)
我希望轴标签自动标记为NS/WE:在上述情况下,例如,它应该显示95.4°E而不是lon -95.4.
我试图弄乱scales包和使用scale_x_continuous和scale_y_continuous标签和打破选项,但我没有设法让它工作.
拥有一个scale_y_latitude和那将是非常棒的scale_x_longitude.
编辑:感谢@Jaap的回答,我得到了以下内容:
scale_x_longitude <- function(xmin=-180, xmax=180, step=1, ...) {
ewbrks <- seq(xmin,xmax,step)
ewlbls <- unlist(lapply(ewbrks, function(x) ifelse(x < 0, paste(x, "W"), ifelse(x > 0, paste(x, "E"),x))))
return(scale_x_continuous("Longitude", breaks = ewbrks, labels = ewlbls, expand = c(0, 0), ...))
}
scale_y_latitude <- function(ymin=-90, ymax=90, step=0.5, ...) {
nsbrks <- seq(ymin,ymax,step)
nslbls <- unlist(lapply(nsbrks, function(x) ifelse(x < 0, paste(x, "S"), ifelse(x > 0, paste(x, "N"),x))))
return(scale_y_continuous("Latitude", …Run Code Online (Sandbox Code Playgroud) 在shinydashboard,可以创建menuItem()s,它们是侧栏中的选项卡.我希望能够使用标准input$foo语法轮询哪个选项卡处于活动状态.
但是,我无法这样做.我尝试引用menuItem()'s tabName或者id什么也没做.
有办法吗?
我正在使用rasterR在R leaflet地图上绘制一个大型的Lat-lon NetCDF shinydashboard.当我点击地图时,会弹出一个弹出窗口,显示所点击的栅格点的行,列,纬度位置和值.(参见下面的可重复代码)
问题是如果光栅足够大,我正在经历光栅的移动.例如,在这里我点击了一个应该有一个值的点,但结果是所识别的点是上面的点.
我相信这与leaflet投影使用的光栅的事实有关,而我用来识别点的原始数据是Lat-Lon,因为点击的点被返回为Lat-Lon leaflet.我不能使用投影文件(depth),因为它的单位是米,而不是度数!即使我试图将这些米重新投射到度数,我也有了转变.
这是代码的基本可运行示例:
#Libraries
library(leaflet)
library(raster)
library(shinydashboard)
library(shiny)
#Input data
download.file("https://www.dropbox.com/s/y9ekjod2pt09rvv/test.nc?dl=0", destfile="test.nc")
inputFile = "test.nc"
inputVarName = "Depth"
lldepth <- raster(inputFile, varname=inputVarName)
lldepth[Which(lldepth<=0, cells=T)] <- NA #Set all cells <=0 to NA
ext <- extent(lldepth)
resol <- res(lldepth)
projection(lldepth) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
#Project for leaflet
depth <- projectRasterForLeaflet(lldepth)
#Prepare UI
sbwidth=200
sidebar <- dashboardSidebar(width=sbwidth)
body <- dashboardBody(
box( #https://stackoverflow.com/questions/31278938/how-can-i-make-my-shiny-leafletoutput-have-height-100-while-inside-a-navbarpa
div(class="outer",width = NULL, solidHeader = …Run Code Online (Sandbox Code Playgroud) 我正在构建一个简单的Shiny + Leaflet R应用程序来导航一个地图,在该地图上用有用的函数绘制raster(从包中raster)addRasterImage().代码很大程度上基于Leaflet自己的例子.但是,我遇到了分层的一些问题:每次重新加载图块时,栅格图像都会以某种方式呈现在图块下方,即使我设置了负片zIndex.标记不会发生这种情况.请参阅附带的代码.这里输入文件示例,366KB.
####
###### YOU CAN SKIP THIS, THE PROBLEM LIES BELOW ######
####
library(shiny)
library(leaflet)
library(RColorBrewer)
library(raster)
selrange <- function(r, min, max) { #Very fast way of selecting raster range, even faster than clamp.
#http://stackoverflow.com/questions/34064738/fastest-way-to-select-a-valid-range-for-raster-data
rr <- r[]
rr[rr < min | rr > max] <- NA
r[] <- rr
r
}
llflood <- raster("example_flooding_posmall.nc")
ext <- extent(llflood)
flood <- projectRasterForLeaflet(llflood)
floodmin <- cellStats(flood, min)
floodmax <- …Run Code Online (Sandbox Code Playgroud) 使用R的raster包,我brick从一个文件中获取,带有以下ncdump标题(我显示一个小的示例文件,实际文件要大得多):
dimensions:
lon = 2 ;
lat = 3 ;
time = UNLIMITED ; // (125000 currently)
variables:
float lon(lon) ;
lon:standard_name = "longitude" ;
lon:long_name = "longitude" ;
lon:units = "degrees_east" ;
lon:axis = "X" ;
float lat(lat) ;
lat:standard_name = "latitude" ;
lat:long_name = "latitude" ;
lat:units = "degrees_north" ;
lat:axis = "Y" ;
double time(time) ;
time:standard_name = "time" ;
time:long_name = "Time" ;
time:units = "seconds since 2001-1-1 00:00:00" ; …Run Code Online (Sandbox Code Playgroud)