作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用一些数据绘制栅格 map ,以及该数据的边距图。有点像 levelplot() 函数,或者使用颜色(反射(reflect)绘图内的颜色)而不是线条更好。看看我正在尝试做什么,下面就会很清楚了。 rasterVis
示例(带密度线):
但是,我想使用ggplot2。下载data used in this example (8KB)。
我尝试使用grid.arrange()
:
library(ggplot2)
library(raster)
library(maps)
library(grid)
library(scales) #for muted()
library(gridExtra)
load("example.Rsave") #load data
#prepare data for the margin plot
var=tp_rlim; min=0; max=1; mid=0.5
varname <- colnames(var)[3]
zonal <- aggregate(var[,3], list(var$Lat), mean)
colnames(zonal) <- c("Lat", varname)
#plot the margin plot
ggzonal <- ggplot(zonal, aes(x="", y=Lat, fill=tp))+
geom_raster() +
theme(legend.position="none",
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank()
) +
scale_fill_gradient2(limits=c(min, max), space="Lab", midpoint=mid)
#plot main plot
gg <- ggplot(var, aes_string(x="Lon", y="Lat", fill=varname)) +
geom_raster() +
coord_quickmap(xlim = range(var$Lon), ylim = range(var$Lat)) +
scale_fill_gradient2(limits=c(min, max), space="Lab", midpoint=mid) +
theme(legend.position="bottom",
legend.key.width = unit(1.5, "cm"),
legend.key.height = unit(0.3, "cm"),
legend.title=element_blank()
) +
borders(region=unique(na.omit(map.where("world", var$Lon, var$Lat))), colour="black")
grid.arrange(gg, ggzonal, ncol=2, widths=c(5, 1))
我也尝试过使用ggExtra::ggMarginal
,但是:
ggExtra::ggMarginal(gg)
或者:
ggExtra::ggMarginal(gg, type="histogram")
我该怎么做?
最佳答案
这可能有用,
# fix the ylim of annotation plot
gg2 = ggzonal + coord_cartesian(ylim = range(var$Lat))
# devtools::install_github("baptiste/egg")
library(egg)
ggarrange(gg, gg2, widths = c(10,1))
关于r - 在 ggplot2 中绘制 geom_ raster() 的边距图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36501486/
我正在尝试使用一些数据绘制栅格 map ,以及该数据的边距图。有点像 levelplot() 函数,或者使用颜色(反射(reflect)绘图内的颜色)而不是线条更好。看看我正在尝试做什么,下面就会很清
我的目标是为每个观察绘制一个图,其中有一个小宽度的垂直条将点连接到 y=0 线(x 轴)。 我尝试了不同的 geom,我的理解是 geom_bar 不起作用,因为它只能取正值,但我不太确定。最接近我想
我是一名优秀的程序员,十分优秀!