我正在尝试使用 ggplot2 和地图来绘制纽约state的县名。我的方法是按县查找经纬度的平均值(我假设这是县的中心,但这可能是错误的想法),然后使用 geom_text 在地图上绘制名称。它的行为不像我预期的那样,因为它在每个县绘制了多个名称。
我正在寻找的结果是每个文本(县)的中心位于它各自县的中心。
除了解决问题之外,我希望能帮助理解我对 ggplot 的想法有什么问题。
提前谢谢你。
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | county_df <- map_data('county') #mappings of counties by state ny <- subset(county_df, region=="new york") #subset just for NYS ny$county <- ny$subregion cnames <- aggregate(cbind(long, lat) ~ subregion, data=ny, FUN=mean) p <- ggplot(ny, aes(long, lat, group=group)) + geom_polygon(colour='black', fill=NA) p #p of course plots as expected #now add some county names (3 wrong attempts) p + geom_text(aes(long, lat, data = cnames, label = subregion, size=.5)) #not correct #I said maybe I'm confusing it with the same names for different data sets names(cnames) <-c('sr', 'Lo', 'La') p + geom_text(Lo, La, data = cnames, label = sr, aes(size=.5)) #attempt 2 p + geom_text(aes(Lo, La, data = cnames, label = sr, size=.5)) #attempt 3 |
由于您要创建两个图层(一个用于多边形,第二个用于标签),您需要为每个图层正确指定数据源和映射:
2 3 | geom_polygon(aes(group=group), colour='black', fill=NA) + geom_text(data=cnames, aes(long, lat, label = subregion), size=2) |
注意:
一旦你这样做了,并且地图绘制出来了,你会发现中点更接近于
2
3
4
5
6
7
FUN=function(x)mean(range(x)))
ggplot(ny, aes(long, lat)) +
geom_polygon(aes(group=group), colour='black', fill=NA) +
geom_text(data=cnames, aes(long, lat, label = subregion), size=2) +
coord_map()

我知道这是一个已经回答的老问题,但我想补充一下,以防有人在这里寻求未来的帮助。
地图包有
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | county_df <- map_data('county') #mappings of counties by state ny <- subset(county_df, region=="new york") #subset just for NYS ny$county <- ny$subregion cnames <- aggregate(cbind(long, lat) ~ subregion, data=ny, FUN=mean) # Use the map function to get the polygon data, then find the centroids county_poly <- map("county","new york", plot=FALSE, fill = TRUE) county_centroids <- maps:::apply.polygon(county_poly, maps:::centroid.polygon) # Create a data frame for graphing out of the centroids of each polygon # with a non-missing name, since these are the major county polygons. county_centroids <- county_centroids[!is.na(names(county_centroids))] centroid_array <- Reduce(rbind, county_centroids) dimnames(centroid_array) <- list(gsub("[^,]*,","", names(county_centroids)), c("long","lat")) label_df <- as.data.frame(centroid_array) label_df$county <- rownames(label_df) p <- ggplot(ny, aes(long, lat, group=group)) + geom_polygon(colour='black', fill=NA) plabels <- geom_text(data=label_df, aes(label=county, group=county)) p + plabels |
@tjebo 在我尝试创建一个新的统计数据时向我指出,这个统计数据将是这个问题的合适解决方案。 它不在 CRAN 上(还),但生活在 github 上。(免责声明:我写了 ggh4x)
对于处理类似问题的其他人,这是如何工作的:
2 3 4 5 6 7 8 9 10 11 12 13 14 | #> Loading required package: ggplot2 #> Warning: package 'ggplot2' was built under R version 4.0.2 library(maps) county_df <- map_data('county') ny <- subset(county_df, region=="new york") ny$county <- ny$subregion ggplot(ny, aes(x = long, y = lat, group = group)) + geom_polygon(colour='black', fill=NA) + stat_midpoint(aes(label = subregion), geom ="text",size=3) + coord_map() |

由 reprex 包 (v0.3.0) 创建于 2020-07-06
似乎kmeans中心会很有用...这是一个糟糕的开始...为时已晚!
2 3 | center.points$county <- ny$county[ny$group == center.points$group] p + geom_text(data=center.points, aes(x=V1, y=V2, label=county)) |
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
当我创建一个Rails应用程序时,控制台:railsnewfoo我的代码可以使用字符串“foo”吗?puts"Yourapp'snameis"+app_name_bar 最佳答案 Rails.application.class将为您提供应用程序的全名(例如YourAppName::Application)。从那里您可以使用Rails.application.class.parent获取模块名称。 关于ruby-on-rails-应用程序的名称是否可以作为变量使用?,我们在StackOve
已经有一个问题回答了如何将“America/Los_Angeles”转换为“PacificTime(US&Canada)”。但是我想将“美国/太平洋”和其他过时的时区转换为RailsTimeZone。我无法在图书馆中找到任何可以帮助我完成此任务的东西。 最佳答案 来自RailsActiveSupport::TimeZonedocs:TheversionofTZInfobundledwithActiveSupportonlyincludesthedefinitionsnecessarytosupportthezonesdefinedb
如thisquestion,当在其自己的赋值中使用未定义的局部变量时,它的计算结果为nil。x=x#=>nil但是当局部变量的名称与现有的方法名称冲突时,就比较棘手了。为什么下面的最后一个示例返回nil?{}.instance_eval{a=keys}#=>[]{}.instance_eval{keys=self.keys}#=>[]{}.instance_eval{keys=keys}#=>nil 最佳答案 在Ruby中,因为可以在没有显式接收器和括号的情况下调用方法,所以在局部变量引用和无接收器无参数方法调用之间存在语法歧义:f
我有一个非常简单的Controller来管理我的Rails应用程序中的静态页面:classPagesController我怎样才能让View模板返回它自己的名字,这样我就可以做这样的事情:#pricing.html.erb#-->"Pricing"感谢您的帮助。 最佳答案 4.3RoutingParametersTheparamshashwillalwayscontainthe:controllerand:actionkeys,butyoushouldusethemethodscontroller_nameandaction_nam
对于用户模型,我有一个过滤器来检查用户的预订状态,该状态由整数值(0、1或2)表示。UserActiveAdmin索引页上的过滤器是通过以下代码实现的:filter:booking_status,as::select然而,这会导致下拉选项为0、1或2。当管理员用户从下拉列表中选择它们时,我更愿意自己将它们命名为“未完成”、“待定”和“已确认”之类的名称。有没有办法在不改变booking_status在模型中的表示方式的情况下做到这一点? 最佳答案 假设booking_status是模型中的枚举字段,您可以使用:过滤器:booking
我在我的rails应用程序中安装了来自github.com的acts_as_versioned插件,但有一段代码我不完全理解,我希望有人能帮我解决这个问题class_eval我知道block内的方法(或任何它是什么)被定义为类内的实例方法,但我在插件的任何地方都找不到定义为常量的CLASS_METHODS,而且我也不确定是什么here,并且有问题的代码从lib/acts_as_versioned.rb的第199行开始。如果有人愿意告诉我这里的内幕,我将不胜感激。谢谢-C 最佳答案 这是一个异端。http://en.wikipedia
如何只加载map边界内的标记gmaps4rails?当然,在平移和/或缩放后加载新的。与此直接相关的是,如何获取map的当前边界和缩放级别? 最佳答案 我是这样做的,我只在用户完成平移或缩放后替换标记,如果您需要不同的行为,请使用不同的事件监听器:在你看来(index.html.erb):{"zoom"=>15,"auto_adjust"=>false,"detect_location"=>true,"center_on_user"=>true}},false,true)%>在View的底部添加:functiongmaps4rail
在Ruby中有运算符(operator)。在API中,他们没有命名它的名字,只是:Theclassmustdefinetheoperator...Comparableusestoimplementtheconventionalcomparison......theobjectsinthecollectionmustalsoimplementameaningfuloperator...它叫什么名字? 最佳答案 参见上面的@Tony。然而,它也被称为(俚语)“宇宙飞船运算符(operator)”。
在我的场景中,Logstash收到的系统日志行的“时间戳”是UTC,我们在Elasticsearch输出中使用事件“时间戳”:output{elasticsearch{embedded=>falsehost=>localhostport=>9200protocol=>httpcluster=>'elasticsearch'index=>"syslog-%{+YYYY.MM.dd}"}}我的问题是,在UTC午夜,Logstash在外时区(GMT-4=>America/Montreal)结束前将日志发送到不同的索引,并且索引在20小时(晚上8点)之后没有日志,因为“时间戳”是UTC。我们已