我正在尝试绘制多个绘图,使用ggplot2它们进行排列grid.arrange().由于我设法找到某人描述我遇到的确切问题,因此我引用了链接中的问题描述:
当我使用ggsave()后grid.arrange(),即
Run Code Online (Sandbox Code Playgroud)grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) ggsave("sgcirNIR.jpg")
我不保存网格图,而是保存最后一个ggplot.是否有任何方法可以通过grid.arrange()使用
ggsave()或类似的方式实际保存绘图?除了使用旧的方式
Run Code Online (Sandbox Code Playgroud)jpeg("sgcirNIR.jpg") grid.arrange(sgcir1,sgcir2,sgcir3,ncol=2,nrow=2) dev.off()
相同的链接提供以下解决方案:
require(grid)
require(gridExtra)
p <- arrangeGrob(qplot(1,1), textGrob("test"))
grid.draw(p) # interactive device
ggsave("saving.pdf", p) # need to specify what to save explicitly
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何使用以下代码ggsave()保存grid.arrange()调用的输出,该代码取自链接:
library(ggplot2)
library(gridExtra)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data=dsamp, colour=clarity)
p2 <- qplot(carat, price, data=dsamp, colour=clarity, geom="path")
g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == …Run Code Online (Sandbox Code Playgroud) 我正在尝试ggplot2使用来自不同数据帧的数据构建映射.
library(maptools)
xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
xx.sub1 <- subset(xx, xx$FIPSNO < 37010)
xx.sub2 <- subset(xx, xx$FIPSNO > 37010)
xx.sub1@data$id <- rownames(xx.sub1@data)
xx.sub1.points <- fortify(xx.sub1, region="id")
xx.sub1.df = plyr::join(xx.sub1.points, xx.sub1@data, by="id")
xx.sub2@data$id <- rownames(xx.sub2@data)
xx.sub2.points <- fortify(xx.sub2, region="id")
xx.sub2.df = plyr::join(xx.sub2.points, xx.sub2@data, by="id")
ggplot(xx.sub2.df) +
aes(long, lat, fill = (SID79/BIR79)*1000, group = group) +
geom_polygon() + geom_path(color="grey80") +
coord_equal() +
scale_fill_gradientn(colours = RColorBrewer::brewer.pal(7, "YlOrBr")) +
geom_polygon(data = xx.sub1.df, fill = "grey50") +
geom_path(data = xx.sub1.df, color="grey80") …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用安排多个地块grid.arrange.它完成了本书的工作,并在致电时:
p1 <- ggplot(subset(mtcars, cyl = 4), aes(wt, mpg, colour = cyl)) + geom_point()
p2 <- ggplot(subset(mtcars, cyl = 8), aes(wt, mpg, colour = cyl)) + geom_point()
grid.arrange(p1, p2, ncol = 2)
Run Code Online (Sandbox Code Playgroud)
我得到两个很好的图,大小对称:

我的图表引用了不同的参数,但它们对组共享相同的颜色编码.所以我想删除除了一个以外的所有传奇,找到一个不错的地方.
但是,当我尝试:
p3 <- ggplot(subset(mtcars, cyl = 8), aes(wt, mpg, colour = cyl)) + geom_point() + guides(colour=FALSE)
grid.arrange(p3, p2, ncol = 2)
Run Code Online (Sandbox Code Playgroud)
没有图例的情节得到(正确)更大:

我想保持大小(作为x轴的长度)在图表中保持相同.
我知道我可以在这里使用faceting,但是我还需要结合使用facet(我认为)很难实现的各种图形.
有可能做到grid.arrange吗?还有其他解决方案吗?
假设这个结构的数据:
test <- structure(list(characteristic = structure(c(1L, 2L, 3L, 1L, 2L
), .Label = c("Factor1", "Factor2", "Factor3"), class = "factor"),
es = c(1.2, 1.4, 1.6, 1.3, 1.5), ci_low = c(1.1, 1.3, 1.5,
1.2, 1.4), ci_upp = c(1.3, 1.5, 1.7, 1.4, 1.6), label = structure(c(1L,
3L, 5L, 2L, 4L), .Label = c("1.2 (1.1, 1.3)", "1.3 (1.2, 1.4)",
"1.4 (1.3, 1.5)", "1.5 (1.4, 1.6)", "1.6 (1.5, 1.7)"), class = "factor"),
set = structure(c(1L, 1L, 1L, 2L, 2L), .Label …Run Code Online (Sandbox Code Playgroud) 该ST_DWithin文件说,第三个参数(距离)以米为单位.但是当我执行一些查询时,它似乎将第3个参数作为'degree'?
这是我的简化表结构:
> \d+ theuser;
Table "public.theuser"
Column | Type | Modifiers | Storage | Description
----------+------------------------+-----------+----------+-------------
id | bigint | not null | plain |
point | geometry | | main |
Indexes:
"theuser_pkey" PRIMARY KEY, btree (id)
"point_index" gist (point)
Referenced by:
...
Has OIDs: no
Run Code Online (Sandbox Code Playgroud)
所有点都以SRID = 4326存储.
这是查询:
> select * from theuser where ST_DWithin(point , ST_GeomFromText('POINT(120.9982 24.788)',4326) , 100 );
Run Code Online (Sandbox Code Playgroud)
它将第3个参数(100)作为'degree',因此它返回所有数据,我必须缩小到0.001以找到附近的点.
但是如何直接传递米作为第三个参数(我不想做米/度变换)?我的查询有什么问题?为什么postgreSQL不像文件那样把它作为米?
环境:
> select version();
version
-----------------------------------------------------------------------------------------------------------
PostgreSQL 8.4.9 on i486-pc-linux-gnu, compiled …Run Code Online (Sandbox Code Playgroud) 我正在使用R&knitr构建小型报告,将输出发送到pdf.
我在分析中使用了几个形状文件,每当我使用包的readOGR功能时,rgdal我都会获得有关正在读取的内容的信息,例如:
OGR data source with driver: ESRI Shapefile
Source: "__PATH_HERE__", layer: "__NAME__OF__LAYER__HERE__"
with 148 features and 5 fields
Feature type: wkbPolygon with 2 dimensions
Run Code Online (Sandbox Code Playgroud)
通常,它是有用的...但不幸的是它也打印出我的pdf输出.
我尝试设置knitr的块选项,echo=FALSE, message=FALSE但不幸的是它没有帮助.
有什么更好的解决方案吗?
从ggplot2手册中获取一个简单的图
p <- ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()
p + geom_hline(yintercept=20)
Run Code Online (Sandbox Code Playgroud)
正如所宣传的那样,我得到一个值为20的水平线.

有没有办法在x轴上限制这条线的范围,比如说2到4的范围?
从ggplot2帮助页面获取图表:
ggplot(mtcars, aes(factor(cyl))) + geom_bar() + facet_grid(. ~ vs)
Run Code Online (Sandbox Code Playgroud)
是否可以更改所选面板的边框(颜色和/或厚度)?例如,我想改变刻面变量'1'方面的边界vs.
我尝试添加
theme(panel.border = element_rect(size = 3, colour = "red", fill = NA))
Run Code Online (Sandbox Code Playgroud)
但该解决方案改变了所有边界.
我也在考虑使用geom_rect或者geom_polygon不确定如何将它限制在一个情节中.
我在R帮助列表上偶然发现了这个帖子,但解决方案对我不起作用
任何有关如何前进的建议都将受到高度赞赏.
我有一堆来自GPSLogger for Android应用程序的gpx文件.
文件看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.0" creator="GPSLogger - http://gpslogger.mendhak.com/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0
http://www.topografix.com/GPX/1/0/gpx.xsd" >
<time>2011-08-26T06:25:20Z</time>
<bounds></bounds>
<trk>
<trkseg>
<trkpt lat="46.94681501102746" lon="7.398453755309032" >
<ele>634.0</ele>
<speed>0.0</speed>
<src>gps</src>
<sat>6</sat>
<time>2011-08-26T06:25:20Z</time>
</trkpt>
<trkpt lat="46.94758878281887" lon="7.398622951942811" >
<ele>748.0</ele>
<speed>0.0</speed>
<src>gps</src>
<sat>5</sat>
<time>2011-08-26T06:30:56Z</time>
</trkpt>
... ... ...
</trkseg>
</trk>
</gpx>
Run Code Online (Sandbox Code Playgroud)
是否可以遍历包含这些文件的目录并使用SQL或Python将它们加载到一个PostGIS表中?
我在这个博客文章中提到了这个问题:
我不知道任何可以直接从GPX转换到PostGIS的东西
这篇文章给出了一个使用SQL来做到这一点的例子,但我无法理解代码:/