本文介绍了修改r中雷达图的现有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想用R制作雷达图,并在网络上找到下面的功能。 链接到网站 看起来不错,但是我想传递一个数值从0到1的数据框,并用百分比来缩放图表。我需要帮助才能实现它...... 以下是我在页面上找到的数据和功能。 CreateRadialPlot< - 函数(plot.data, axis.labels = colnames(plot.data)[ - 1], grid.min = -0.5,#10, grid.mid = 0,#50, grid.max = 0.5,#100, centre.y = grid.min - ((1/9 )*(grid.max-grid.min)), plot.extent.x.sf = 1.2, plot.extent.y.sf = 1.2, x.centre.range = 0.02 *(grid.max-centre.y), label.centre.y = FALSE, grid.line.width = 0.5, gridline.min.linetype =longdash , gridline.mid.linetype =longdash, gridline.max.linetype =longdash, gridline.min.colour =gray, gridline.mid.colour =blue, gridline.max.colour =gray , grid.label.size = 4, gridline.label.offset = -0.02 *(grid.max-centre.y), label.gridline.min = TRUE, axis.label.offset = 1.15, axis.label.size = 3, axis.line.colour =gray, group.line.width = 1, group.point.size = 4, background.circle.colour =yellow, background.circle.transparency = 0.2, plot.legend = if(nrow(plot .data)> 1)TRUE else FALSE, legend.title =Cluster, legend.text.size = grid.label.size){ var.names< - colnames(plot.data)[ - 1]#'变量名的简短版本 #axis .labels [如果提供]设计用于保存变量名称的'长版本'#具有使用\ n 指示的换行符#caclulate总绘图程度作为外圆半径xa用户可指定的比例因子 plot.extent.x =(grid.max + abs(centre.y))* plot.extent.x.sf plot.extent.y =(grid.max + abs(centre.y))* plot.extent.y.sf #检查提供的数据是否合理 if(length(axis.labels)!= ncol(plot.data) - 1) return(Error:'axis.labels'contains the wrong number of axis labels) if(min(plot.data [, - 1]) return(错误:plot.data'包含值(s)< (plot.data [, - 1])> grid.max) return(Error:'plot.data'contains value(s)> grid .max) #Declare所需的内部函数 CalculateGroupPath< - function(df){#将变量值转换为一组径向xy坐标 #Code由Tony M发布的解决方案改编为 #http://stackoverflow.com/questions/9614433/creating-radar-chart-aka-star-plot-spider-plot-using-ggplot2 in-r #Args:#df:Col 1 - 组(实体的'unique'簇/组ID)#Col 2-n:v1.value到vn.value - 变量v1到vn path< - as.factor(as.character(df [,1]))$ b $的值(例如组/队长平均值或中值) b ##找到增量 angles = seq(from = 0,to = 2 * pi,by =(2 * pi)/(ncol(df)-1)) ##创建图形数据框 graphData = data.frame(seg =,x = 0,y = 0) graphData = graphData [-1,] (我在等级中(路径)){ pathData = subset(df,df [,1] == i) for(j in c(2:ncol(df))) { #pathData [,j] = pathData [,j] graphData = rbind(graphData,data.frame(group = i,x = pathData [ ,j] * sin(angles [j-1]),y = pathData [,j] * cos(angles [j-1])))} ##完成路径通过在路径中重复第一对坐标 graphData = rbind(graphData,data.frame(group = i,x = pathData [,2] * sin(angles [1]),y = pathData [,2] * cos(angles [1]))) } #确保第一列的名称与输入数据的名称相匹配(万一! =group) colnames(graphData)[1]< - colnames(df)[1] graphData#函数返回的数据帧 } CaclulateAxisPath = function(var.names,min,max){ #计算一组径向轴的xy坐标(每个变量一个绘制在雷达图中) #Args:#var.names - 要绘制的变量列表在雷达图上 #min - 绘制轴所需的MININUM值(相同的值将应用于所有轴) #max - 绘制轴所需的最大值(相同值将应用于所有轴) #var.names n.vars< - length var.names)#所需变量(轴)的数量 #计算所需角度数(弧度)角度 #计算最小和最大x + y坐标向量 min.x min.y max.x max.y #组合成一组唯一编号的路径(每个变量一个) axisData< - NULL for(i in 1:nv (i,min.x [i],min.y [i])b axisData< - rbind(axisData,a,b)} #添加列名+设置行名=行号。以允许转换成数据帧 colnames(axisData)< -c(axis.no,x,y) rownames(axisData)< - seq(1:nrow (axisData)) #返回计算的轴路径 as.data.frame(axisData)} funcCircleCoords< - 函数(center = c(0,0),r = 1,npoints = 100){#从Joran的回复中调用http://stackoverflow.com/questions/6862742/draw-a-circle-with- ggplot2 tt xx yy return(data.frame(x = xx,y = yy))} ###将提供的数据转换为可绘制格式 #(a)将ABS(centre.y)添加到提供的绘图数据#[为内部使用创建绘图质心0,0,而不管min。用户提供的数据中的y #的值] plot.data.offset< - plot.data plot.data.offset [,2:ncol(plot.data)]< ; - plot.data [,2:ncol(plot.data)] + abs(centre.y) #print(plot.data.offset) #(b)转换成径向坐标组< -NULL 组$ path< - CalculateGroupPath(plot.data.offset) #print(group $ path) #( c)计算绘制径向变量轴所需的坐标 axis axis $ path #print(axis $ path) #(d)创建包含轴标签的文件+相关的绘图坐标 #Labels 轴$ label< - data.frame( text = axis.labels,x = NA,y = NA) #print(axis $ label) #axis标签坐标 n.vars< - length(var.names) angles = seq(from = 0,to = 2 * pi,by =(2 * pi )/n.vars)轴$ label $ x< - sapply(1:n.vars,function( i,x){((grid.max + abs(centre.y))* axis.label.offset)* sin(angles [i])}) axis $ label $ y< - sapply(1 :n.vars,function(i,x){((grid.max + abs(centre.y))* axis.label.offset)* cos(angles [i])}) #print(axis $标签) #(e)创建圆形网格线+标签 #使用三个用户指定的绘制圆形网格线所需的坐标系#y轴的值:min,mid和max [grid.min; grid.mid; grid.max] gridline< - NULL gridline $ min $ path< - funcCircleCoords(c(0,0),grid.min + abs(centre.y),npoints = 360) gridline $ mid $ path< - funcCircleCoords(c(0,0),grid.mid + abs(centre.y),npoints = 360) gridline $ max $ path #print(head(gridline $ max $ path)) #gridline labels gridline $ min $ label< - data.frame(x = gridline.label.offset,y = grid.min + abs(centre.y), text = as.character(grid.min)) gridline $ max $ label< - data.frame(x = gridline.label.offset,y = grid.max + abs(centre.y), text = as.character(grid.max )) gridline $ mid $ label< - data.frame(x = gridline.label.offset,y = grid.mid + abs(centre.y), text = as.character(grid .mid)) #print(gridline $ min $ label) #print(gridline $ max $ label) #print(gridline $ mid $ label) ###明星t建立雷达图 #Delcare'theme_clear',有或没有用户要求的绘图图例#[默认=没有图例,如果只绘制1组[绘制路径]] theme_clear< - theme_bw()+ theme(axis.text.y = element_blank(), axis.text.x = element_blank(), axis.ticks = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), legend。 key = element_rect(linetype =blank)) if(plot.legend == FALSE)theme_clear< - theme_clear + theme(legend.position =none) #Base-layer =轴标签+绘图范围#[也需要声明绘图范围,因为轴标签并不总是#适合由ggplot自动计算的绘图区域,甚至如果所有#包含在第一个阴谋;并且在任何情况下,这里所遵循的策略是,首先#将轴标签的右对齐标签绘制到Y轴左侧,以便x #然后几乎在x = 0之上/之下的轴标签的居中标签#[abs(x)< x.centre.range];然后左对齐的轴标签位于Y轴的右侧[x> 0]。 #这种在图层中构建图层不允许ggplot正确#在绘制第一层(基本层)时标识绘图范围] #base layer = axis labels for轴在中心y轴的左侧[x base< - ggplot(axis $ label)+ xlab(NULL)+ ylab(NULL)+ coord_equal()+ geom_text(data = subset(axis $标签,轴$ label $ x aes(x = x,y = y,label = text),size = axis.label.size,hjust = 1) + scale_x_continuous(limits = c(-plot.extent.x,plot.extent.x))+ scale_y_continuous(limits = c(-plot.extent.y,plot.extent.y)) #+任何垂直轴的轴标签[abs(x) base aes(x = x,y = y,label = text),size = axis.label.size,hjust = 0.5) #+任何垂直轴的轴标签[x> x.centre.range] base< - base + geom_text(data = subset(axis $ label,axis $ label $ x> x.centre.range), aes(x = x,y = y,label = text),size = axis.label.size,hjust = 0) #+ theme_clear [去除灰色背景,网格线,轴刻度标记d轴文本] base< - base + theme_clear #+用于绘制雷达数据的背景圆圈 base< - base + geom_polygon(data = gridline $ max $ path,aes(x,y), fill = background.circle.colour, alpha = background.circle.transparency) #+径向轴 base color = axis.line.colour) #... + group(cluster)'paths' base size = group.line.width) #... +组点数(集群数据) base #... +修正图例标题 if(plot。图例== TRUE)base< - base + labs(color = legend.title,size = legend.text.size) # ... +在'min','mid'和'max'y轴值处的圆形网格线 base lty = gridline.min.linetype,color = gridline.min.colour,size = grid.line.width) base< - base + geom_path(data = gridline $ mid $ path,aes(x = x,y = y), lty = gridline.mid.linetype,color = gridline.mid.colour,size = grid.line.width) base< - base + geom_path(data = gridline $ max $ path,aes(x = x,y = y), lty = gridline.max.linetype,color = gridline.max.colour,size = grid.line.width ) #... +网格线标签(max; AVE;分钟)[只加分钟。如果需要,网格线标签] if(label.gridline.min == TRUE){ base< - base + geom_text(aes(x = x,y = y,label = text) gridline $ min $ label,face =bold,size = grid.label.size,hjust = 1)} base< - base + geom_text(aes(x = x,y = y,label = text ),data = gridline $ mid $ label,face =bold,size = grid.label.size,hjust = 1) base< - base + geom_text(aes(x = x,y = y, label = text),data = gridline $ max $ label,face =bold,size = grid.label.size,hjust = 1) #... + centre.y label if if required [即如果(label.centre.y == TRUE){ centre.y.label< - data.frame(x = 0,y = 0,text = as.character(centre.y)) base< - base + geom_text(aes(x = x,y = y,label = text),data = centre.y.label,face =bold ,size = grid.label.size,hjust = 0.5)} return(base) } #(1 )定义绘图所需的数据构建块[使用#绘制上面的OAC结果的子集] var.names HE资格,日常/半日常职业,2+汽车家庭, (1:9) values.a 0.1099018,-0.05310315,0.0182626) values.b -c(0.2808439,-0.2936949,-0.1925846,0.08910815,-0.0346 8011,0.07385727, -0.07228813,0.1501105,-0.06800127) values.c group.names #(2)创建df1:以ggplot2所需格式绘制数据框 df1.a < - data.frame(matrix(c(rep(group.names [1],9),var.names),nrow = 9,ncol = 2), var.order = var.order,value = values.a) df1.b< - data.frame(matrix(c(rep(group.names [2],9),var.names),nrow = 9,ncol = 2), var.order = var.order,value = values.b) df1.c< - data.frame(matrix(c(rep(group.names [3],9)),var.names ),nrow = 9,ncol = 2), var.order = var.order,value = values.c) df1 colnames(df1)< -c(group,variable.name,variable.order,variable.value) df1 #(4)创建df2:以#fu所需的格式绘制数据框ncRadialPlot $ b m2< - 矩阵(abs(c(values.a,values.b)),nrow = 2,ncol = 9,byrow = TRUE) group.names< - c(group.names [1:2]) df22< - data.frame(group = group.names,m2) colnames(df22)[2:10]< - var。名称 print(df22) #(6)使用函数CreateRadialPlot创建径向图,最小#绘制中心的y值 CreateRadialPlot( df22,plot.extent.x = 1.5,grid.min = -0.4,centre.y = -0.5, label.centre.y = TRUE,label.gridline.min = FALSE) 输出: 我想将包含0到1列中的值的数据框传递给函数,并在图表中生成百分比刻度。如果可能的话,网格上还会显示比例尺(0,10 .... 90,100)。在例子中作为例子: m2 group.names< -c(group.names [1:2]) df22 colnames(df22)[2:10]< - var.names print(df22) 解决方案 您也可以使用 rCharts 包来制作这种情节。有很多选项,您可以更容易地定制它。 它是您第一次使用rCharts,您应该执行以下设置: p> install.packages('devtools') require(devtools) install_github('rCharts','ramnathv ') 以下是一个示例代码: library(rCharts)#创建数字范围从0到1的虚拟数据框 df #muliply数字乘以100得到百分比 df [, - 1]< -df [, - 1] * 100 plot plot $ chart(polar = TRUE, (b = $); plot $ xAxis(gridLineInterpolation ='circle',lineWidth ='line',height = 500) plot $ xAxis(categories = df $ id,tickmarkPlacement ='on',lineWidth = 0,min = 0,max = 100,endOnTick = T,tickInterval = 10) plot $ series(data = df [,val1],name =Series 1,pointPlacement =on) plo t $ series(data = df [,val2],name =Series 2,pointPlacement =on) plot 输出如下所示: I would like to make a radar plot using R and found the function below on the web.Link to siteLooks pretty good , however I would like to pass a dataframe with values from 0 to 1 and scale the chart with percentages instead. I need help to make it happen though...Here is the data and the function that I found on the page.CreateRadialPlot <- function(plot.data, axis.labels=colnames(plot.data)[-1], grid.min=-0.5, #10, grid.mid=0, #50, grid.max=0.5, #100, centre.y=grid.min - ((1/9)*(grid.max-grid.min)), plot.extent.x.sf=1.2, plot.extent.y.sf=1.2, x.centre.range=0.02*(grid.max-centre.y), label.centre.y=FALSE, grid.line.width=0.5, gridline.min.linetype="longdash", gridline.mid.linetype="longdash", gridline.max.linetype="longdash", gridline.min.colour="grey", gridline.mid.colour="blue", gridline.max.colour="grey", grid.label.size=4, gridline.label.offset=-0.02*(grid.max-centre.y), label.gridline.min=TRUE, axis.label.offset=1.15, axis.label.size=3, axis.line.colour="grey", group.line.width=1, group.point.size=4, background.circle.colour="yellow", background.circle.transparency=0.2, plot.legend=if (nrow(plot.data)>1) TRUE else FALSE, legend.title="Cluster", legend.text.size=grid.label.size ) { var.names <- colnames(plot.data)[-1] #'Short version of variable names #axis.labels [if supplied] is designed to hold 'long version' of variable names #with line-breaks indicated using \n #caclulate total plot extent as radius of outer circle x a user-specifiable scaling factor plot.extent.x=(grid.max+abs(centre.y))*plot.extent.x.sf plot.extent.y=(grid.max+abs(centre.y))*plot.extent.y.sf #Check supplied data makes sense if (length(axis.labels) != ncol(plot.data)-1) return("Error: 'axis.labels' contains the wrong number of axis labels") if(min(plot.data[,-1])<centre.y) return("Error: plot.data' contains value(s) < centre.y") if(max(plot.data[,-1])>grid.max) return("Error: 'plot.data' contains value(s) > grid.max") #Declare required internal functions CalculateGroupPath <- function(df) { #Converts variable values into a set of radial x-y coordinates #Code adapted from a solution posted by Tony M to #http://stackoverflow.com/questions/9614433/creating-radar-chart-a-k-a-star-plot-spider-plot-using-ggplot2-in-r #Args: # df: Col 1 - group ('unique' cluster / group ID of entity) # Col 2-n: v1.value to vn.value - values (e.g. group/cluser mean or median) of variables v1 to v.n path <- as.factor(as.character(df[,1])) ##find increment angles = seq(from=0, to=2*pi, by=(2*pi)/(ncol(df)-1)) ##create graph data frame graphData= data.frame(seg="", x=0,y=0) graphData=graphData[-1,] for(i in levels(path)){ pathData = subset(df, df[,1]==i) for(j in c(2:ncol(df))){ #pathData[,j]= pathData[,j] graphData=rbind(graphData, data.frame(group=i, x=pathData[,j]*sin(angles[j-1]), y=pathData[,j]*cos(angles[j-1]))) } ##complete the path by repeating first pair of coords in the path graphData=rbind(graphData, data.frame(group=i, x=pathData[,2]*sin(angles[1]), y=pathData[,2]*cos(angles[1]))) } #Make sure that name of first column matches that of input data (in case !="group") colnames(graphData)[1] <- colnames(df)[1] graphData #data frame returned by function } CaclulateAxisPath = function(var.names,min,max) { #Caculates x-y coordinates for a set of radial axes (one per variable being plotted in radar plot) #Args: #var.names - list of variables to be plotted on radar plot #min - MININUM value required for the plotted axes (same value will be applied to all axes) #max - MAXIMUM value required for the plotted axes (same value will be applied to all axes) #var.names <- c("v1","v2","v3","v4","v5") n.vars <- length(var.names) # number of vars (axes) required #Cacluate required number of angles (in radians) angles <- seq(from=0, to=2*pi, by=(2*pi)/n.vars) #calculate vectors of min and max x+y coords min.x <- min*sin(angles) min.y <- min*cos(angles) max.x <- max*sin(angles) max.y <- max*cos(angles) #Combine into a set of uniquely numbered paths (one per variable) axisData <- NULL for (i in 1:n.vars) { a <- c(i,min.x[i],min.y[i]) b <- c(i,max.x[i],max.y[i]) axisData <- rbind(axisData,a,b) } #Add column names + set row names = row no. to allow conversion into a data frame colnames(axisData) <- c("axis.no","x","y") rownames(axisData) <- seq(1:nrow(axisData)) #Return calculated axis paths as.data.frame(axisData) } funcCircleCoords <- function(center = c(0,0), r = 1, npoints = 100){ #Adapted from Joran's response to http://stackoverflow.com/questions/6862742/draw-a-circle-with-ggplot2 tt <- seq(0,2*pi,length.out = npoints) xx <- center[1] + r * cos(tt) yy <- center[2] + r * sin(tt) return(data.frame(x = xx, y = yy)) } ### Convert supplied data into plottable format # (a) add abs(centre.y) to supplied plot data #[creates plot centroid of 0,0 for internal use, regardless of min. value of y # in user-supplied data] plot.data.offset <- plot.data plot.data.offset[,2:ncol(plot.data)]<- plot.data[,2:ncol(plot.data)]+abs(centre.y) #print(plot.data.offset) # (b) convert into radial coords group <-NULL group$path <- CalculateGroupPath(plot.data.offset) #print(group$path) # (c) Calculate coordinates required to plot radial variable axes axis <- NULL axis$path <- CaclulateAxisPath(var.names,grid.min+abs(centre.y),grid.max+abs(centre.y)) #print(axis$path) # (d) Create file containing axis labels + associated plotting coordinates #Labels axis$label <- data.frame( text=axis.labels, x=NA, y=NA ) #print(axis$label) #axis label coordinates n.vars <- length(var.names) angles = seq(from=0, to=2*pi, by=(2*pi)/n.vars) axis$label$x <- sapply(1:n.vars, function(i, x) {((grid.max+abs(centre.y))*axis.label.offset)*sin(angles[i])}) axis$label$y <- sapply(1:n.vars, function(i, x) {((grid.max+abs(centre.y))*axis.label.offset)*cos(angles[i])}) #print(axis$label) # (e) Create Circular grid-lines + labels #caclulate the cooridinates required to plot circular grid-lines for three user-specified #y-axis values: min, mid and max [grid.min; grid.mid; grid.max] gridline <- NULL gridline$min$path <- funcCircleCoords(c(0,0),grid.min+abs(centre.y),npoints = 360) gridline$mid$path <- funcCircleCoords(c(0,0),grid.mid+abs(centre.y),npoints = 360) gridline$max$path <- funcCircleCoords(c(0,0),grid.max+abs(centre.y),npoints = 360) #print(head(gridline$max$path)) #gridline labels gridline$min$label <- data.frame(x=gridline.label.offset,y=grid.min+abs(centre.y), text=as.character(grid.min)) gridline$max$label <- data.frame(x=gridline.label.offset,y=grid.max+abs(centre.y), text=as.character(grid.max)) gridline$mid$label <- data.frame(x=gridline.label.offset,y=grid.mid+abs(centre.y), text=as.character(grid.mid)) #print(gridline$min$label) #print(gridline$max$label) #print(gridline$mid$label) ### Start building up the radar plot # Delcare 'theme_clear', with or without a plot legend as required by user #[default = no legend if only 1 group [path] being plotted] theme_clear <- theme_bw() + theme(axis.text.y=element_blank(), axis.text.x=element_blank(), axis.ticks=element_blank(), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.border=element_blank(), legend.key=element_rect(linetype="blank")) if (plot.legend==FALSE) theme_clear <- theme_clear + theme(legend.position="none") #Base-layer = axis labels + plot extent # [need to declare plot extent as well, since the axis labels don't always # fit within the plot area automatically calculated by ggplot, even if all # included in first plot; and in any case the strategy followed here is to first # plot right-justified labels for axis labels to left of Y axis for x< (-x.centre.range)], # then centred labels for axis labels almost immediately above/below x= 0 # [abs(x) < x.centre.range]; then left-justified axis labels to right of Y axis [x>0]. # This building up the plot in layers doesn't allow ggplot to correctly # identify plot extent when plotting first (base) layer] #base layer = axis labels for axes to left of central y-axis [x< -(x.centre.range)] base <- ggplot(axis$label) + xlab(NULL) + ylab(NULL) + coord_equal() + geom_text(data=subset(axis$label,axis$label$x < (-x.centre.range)), aes(x=x,y=y,label=text),size=axis.label.size,hjust=1) + scale_x_continuous(limits=c(-plot.extent.x,plot.extent.x)) + scale_y_continuous(limits=c(-plot.extent.y,plot.extent.y)) # + axis labels for any vertical axes [abs(x)<=x.centre.range] base <- base + geom_text(data=subset(axis$label,abs(axis$label$x)<=x.centre.range), aes(x=x,y=y,label=text),size=axis.label.size,hjust=0.5) # + axis labels for any vertical axes [x>x.centre.range] base <- base + geom_text(data=subset(axis$label,axis$label$x>x.centre.range), aes(x=x,y=y,label=text),size=axis.label.size,hjust=0) # + theme_clear [to remove grey plot background, grid lines, axis tick marks and axis text] base <- base + theme_clear # + background circle against which to plot radar data base <- base + geom_polygon(data=gridline$max$path,aes(x,y), fill=background.circle.colour, alpha=background.circle.transparency) # + radial axes base <- base + geom_path(data=axis$path,aes(x=x,y=y,group=axis.no), colour=axis.line.colour) # ... + group (cluster) 'paths' base <- base + geom_path(data=group$path,aes(x=x,y=y,group=group,colour=group), size=group.line.width) # ... + group points (cluster data) base <- base + geom_point(data=group$path,aes(x=x,y=y,group=group,colour=group),size=group.point.size) #... + amend Legend title if (plot.legend==TRUE) base <- base + labs(colour=legend.title,size=legend.text.size) # ... + circular grid-lines at 'min', 'mid' and 'max' y-axis values base <- base + geom_path(data=gridline$min$path,aes(x=x,y=y), lty=gridline.min.linetype,colour=gridline.min.colour,size=grid.line.width) base <- base + geom_path(data=gridline$mid$path,aes(x=x,y=y), lty=gridline.mid.linetype,colour=gridline.mid.colour,size=grid.line.width) base <- base + geom_path(data=gridline$max$path,aes(x=x,y=y), lty=gridline.max.linetype,colour=gridline.max.colour,size=grid.line.width) # ... + grid-line labels (max; ave; min) [only add min. gridline label if required] if (label.gridline.min==TRUE) { base <- base + geom_text(aes(x=x,y=y,label=text),data=gridline$min$label,face="bold",size=grid.label.size, hjust=1) } base <- base + geom_text(aes(x=x,y=y,label=text),data=gridline$mid$label,face="bold",size=grid.label.size, hjust=1) base <- base + geom_text(aes(x=x,y=y,label=text),data=gridline$max$label,face="bold",size=grid.label.size, hjust=1) # ... + centre.y label if required [i.e. value of y at centre of plot circle] if (label.centre.y==TRUE) { centre.y.label <- data.frame(x=0, y=0, text=as.character(centre.y)) base <- base + geom_text(aes(x=x,y=y,label=text),data=centre.y.label,face="bold",size=grid.label.size, hjust=0.5) } return(base)}# (1) Define the data building blocks required for plotting purposes [uses# a subset of the OAC results plotted above]var.names <- c("All Flats", "No central heating", "Rooms per\nhousehold", "People per room", "HE Qualification", "Routine/Semi-Routine\nOccupation", "2+ Car household", "Public Transport\nto work", "Work from home")var.order = seq(1:9)values.a <- c(-0.1145725, -0.1824095, -0.01153078, -0.0202474, 0.05138737, -0.1557234, 0.1099018, -0.05310315, 0.0182626)values.b <- c(0.2808439, -0.2936949, -0.1925846, 0.08910815, -0.03468011, 0.07385727, -0.07228813, 0.1501105, -0.06800127)values.c <- rep(0, 9)group.names <- c("Blue Collar Communities", "Prospering Suburbs", "National Average")# (2) Create df1: a plotting data frame in the format required for ggplot2df1.a <- data.frame(matrix(c(rep(group.names[1], 9), var.names), nrow = 9, ncol = 2), var.order = var.order, value = values.a)df1.b <- data.frame(matrix(c(rep(group.names[2], 9), var.names), nrow = 9, ncol = 2), var.order = var.order, value = values.b)df1.c <- data.frame(matrix(c(rep(group.names[3], 9), var.names), nrow = 9, ncol = 2), var.order = var.order, value = values.c)df1 <- rbind(df1.a, df1.b, df1.c)colnames(df1) <- c("group", "variable.name", "variable.order", "variable.value")df1#(4) Create df2: a plotting data frame in the format required for# funcRadialPlotm2 <- matrix(abs(c(values.a, values.b)), nrow = 2, ncol = 9, byrow = TRUE)group.names <- c(group.names[1:2])df22 <- data.frame(group = group.names, m2)colnames(df22)[2:10] <- var.namesprint(df22)# (6) Create a radial plot using the function CreateRadialPlot, with min# y-value in center of plotCreateRadialPlot(df22, plot.extent.x = 1.5, grid.min = -0.4, centre.y = -0.5, label.centre.y = TRUE, label.gridline.min = FALSE)output:I would like to pass a dataframe containing values in the columns from 0 to 1 to the function and produce a percentage scale in the chart. And also to have a grid showing the percentage scale on it if possible (0,10....90,100).Here is the absolute values of the same data as in the example as an example:m2 <- matrix(abs(c(values.a, values.b)), nrow = 2, ncol = 9, byrow = TRUE)group.names <- c(group.names[1:2])df22 <- data.frame(group = group.names, m2)colnames(df22)[2:10] <- var.namesprint(df22) 解决方案 You could also use the rCharts package to make this kind of plot. There are a lot of options and you can probably customize it more easily.It it is the first time you are using rCharts, you should do the following setup:install.packages('devtools')require(devtools)install_github('rCharts', 'ramnathv')Here is an example code:library(rCharts)#create dummy dataframe with number ranging from 0 to 1df<-data.frame(id=c("a","b","c","d","e"),val1=runif(5,0,1),val2=runif(5,0,1))#muliply number by 100 to get percentagedf[,-1]<-df[,-1]*100plot <- Highcharts$new()plot$chart(polar = TRUE, type = "line",height=500)plot$xAxis(categories=df$id, tickmarkPlacement= 'on', lineWidth= 0)plot$yAxis(gridLineInterpolation= 'circle', lineWidth= 0, min= 0,max=100,endOnTick=T,tickInterval=10)plot$series(data = df[,"val1"],name = "Series 1", pointPlacement="on")plot$series(data = df[,"val2"],name = "Series 2", pointPlacement="on")plotThe output would look like this: 这篇关于修改r中雷达图的现有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-22 19:05