如何在ggplot2或R中绘制雷达图 [英] How to plot a Radar chart in ggplot2 or R
本文介绍了如何在ggplot2或R中绘制雷达图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图制作一个雷达图,如附件图片所示和ggplot2(或R中的任何其他软件包)。谈论这个,但我的情况是不同的,因为我试图创建一个具有一定范围的响应数据的蜘蛛情节。
我使用下面的代码做了一个情节,但我无法弄清楚如何在图像中做出这种情况。
影响< - c(system,supply,security,well (c)(5,5,3,5)
过去<-c(6,6,4,5)
group.names< - c(present,past)
ddf.pre < - data.frame(matrix(c(rep(group.names [1],4),Impuls),nrow = 4,ncol = 2 ),var.order = seq(1:4),value = present)
ddf.pas < - data.frame(matrix(c(rep(group.names [2],4),Impcts), (ddf.pre,ddf.pas)
(ddf.pre,ddf.pas)(nf = 4,ncol = 2),var.order = seq(1:4),value = past)
ddf < - rbind < - c(Group,Impcts,var.order,var.value)
library(ggplot2)
ggplot(ddf,aes(y = var.value,x = reorder(Impcts,var.order),
group = Group,color = Group))+
coord_polar()+
geom_path()+
geom_point()+
labs(title =影响分析)。
绘制了方格, geom_path()。然后,我使用 geom_polygon()
绘制了两个多边形。
###绘制方格
mydf< - data.frame(id = rep( 1:6,每个= 5),
x = c(0,6,0,-6,0,
0,5,0,-5,0,
0,4, 0,-4,0,
0,3,0,-3,0,
0,2,0,-2,0,
0,1,0,-1, 0),
y = c(6,0,6,0,6,
5,0,-5,0,5,
4,0,-4,0,4 ,
3,0,-3,0,3,
2,0,-2,0,2,
1,0,-1,0,1))
g < - ggplot(data = mydf,aes(x = x,y = y,group = factor(id))+
geom_path()
###绘制多边形
mydf2< - data.frame(id = rep(7:8,each = 5),
x = c(0,6,0,-5,0,
0 ,5,0,-5,0),
y = c(6,0,-4,0, 6,
5,0,3,0,5))
gg < - g +
geom_polygon(data = mydf2,aes(x = x,y = y,group = factor(id),fill = factor(id)))+
scale_fill_manual(name =Time,values = c(darkolivegreen4,brown4),
labels = c (过去,当前))
###添加注释
mydf3< - data.frame(x = c(0,6.5 ,0,-6.5),
y = c(6.5,0,-6.5,0),
label = c(系统,供应,安全,福祉) )
ggg< - gg +
annotate(text,x = mydf3 $ x,y = mydf3 $ y,label = mydf3 $ label,size = 3 )
ggsave(ggg,file =name.png,width = 10,height = 9)
I am trying to make a Radar plot as in attached image using and ggplot2 ( or any other package in R).This talk about this but my case is different as i am trying to create a spider plot for response data with certain range. I made a plot using a code as below, but i couldn't figure out howto make this like in the image. Kindly help me with this.
Impcts <- c("system","supply","security","well-being")
present <- c(5,5,3,5)
past <- c(6,6,4,5)
group.names <- c("present", "past")
ddf.pre <- data.frame(matrix(c(rep(group.names[1], 4), Impcts), nrow = 4, ncol = 2), var.order = seq(1:4), value = present)
ddf.pas <- data.frame(matrix(c(rep(group.names[2], 4), Impcts), nrow = 4, ncol = 2), var.order = seq(1:4), value = past)
ddf <- rbind(ddf.pre, ddf.pas)
colnames(ddf) <- c("Group", "Impcts", "var.order", "var.value")
library(ggplot2)
ggplot(ddf, aes(y = var.value, x = reorder(Impcts, var.order),
group = Group, colour = Group))+
coord_polar() +
geom_path() +
geom_point()+
labs(title = "Impacts Analysis").
解决方案
Here is my attempt.First I drew squares using geom_path()
. Then, I drew two polygons on top of the squares using geom_polygon()
. Finally I added annotations.
### Draw squares
mydf <- data.frame(id = rep(1:6, each = 5),
x = c(0, 6, 0, -6, 0,
0, 5, 0, -5, 0,
0, 4, 0, -4, 0,
0, 3, 0, -3, 0,
0, 2, 0, -2, 0,
0, 1, 0, -1, 0),
y = c(6, 0, -6, 0, 6,
5, 0, -5, 0, 5,
4, 0, -4, 0, 4,
3, 0, -3, 0, 3,
2, 0, -2, 0, 2,
1, 0, -1, 0, 1))
g <- ggplot(data = mydf, aes(x = x, y = y, group = factor(id)) +
geom_path()
### Draw polygons
mydf2 <- data.frame(id = rep(7:8, each = 5),
x = c(0, 6, 0, -5, 0,
0, 5, 0, -5, 0),
y = c(6, 0, -4, 0, 6,
5, 0, -3, 0, 5))
gg <- g +
geom_polygon(data = mydf2, aes(x = x, y = y, group = factor(id), fill = factor(id))) +
scale_fill_manual(name = "Time", values = c("darkolivegreen4", "brown4"),
labels = c("Past", "Present"))
### Add annotation
mydf3 <- data.frame(x = c(0, 6.5, 0, -6.5),
y = c(6.5, 0, -6.5, 0),
label = c("system", "supply", "security", "well-being"))
ggg <- gg +
annotate("text", x = mydf3$x, y = mydf3$y, label = mydf3$label, size = 3)
ggsave(ggg, file = "name.png", width = 10, height = 9)
这篇关于如何在ggplot2或R中绘制雷达图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文