使用ggplot库中的geom_path [英] Using geom_path from ggplot library

查看:1394
本文介绍了使用ggplot库中的geom_path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有12个变量,,M2,...,M12 ,为此我计算了一个统计量 x

  df = data.frame(model = factor(paste(M,1:28,sep =),levels = paste(M,1:28,sep =)),x = runif(28,1,1.05))

levels = seq(0.8,1.2 ,0.05)

我想绘制如下数据:


每个圆圈(轮廓)表示该统计量 x 的等级。三条蓝线代表三种不同的情况。

本例中包含的数据框表示一种情况。蓝线将简单地将所有模型的值加入 M1 M28

我试过以下内容:

  ggplot(data = df,aes( x = model,y = x,group = 1))+ 
geom_line()+ coord_polar()+
scale_y_continuous(限制=范围(水平),休息=水平,标签=水平)+
theme(axis.text.y = element_blank(),axis.ticks = element_blank(),axis.title.x = element_blank(),axis.title.y = element_blank())

但是,我得到了一个断开的路径(在M28和M1之间)

< img src =https://i.stack.imgur.com/rWH4j.jpgalt =然后,我复制了第一张照片将它放在数据框的底部(见下面),然后使用 geom_path()而不是 geom_line(),但我没有得到我期待的结果:

  ##复制fi使用geom_path()

将第一行(model1)放置在数据框的末尾
df = rbind(df,df [1,])

## ggplot(data = df,aes(x = model,y = lg,group = 1))+
geom_path()+ coord_polar()+
scale_y_continuous(限制= range(levels),breaks = levels ,labels = levels)+
theme(axis.text.y = element_blank(),axis.ticks = element_blank(),axis.title.x = element_blank(),axis.title.y = element_blank())


请问我可以帮助我达到我期待的结果吗?任何帮助,将不胜感激。谢谢!

解决方案

对于封闭路径,您必须使用 geom_polygon

  library(ggplot2)
ggplot(data = df,aes(x = model,y = x,group = 1 )+
geom_polygon(fill = NA,color =black)+
coord_polar()+
scale_y_continuous(限制=范围(水平),休息=水平,标签=水平)+
theme(axis.text.y = element_blank(),axis.ticks = element_blank(),
axis.title.x = element_blank(),axis.title.y = element_blank())


I have 12 variables, M1, M2, ..., M12, for which I compute a certain statistic x.

 df = data.frame(model = factor(paste("M", 1:28, sep = ""), levels=paste("M", 1:28, sep = "")), x = runif(28, 1, 1.05))

 levels = seq(0.8, 1.2, 0.05)

I would like to plot this data as follows:

Each circle (contour) represents the a level of that statistic "x". The three blue lines simply represent three different scenarios.

The dataframe included in this example represents one scenario. The blue line would simply join the values of all the models M1 to M28 for that specific scenario.

I tried the following:

 ggplot(data=df, aes(x=model, y=x, group=1)) + 
   geom_line() + coord_polar() + 
   scale_y_continuous(limits=range(levels), breaks=levels, labels=levels) +
   theme(axis.text.y = element_blank(), axis.ticks = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank())

However, I get a disconnected path (between M28 and M1)

Then, I replicated the first row and placed it at the bottom of the dataframe (see below), and then used geom_path() instead of geom_line(), but I didn't get the result I was looking for:

 ## Replicating the first row (model1) and placing it at end of dataframe
 df = rbind(df, df[1,])

 ## using geom_path()

 ggplot(data=df, aes(x=model, y=lg, group=1)) + 
   geom_path() + coord_polar() + 
   scale_y_continuous(limits=range(levels), breaks=levels, labels=levels) +
   theme(axis.text.y = element_blank(), axis.ticks = element_blank(), axis.title.x =  element_blank(), axis.title.y = element_blank())

Could any please help me achieve the result that I am looking for? Any help would be appreciated. Thanks!

解决方案

You have to use geom_polygon for closed paths:

library(ggplot2)
ggplot(data=df, aes(x=model, y=x, group=1)) + 
  geom_polygon(fill = NA, colour = "black") + 
  coord_polar() + 
  scale_y_continuous(limits=range(levels), breaks=levels, labels=levels) +
  theme(axis.text.y = element_blank(), axis.ticks = element_blank(), 
        axis.title.x = element_blank(), axis.title.y = element_blank())

这篇关于使用ggplot库中的geom_path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆