[英]radar chart /spider diagram with ggplot2
I am trying to make a radar chart with ggplot2. 我正在尝试使用ggplot2制作雷达图。
> Test1
Country fertility gengap LEB
1 Colombia 1.92 0.29 79.30
2 Peru 1.94 0.37 78.40
3 Min 9.23 3.83 0.00
4 Max 1.59 23.70 78.43
#normalizing the data:
Test1_norm<- data.frame(sapply(Test1[1:4, 2:4], scales::rescale))
> Test1_norm
fertility gengap LEB
1 0.04319372 0.000000000 1.0000000
2 0.04581152 0.003417343 0.9886507
3 1.00000000 0.151217428 0.0000000
4 0.00000000 1.000000000 0.9890290
I found this code to plot a radar chart: http://www.cmap.polytechnique.fr/~lepennec/R/Radar/RadarAndParallelPlots.html for the mtcars-example: 我找到了这段代码来绘制雷达图: http ://www.cmap.polytechnique.fr/~lepennec/R/Radar/RadarAndParallelPlots.html用于mtcars示例:
ggplot(mtcarsmelted, aes(x = variable, y = value)) +
geom_path(aes(group = model, color = model), size = 2) +
theme(strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = rel(0.8)),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
xlab("") + ylab("") +
guides(color = guide_legend(ncol=2)) +
coord_polar()
Using the mtcars example everything works fine for me, but when I try to rewrite the code for my data I just get errors. 使用mtcars示例对我来说一切正常,但是当我尝试为数据重写代码时,我只会出错。 I was trying for several hours now and I am not sure which variables I need to exchange and which have to stay like in the example.
我已经尝试了几个小时,但是我不确定需要交换哪些变量,必须像示例中那样保留哪些变量。
I would appreciate a little help a lot! 我将不胜感激! I am still very new to R ;)
我对R还很陌生;)
Thanks everybody :) 谢谢大家 :)
First, using normal data, I attempt to plot radar charts according to the method you display to me. 首先,我使用正常数据尝试根据您显示给我的方法绘制雷达图。 reshape2 package is what you need.
reshape2软件包是您所需要的。 Here's the code:
这是代码:
Test1_norm <- data.frame(group=c("Colombia","Peru","Min","Max"),
fertility=c(0.04319372,0.04581152,1,0),
gengap=c(0,0.003417343,0.151217428,1),
LEB=c(1,0.9886507,0,0.9890290))
Test1_norm$model <- Test1_norm$group
library(reshape2)
Test1_plot<- melt(Test1_norm)
##Define a radar coordinate system
coord_radar <- function (theta = "x", start = 0, direction = 1)
{
theta <- match.arg(theta, c("x", "y"))
r <- if (theta == "x")
"y"
else "x"
ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start,
direction = sign(direction),
is_linear = function(coord) TRUE)
}
#Plot
library(ggplot2)
ggplot(Test1_plot, aes(x = variable, y = value)) +
geom_polygon(aes(group = model, color = model), fill = NA, size = 2, show.legend = FALSE) +
geom_line(aes(group = model, color = model), size = 2) +
theme(strip.text.x = element_text(size = rel(0.8)),
axis.text.x = element_text(size = rel(0.8)),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
xlab("") + ylab("") +
guides(color = guide_legend(ncol=2)) +
coord_radar()
Fortunately, Ricardo Bion provided an efficient methods to create radar charts with ggplot2.This may be helpful to you: https://github.com/ricardo-bion/ggradar You can install the package and call ggradar()
function as introduced in the manual.It's my code with original data. 幸运的是,里卡多·比昂(Ricardo Bion)提供了一种使用ggplot2创建雷达图的有效方法,这可能对您有所帮助: https : //github.com/ricardo-bion/ggradar您可以安装该软件包并调用
ggradar()
函数,如手册。这是我的原始数据代码。
Test1 <- data.frame(fertility=c(1.92,1.94,9.23,1.59),
gengap=c(0.29,0.37,3.83,23.70),
LEB=c(79.30,78.40,0.00,78.43))
rownames(Test1)<-c("Colombia","Peru","Min","Max")
source("ggradar.R")
library(dplyr)
library(ggplot2)
library(scales)
Test1 %>%
add_rownames( var = "group" ) %>%
mutate_each(funs(rescale), -group) -> Test1_radar
ggradar( Test1_radar)
ggsave("Test1_radar.tiff",width = 8, height = 8)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.