简体   繁体   English

在这些上绘制带有颜色的绘图距离

[英]maps r plot distances with color on these

perhaps you have an idea and could help me. 也许您有个主意可以帮助我。 I have following data: 我有以下数据:

 lon.x <- c(11.581981, 13.404954, 9.993682,  7.842104 , 11.741185)
 lat.x <- c(48.135125, 52.520007, 53.551085, 47.999008, 48.402880)
 lon.y <- c(8.801694, 7.842104 , 11.581981, 13.404954,  7.842104 )
 lat.y <- c(53.079296,47.999008,  48.135125, 52.520007, 47.999008)
 pred <- c(1,2,3,4,5)
 data <- data.frame(cbind(lon.x, lat.x, lon.y, lat.y, pred))

where "lon.x" and "lat.x" are longitude-latitude points of a city and "lon.y" and "lat.y" of another city. 其中“ lon.x”和“ lat.x”是一个城市的经纬度点,是另一个城市的“ lon.y”和“ lat.y”。 So there are pairs of cities. 因此,有成对的城市。

Now, I would like to make a map in R, with 现在,我想用

(1) the direct distances between the x and y coordinates as a line (1)x和y坐标之间的直接距离为一条线

(2) which will receive a different color based on the variable "pred", this could be red for higher values and blue for lower, or thicker lines with higher values of "pred". (2)会根据变量“ pred”接收不同的颜色,对于较高的值,它可以是红色,对于较低或较粗的线,其“ pred”的值可以是蓝色。

The result should be a simple map, with lines between the cities, that are shaped based on the variable "pred". 结果应该是一张简单的地图,在城市之间具有基于变量“ pred”的形状。 For instance, the line between the first pair of cities would be thinner, while the last one would be thicker. 例如,第一对城市之间的线会更细,而最后一对城市之间的线会更粗。 Is that possible? 那可能吗?

I have currently only made to receive a (very complicated) google map of Germany: 我目前仅能收到一张德国(非常复杂)的Google地图:

              library(mapproj)
              map <- get_map(location = 'Germany', zoom = 6.2)
              ggmap(map)

But I am not sure how to plot points and especially relations between the points that differ based on "pred". 但是我不确定如何绘制点,尤其是基于“ pred”的点之间的关系。 Also a very simple map (not so detailed google map) would be best! 另外一个非常简单的地图(不是那么详细的谷歌地图)将是最好的! Any idea? 任何想法? THANKS! 谢谢!

You can use ggplot2 to add lines onto the plot. 您可以使用ggplot2将线添加到绘图上。

library(ggplot2)
library(ggmap)
map <- get_map(location = 'Germany', zoom = 6)

ggmap(map) +
  geom_segment(data=data, aes(x=lon.x, xend=lon.y, y=lat.x, yend=lat.y, color=pred), size=2) +
  scale_color_continuous(high="red", low="blue")

As for the simpler map, you can download shape files (just the outlines of countries) from www.gadm.org . 至于更简单的地图,您可以从www.gadm.org下载形状文件(仅是国家的轮廓)。 Level 0 maps are just the country, level 1 have state boundaries, etc. To use one of these, download the file from the website and use this code: 级别0的地图仅是国家/地区,级别1的国家边界等。要使用其中的一种,请从网站上下载文件并使用以下代码:

load("DEU_adm0.RData")
gadm <- fortify(gadm)

ggplot(gadm) +
  geom_path(aes(x=long, y=lat, group=group)) + 
  geom_segment(data=data, aes(x=lon.x, xend=lon.y, y=lat.x, yend=lat.y, color=pred), size=2) +
  scale_color_continuous(high="red", low="blue")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM