本文介绍了在ggmap中绘制曲线,geom_curve不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想用荷兰城市之间的曲线制作荷兰地图。我有两个数据框,分别叫做: df_verticles ,其中包含24个城市的经纬度组合。第二个数据框叫做: df 我想用来绘制经纬线之间从组合到经纬度之间的曲线到组合。 > (df_vertices) city AmountSessions totalkWh AmountRFID scaledAmount scaledkWh纬度经度 1 Alkmaar 13608 104554.68 1326 0.07139012 0.026941910 52.63903 4.755538 2 Almere 11281 100841.42 930 0.05006999 0.025985067 52.39447 5.282043 3 Amersfoort 7719 67663.30 1198 0.06449876 0.017435647 52.15108 5.383069 4 Amstelveen 25794 236437.93 2616 0.14084204 0.060925915 52.31724 4.859266 5阿姆斯特丹402365 3880744.86 18574 1.00000000 1.000000000 52.34560 4.808834 > (df) CityChargeSessions NextCity Amount sumkWh scaledAmount scaledkWh Latitude_from Longitude_from Latitude_to Longitude_to 1 Amsterdam Alkmaar 1058 8133.736 0.18438480 0.15480933 52.34560 4.808834 52.63903 4.755538 2 Amsterdam Almere 998 7254.133 0.17392820 0.13806786 52.34560 4.808834 52.39447 5.282043 3分配阿姆斯特丹阿默斯福特566 4977.404 0.09864064 0.09473489 52.34560 4.808834 52.15108 5.383069 4分配阿姆斯特丹阿姆斯多芬3724 24210.289 0.64900662 0.46079423 52.34560 4.808834 52.31724 4.859266 5分配阿尔默勒阿姆斯特丹1034 8685.526 0.18020216 0.16531155 52.39447 5.282043 52.34560 4.808834 6分配阿默斯福特阿姆斯特丹579 4936.823 0.10090624 0.09396251 52.15108 5.383069 52.34560 4.808834 通常情况下ggmap只是一个underlaye rg of ggplot,所以我决定先在ggplot中绘制我想要的图: ggplot_curve< - ggplot()+ geom_text(data = df_vertices,aes(x = Longitude,y = Latitude + 0.025,label = df_vertices $ city),size = 6)+ geom_point(aes(x = Longitude,y = Latitude,size = scaledkWh ),color =red,data = df_vertices,alpha = 0.5)+ scale_size_continuous(range = c(1,30))+ geom_curve(x = Longitude_from,y = Latitude_from,xend = Longitude_to,yend = Latitude_to), arrow = arrow(angle = 15,ends =first,length = unit(0.7,cm),type =closed),size = df $ scaledAmount,alpha = 0.5,curvature = 0.15)+ theme_bw() 这正是我想要的,现在我只想添加所需的ggmap作为底层。 现在我只需用g替换ggplot() gmap ggmap_with_curve geom_point(aes(x = Longitude,y = Latitude,size =范围= c(1,30))+ geom_curve(data = df,aes(x = Longitude_from)= scaledkWh),color =red,data = df_vertices,alpha = 0.5)+ scale_size_continuous ,y = Latitude_from,xend = Longitude_to,yend = Latitude_to), arrow = arrow(angle = 15,ends =first,length = unit(0.7,cm),type =closed size = df $ scaledAmount,alpha = 0.5,curvature = 0.15) 正如你所看到的,这不是我期望的输出,R给了我这个错误信息: geom_curve未用于非线性坐标。 我试过谷歌它,但我coudnt自己修复它。 所以我的问题是如何将这个ggplot输出与所需的地图一起作为底层。任何人都可以帮我解决问题吗?解决方案我想用 coord_cartesian()给你你想要的。在你的文章中显示5行 map< - get_map(location =Amsterdam,zoom = 11) ggmap(map)+ geom_point(data = df_vertices, aes(x = Longitude,y = Latitude,size = scaledkWh), color =red,alpha = 0.5) + geom_curve(data = df, aes(x = Longitude_from,y = Latitude_from,xend = Longitude_to,yend = Latitude_to), arrow = arrow(angle = 15,ends =first ,length = unit(0.5,cm),type =closed), size = df $ scaledAmount,alpha = 0.5,curvature = 0.15,inherit.aes = TRUE) scale_size_continuous范围= c(1,30))+ coord_cartesian() I would like to make a map of the Netherlands with curved lines between cities. I have two dataframes number one called: df_verticles which contains 24 cities with their lat/lon combination. The second dataframe called: df i want to use to draw a curved line between lat/lon from combination to the lat/lon to combination.> head(df_vertices) city AmountSessions totalkWh AmountRFID scaledAmount scaledkWh Latitude Longitude1 Alkmaar 13608 104554.68 1326 0.07139012 0.026941910 52.63903 4.7555382 Almere 11281 100841.42 930 0.05006999 0.025985067 52.39447 5.2820433 Amersfoort 7719 67663.30 1198 0.06449876 0.017435647 52.15108 5.3830694 Amstelveen 25794 236437.93 2616 0.14084204 0.060925915 52.31724 4.8592665 Amsterdam 402365 3880744.86 18574 1.00000000 1.000000000 52.34560 4.808834> head(df)CityChargeSessions NextCity Amount sumkWh scaledAmount scaledkWh Latitude_from Longitude_from Latitude_to Longitude_to1 Amsterdam Alkmaar 1058 8133.736 0.18438480 0.15480933 52.34560 4.808834 52.63903 4.7555382 Amsterdam Almere 998 7254.133 0.17392820 0.13806786 52.34560 4.808834 52.39447 5.2820433 Amsterdam Amersfoort 566 4977.404 0.09864064 0.09473489 52.34560 4.808834 52.15108 5.3830694 Amsterdam Amstelveen 3724 24210.289 0.64900662 0.46079423 52.34560 4.808834 52.31724 4.8592665 Almere Amsterdam 1034 8685.526 0.18020216 0.16531155 52.39447 5.282043 52.34560 4.8088346 Amersfoort Amsterdam 579 4936.823 0.10090624 0.09396251 52.15108 5.383069 52.34560 4.808834Normally ggmap is just an underlayer of a ggplot so i decided to plot my desired plot first in ggplot:ggplot_curve<- ggplot()+geom_text(data=df_vertices, aes(x = Longitude, y = Latitude+0.025, label = df_vertices$city), size=6)+geom_point(aes(x = Longitude, y = Latitude, size=scaledkWh), colour="red", data = df_vertices, alpha =0.5)+scale_size_continuous(range=c(1,30))+geom_curve(data=df, aes(x=Longitude_from, y=Latitude_from, xend=Longitude_to, yend=Latitude_to), arrow=arrow(angle=15,ends="first",length=unit(0.7,"cm"),type="closed"), size= df$scaledAmount,alpha=0.5, curvature = 0.15)+theme_bw()This is exactly what i want, now i just want to add the desired ggmap as underlayer.Now i just replace ggplot() by ggmapggmap_with_curve<- ggmap(map)+geom_point(aes(x = Longitude, y = Latitude, size=scaledkWh), colour="red", data = df_vertices, alpha =0.5)+scale_size_continuous(range=c(1,30))+ geom_curve(data=df, aes(x=Longitude_from, y=Latitude_from, xend=Longitude_to, yend=Latitude_to), arrow=arrow(angle=15,ends="first",length=unit(0.7,"cm"),type="closed"), size= df$scaledAmount,alpha=0.5, curvature = 0.15)As you can see this is not the desired output i was hoping for, R gave me this error message: geom_curve is not implemented for non-linear coordinates.I tried to google it, but i coudnt fix it myself.So my question how do i get this ggplot output with the desired map as underlayer. Could anyone help me out? 解决方案 I think using coord_cartesian() give you what you want. Using the 5 rows showed in your postmap <- get_map(location = "Amsterdam", zoom = 11)ggmap(map) + geom_point(data = df_vertices, aes(x = Longitude, y = Latitude, size = scaledkWh), colour = "red", alpha =0.5) + geom_curve(data = df, aes(x = Longitude_from, y = Latitude_from, xend = Longitude_to, yend = Latitude_to), arrow = arrow(angle = 15, ends = "first", length = unit(0.5, "cm"), type = "closed"), size = df$scaledAmount, alpha = 0.5, curvature = 0.15, inherit.aes = TRUE)scale_size_continuous(range=c(1,30)) +coord_cartesian() 这篇关于在ggmap中绘制曲线,geom_curve不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 12:50