- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我认为我有一个非常简单的问题,但我无法在任何地方找到答案。
我有很多包含龙虾捕获量的数据。一切看起来都很像这样。
Trip.ID Latitude Longitude DateTime ML6 TotalNephropsLandings
16409 OTB_CRU_32-69_0_0DK102831 57.931 9.277 2012-10-04 19:02:00 OTB_CRU_32-69_0_0 0.2188619
16410 OTB_CRU_32-69_0_0DK102831 57.959 9.375 2012-10-04 21:02:00 OTB_CRU_32-69_0_0 0.2188619
16411 OTB_CRU_32-69_0_0DK102831 58.201 10.232 2012-10-04 02:00:00 OTB_CRU_32-69_0_0 0.2188619
16412 OTB_CRU_32-69_0_0DK102831 58.208 10.260 2012-10-04 03:00:00 OTB_CRU_32-69_0_0 0.2188619
16413 OTB_CRU_32-69_0_0DK102831 58.169 10.078 2012-10-03 23:00:00 OTB_CRU_32-69_0_0 0.2188619
16414 OTB_CRU_32-69_0_0DK102831 57.919 9.227 2012-10-04 18:00:00 OTB_CRU_32-69_0_0 0.2188619
我想做的只是根据“ML6”列制作一张包含区域周围轮廓的 map ,这是用于钓鱼的不同工具。
我尝试使用 geom_densis2d,它看起来像这样: 然而我真的不想展示密度,只想展示它们存在的地方。因此,基本上一条线围绕一组坐标,这些坐标来自 ML6 中的同一级别。有人能帮我解决这个问题吗?
如果有替代方案将它们也填充为多边形,那就太好了。但也许可以使用“fill=”简单地完成。
如果有人知道如何在没有 R 的情况下做到这一点,也欢迎您提供帮助,但那样我可能需要更深入的信息。
很抱歉没有生成更多的数据框...
当然,我应该生成我的情节代码,所以这里基本上是:
#Get map
map <- get_map(location=c(left= 0, bottom=45, right=15 ,top=70), maptype = 'satellite')
ggmap(map, extent="normal") +
geom_density2d(data = df, aes(x=Longitude, y=Latitude, group=ML6, colour=ML6))
最佳答案
可能有更好的方法来完成这项工作。但是,这是我为您提供的方法。我希望这种方法适用于 ggmap
以及。鉴于我有时间,这是我对你最好的。由于您的样本数据太小,我决定使用我自己的部分数据。您要做的就是查看ggplot_build(objectname)$data[1]
。 (看起来,当您使用 ggmap
时,数据将在 ggplot_build(object name)$data[4]
中。)例如,创建一个像这样的对象。
foo <- ggmap(map, extent="normal") +
geom_density2d(data = df, aes(x=Longitude, y=Latitude, group=ML6, colour=ML6))
然后,输入 ggplot_build(foo)$data[1]
。您应该看到一个数据框 ggplot
正在使用。将有一个名为 level
的列。具有最小级别值的数据子集。我正在使用filter
来自dplyr
。例如,
foo2 <- ggplot_build(foo)$data[1]
foo3 <- filter(foo2, level == 0.02)
foo3 现在有数据点,允许您在 map 上画线。该数据包含关卡最外圈的数据点。你会看到类似这样的东西。
# fill level x y piece group PANEL
#1 #3287BD 0.02 168.3333 -45.22235 1 1-001 1
#2 #3287BD 0.02 168.3149 -45.09596 1 1-001 1
#3 #3287BD 0.02 168.3197 -44.95455 1 1-001 1
然后,您将执行如下操作。就我而言,我没有谷歌地图。我有新西兰的 map 数据。所以我用第一个 geom_path
画这个国家。第二个geom_path
就是你需要的。确保将 lon 和 lat 更改为 x 和 y,如下所示。这样我认为您就拥有了想要的圆圈。
# Map data from gadm.org
NZmap <- readOGR(dsn=".",layer="NZL_adm2")
map.df <- fortify(NZmap)
ggplot(NULL)+
geom_path(data = map.df,aes(x = long, y = lat, group=group), colour="grey50") +
geom_path(data = foo3, aes(x = x, y = y,group = group), colour="red")
更新
这是另一种方法。在这里我使用了我的答案 this post 。您基本上可以确定数据点来绘制圆(多边形)。我在帖子里有一些链接。请看一看。您可以了解循环中发生了什么。抱歉很短。但是,我认为这种方法可以让你画出你想要的所有圆圈。请记住,结果可能不是像轮廓那样光滑的圆圈。
library(ggmap)
library(sp)
library(rgdal)
library(data.table)
library(plyr)
library(dplyr)
### This is also from my old answer.
### Set a range
lat <- c(44.49,44.5)
lon <- c(11.33,11.36)
### Get a map
map <- get_map(location = c(lon = mean(lon), lat = mean(lat)), zoom = 14,
maptype = "satellite", source = "google")
### Create pseudo data.
foo <- data.frame(x = runif(50, 11.345, 11.357),
y= runif(50, 44.4924, 44.4978),
group = "one",
stringsAsFactors = FALSE)
foo2 <- data.frame(x = runif(50, 11.331, 11.338),
y= runif(50, 44.4924, 44.4978),
group = "two",
stringsAsFactors = FALSE)
new <- rbind(foo,foo2)
### Loop through and create data points to draw a polygon for each group.
cats <- list()
for(i in unique(new$group)){
foo <- new %>%
filter(group == i) %>%
select(x, y)
ch <- chull(foo)
coords <- foo[c(ch, ch[1]), ]
sp_poly <- SpatialPolygons(list(Polygons(list(Polygon(coords)), ID=1)))
bob <- fortify(sp_poly)
bob$area <- i
cats[[i]] <- bob
}
cathy <- as.data.frame(rbindlist(cats))
ggmap(map) +
geom_path(data = cathy, aes(x = long, y = lat, group = area), colour="red") +
scale_x_continuous(limits = c(11.33, 11.36), expand = c(0, 0)) +
scale_y_continuous(limits = c(44.49, 44.5), expand = c(0, 0))
关于r - 使用 ggmap/ggplot2 在 map 上按组绘制等高线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26842278/
前 R 用户,我曾经通过 ggplotly() 函数广泛地结合 ggplot 和 plot_ly 库来显示数据。 刚到 Python 时,我看到 ggplot 库可用,但在与 plotly 的简单组合
ggplotly 使用 ggplot 删除 geom_line 图的图例。 见例如以下: library(plotly) g % ggplotly() 关于r - ggplotly 从 gg
我有一个 ggplot我试图以非常简约的外观制作线图的问题。我已经摆脱了图例,转而使用每行右侧的文本标签。如果标签不是那么长,它可能不会那么明显,但如果网格线停在最大 x 值(在这种情况下,在 201
我想使用相同的 ggplot 代码以我的数据框中的数字为条件生成 8 个不同的数字。通常我会使用 facet_grid,但在这种情况下,我希望最终得到每个单独数字的 pdf。例如,我想要这里的每一行一
当我在 ggplot 上使用 geom_text 时,与 ggplot 的“填充”选项发生冲突。 这是问题的一个明显例子: library(ggplot2) a=ChickWeight str(a)
是否可以结合使用 ggplot ly 和拼凑而成的ggplot? 例子 这将并排显示两个图 library(ggplot2) library(plotly) library(patchwork) a
我想绘制一个图表,其中 y 轴以百分比表示: p = ggplot(test, aes(x=creation_date, y=value, color=type)) + geom_line(aes
如何去除ggsave中的白边距? 我的问题和Remove white space (i.e., margins) ggplot2 in R一模一样。然而,那里的答案对我来说并不理想。我不想对固定但未知
我有一个带有一些文本层的条形图,在 ggplot 库中一切正常,但现在我想添加一些与 ggplotly 的交互性,但它无法显示文本层 我更新了所有软件包但问题仍然存在 df = read.table(
当我尝试在 ggplot 中为我的箱线图设置自定义宽度时,它工作正常: p=ggplot(iris, aes(x = Species,y=Sepal.Length )) + geom_boxplot(
我正在尝试为 ggplot 密度创建一个图例,将一个组与所有组进行比较。使用此示例 - R: Custom Legend for Multiple Layer ggplot - 我可以使用下面的代码成
所以我试图在一个多面的 ggplot 上编辑 y 值,因为我在编织时在情节上有几个不准确之处。我对 R 和 R Markdown 很陌生,所以我不太明白为什么,例如,美国的 GDP PPP 在美元金额
我需要在 python 条形图的 x 轴 ggplot 上格式化日期。 我该怎么做? 最佳答案 使用 scale_x_date() 格式化 x 轴上的日期。 p = ggplot(aes(x='dat
我想使用 ggplotly因为它的副作用相同ggplot甚至graphics做。我的意思是当我 knitr::knit或 rmarkdown::render我期望的 Rmd 文档 print(obj)
我在下面有一个简单的应用程序,它显示了一个 ggplot。 ggplot 在控制台中生成警告(见底部图片)。我想捕获警告,并将其显示在应用程序的情节下方。 这是我的代码: library(shiny)
如果显示的基本数据集很大(下面的示例工作代码),则在 Shiny 的应用程序中向/从 ggplot 添加/删除图层可能需要一段时间。 问题是: 有没有办法缓存 ggplot(基本图)并添加/删除/修改
我正在组合 ggplot 的多个绘图,使用网格视口(viewport),这是必要的(我相信),因为我想旋转绘图,这在标准 ggplot 中是不可能的,甚至可能是 gridExtra 包。 我想在两个图
我可以使用 lattice 在 R 中绘制相对频率直方图包裹: a <- runif(100) library(lattice) histogram(a) 我想在 ggplot 中获得相同的图形.我试
我需要重新安装 R,但我现在遇到了 ggplot 的一个小问题。我确信有一个简单的解决方案,我感谢所有提示! 我经常使用堆叠面积图,通常我通过定义因子水平并以相反的顺序绘制来获得所需的堆叠和图例顺序。
新的并且坚持使用ggplot: 我有以下数据: tribe rho preference_watermass 1 Luna2 -1.000 hypolimnic 2 OP10I-A1
我是一名优秀的程序员,十分优秀!