本文介绍了如何测量ggplot2中多边形的面积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好,我有一些样本,我想为它们绘制一个多边形来说明数据的形状。我的数据看起来像这样: 01 0.31707317 02 0.12195122 03 0.09756098 04 0.07317073 0.07317073 06 0.07317073 $ b $ 07 0.07317073 08 0.07317073 09 0.04878049 $ b $ 10 0.04878049 我可以使用radarchart轻松绘制雷达图,如下所示: 但我试图测量结果形状的面积并将其用作数据形状的度量。这是我奋斗的地方。 我尝试将结果图保存为矢量并使用点,但看起来像我可以不要将图表传递给矢量。然后我尝试了rgdal包来将我的图形显示为shapefile并使用坐标: coorddf< - SpatialPointsDataFrame(radarchart( as.data.frame(ttradar),pcol = rgb(0.2,0.5,0.5),pfcol = rgb(0.2,0.5,0.5,0.2)),data = radarchart(as.data.frame(ttradar) ,pcol = rgb(0.2,0.5,0.5),cglcol =white,pfcol = rgb(0.2,0.5,0.5,0.2)) writeOGR(coorddf,dsn ='。',layer ='mypoints',driver =ESRI Shapefile) 这不是一个好主意,因为我的数据没有可以用作经纬度的值。 有什么建议吗? 解决方案展开 如果你正在做很多这些,值得看看 ggradar 包: http://www.ggplot2- exts.org/ggra dar.html 因为我只是做这个一次性工作,所以我使用了Erwan Le Pennec的极坐标修改: http://www.cmap.polytechnique.fr/~lepennec/R/Radar /RadarAndParallelPlots.html coord_radar { theta< - match.arg(theta,c(x,y))r y elsex ggproto(CoordRadar,CoordPolar,theta = theta,r = r,start = start, direction = sign(方向), is_linear = function(coord)TRUE)}Hi everyone,I have a number of samples that I would like to draw a polygon for each of them to illustrate the shape of the data. My data look likes this:01 0.3170731702 0.1219512203 0.0975609804 0.0731707305 0.0731707306 0.0731707307 0.0731707308 0.0731707309 0.0487804910 0.04878049I can easily draw a radar chart using radarchart, which looks like this:But I am trying to measure the area of the results shape and use that as a measure of data shape. This is where I struggle.I tried to save the resulting figure as a vector and use the points there but it looks like I can not pass the chart into a vector. Then I tried rgdal package to exprt my figure as a shapefile and use the coordinates there:coorddf <- SpatialPointsDataFrame(radarchart(as.data.frame(ttradar), pcol=rgb(0.2,0.5,0.5) , pfcol=rgb(0.2,0.5,0.5, 0.2)), data = radarchart(as.data.frame(ttradar), pcol=rgb(0.2,0.5,0.5) , cglcol = "white", pfcol=rgb(0.2,0.5,0.5, 0.2))writeOGR(coorddf, dsn = '.', layer = 'mypoints', driver = "ESRI Shapefile")Which was not a good idea because my data does not have values that can be used as lat and long points..Any suggestions? 解决方案 To expand on @G5W's excellent point:library(dplyr)library(ggplot2)df <- structure( list( V1 = 1:10, V2 = c( 0.31707317, 0.12195122, 0.09756098, 0.07317073, 0.07317073, 0.07317073, 0.07317073, 0.07317073, 0.04878049, 0.04878049 ) ), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, -10L))You can calculate each triangle from its neighbor to the right using dplyr::lead:areas <- df %>% setNames(c("variable", "value")) %>% mutate(nextval = lead(value, default = value[1]), angle = (1/10) * (2*pi), # change 1/n to number of variables area = value*nextval*sin(angle)/2) variable value nextval angle area1 1 0.31707317 0.12195122 0.6283185 0.01136408132 2 0.12195122 0.09756098 0.6283185 0.00349664063 3 0.09756098 0.07317073 0.6283185 0.00209798434 4 0.07317073 0.07317073 0.6283185 0.00157348815 5 0.07317073 0.07317073 0.6283185 0.00157348816 6 0.07317073 0.07317073 0.6283185 0.00157348817 7 0.07317073 0.07317073 0.6283185 0.00157348818 8 0.07317073 0.04878049 0.6283185 0.00104899219 9 0.04878049 0.04878049 0.6283185 0.000699328110 10 0.04878049 0.31707317 0.6283185 0.0045456327A couple things: notice that I used the default = value[1] to make sure that the NA that would be caused at the end to wrap around to using the first value instead. Also you need to use angles in radians, so that's just 1/n * 2pi. Now that we have all the triangle areas, we can add them:areas %>% summarise(total = sum(area)) total1 0.02954661This approach is easily extended to multiple groups to compare.df <- expand.grid(var = 1:8, grp = c("a", "b")) %>% mutate(value = runif(length(var), 0.25, 1)) %>% group_by(grp) %>% mutate(nextval = lead(value, default = value[1]), angle = (1/8)*(2*pi), area = value*nextval*sin(angle)/2) %>% mutate(total = sum(area))# A tibble: 16 x 7# Groups: grp [2] var grp value nextval angle area total <int> <fctr> <dbl> <dbl> <dbl> <dbl> <dbl> 1 1 a 0.3101167 0.6831233 0.7853982 0.07489956 0.5689067 2 2 a 0.6831233 0.4166692 0.7853982 0.10063417 0.5689067 3 3 a 0.4166692 0.4756976 0.7853982 0.07007730 0.5689067 4 4 a 0.4756976 0.3426595 0.7853982 0.05763002 0.5689067 5 5 a 0.3426595 0.3107870 0.7853982 0.03765135 0.5689067 6 6 a 0.3107870 0.3001208 0.7853982 0.03297721 0.5689067 7 7 a 0.3001208 0.9039894 0.7853982 0.09592115 0.5689067 8 8 a 0.9039894 0.3101167 0.7853982 0.09911594 0.5689067 9 1 b 0.9888119 0.3481213 0.7853982 0.12170243 1.174978910 2 b 0.3481213 0.8513316 0.7853982 0.10478143 1.174978911 3 b 0.8513316 0.9928401 0.7853982 0.29883611 1.174978912 4 b 0.9928401 0.6372992 0.7853982 0.22370605 1.174978913 5 b 0.6372992 0.8303906 0.7853982 0.18710303 1.174978914 6 b 0.8303906 0.3607232 0.7853982 0.10590379 1.174978915 7 b 0.3607232 0.2786354 0.7853982 0.03553575 1.174978916 8 b 0.2786354 0.9888119 0.7853982 0.09741033 1.1749789df %>% ggplot(aes(var, value)) + geom_polygon() + geom_text(aes(0,0, label = round(total, 2)), color = "white") + facet_grid(~grp) + scale_y_continuous("", limits = c(0, 1), expand = c(0,0)) + scale_x_continuous("", breaks = 1:8, expand = c(0,0)) + theme_minimal() + coord_radar()If you're doing a lot of these, it's worth looking at the ggradar package: http://www.ggplot2-exts.org/ggradar.htmlSince I was just doing this one-off, I used a polar coordinate modification from Erwan Le Pennec:http://www.cmap.polytechnique.fr/~lepennec/R/Radar/RadarAndParallelPlots.htmlcoord_radar <- function (theta = "x", start = 0, direction = 1){ theta <- match.arg(theta, c("x", "y")) r <- if (theta == "x") "y" else "x" ggproto("CoordRadar", CoordPolar, theta = theta, r = r, start = start, direction = sign(direction), is_linear = function(coord) TRUE)} 这篇关于如何测量ggplot2中多边形的面积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 07:26