使用ggplots或使用R的其他替代包修改极坐标图 [英] modified polar plot using ggplots or other alternative packages using R

查看:404
本文介绍了使用ggplots或使用R的其他替代包修改极坐标图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  gr1 <-c(0) (0,5,10,25,40,60,80)
gr2 -c(0,15,25,b) ,30,40)

df2 < - data.frame(pos = c(gr1,gr2,gr3),group = c(rep(1,length(gr1)),
rep (2,长度(gr1)),代表(2,长度(gr1))))



内部圆圈段标记
第一层,组1 ,15,20美元之间的B组3,5,10美元之间的B $ B,40-60美元之间的第二层,1组,15,20美元之间的B组,3美元之间,5-10美元之间b $ b在10到25
之间40到60

两条线之间的夹角两个pos之间的间隔。

我的真实数据中有两层以上的图形。



标记可以是分段或其他标记,例如填充颜色。

不同的组可以用颜色编码if可能。





可选圆圈标记(首选)



我用ggplot2试了一下。

  cx < -  ggplot(df2,aes(x = pos,group = group)+ geom_bar(width = 1,color =black))
aes(x = pos,group = group)+ geom_bar(width = 1,color =black)的错误:
二元运算符的非数字参数
cx + coord_polar()

更多试用:

  df2 $ cpos < -  cumsum(df2 $ pos)
cx < - ggplot(df2,aes(x = cpos,group = group)+ geom_bar(width = 1,color =black))

编辑1:

我想我可能很难解释这个问题。这里是解释的图。



pos(位置)是一个连续的尺度,极角应取决于位置的值。一旦一个组完成,它将重新启动。我做了一个诡计来哄骗这个诡计(我可能是错的)。

编辑2:



以下回答为这个问题提供了更多的想法。这是改进版本,但颜色管理不起作用。

  df2 $ cpos < -  cumsum(df2 $ pos)
df2 $ y < - rep(3,长度(df2 $ cpos))
cx < - ggplot(df2,aes(y = y,x = cpos))
cx + geom_bar(aes(stat =identity,fill =yellow ,color =yellow))+
geom_point(aes(color = factor(group)),pch = 18,size = 2)+ coord_polar()+
scale_fill_manual(values = c( #CC6666,#9999CC,#66CC99))+ ylim(0,3)



当我尝试设置ylim(2,3),使它们看起来像段不起作用。

 数据$ y [数据$ y == -Inf]中的错误< - 范围$ y.range [1]: 
replacement has length zero


解决方案

很难理解你想要做什么(条形图计算频率,你可能知道这一点)。然而,这是一种将组分配成颜色的方式:

  gr1 < -  c(0,5,15,20, (0,5,10,25,40,60,80)
gr2 -c(0,15,25,30,40)
gr3 <-c(0,5,10,25,40,60,80)
$ b df2 < - data.frame(pos = c(gr1,gr2,gr3),group = c(rep(1,length(gr1)),
rep(2,length(gr2) ),rep(3,length(gr3))))
df2 $ cpos < - cumsum(df2 $ pos)

cx < - ggplot(df2,aes(fill = factor (group),x = cpos))

cx + geom_bar(width = 1,color =black,position =dodge)+ coord_polar()

$ b

如果你想获得pos的频率,在中使用 melt() reshape2



如果您想使用点作为示例,可以采用以下方法吗?

  cx < -  ggplot(df2,aes(y = group,x = cpos))

cx + geom_point(aes(color = factor (group)))+ coord_pola r()+ ylim(0,3)

无论如何,你会看到模式?使用x轴作为角度,使用y轴作为中间距离,然后将其转换为极坐标,以法线坐标进行绘图。



回答Edit2



我仍然想知道你是否可以制作更有意义的情节,但也许你有理由这么做。这是一个更接近你的例子的情节。这并不完美。也许大师明天到达他们的办公室后可以给你一个更好的建议。同时,您可以从更多规格的链接中查找更多规格。

  library(ggplot2)

gr1 <-c(0,5,15,20,30,40 )
gr3 <-c(0,5,10,25,40,60,80)
gr2 <-c(0,15,25,30,40)

df2 < - data.frame(pos = c(gr1,gr2,gr3),group = c(rep(1,length(gr1)),
rep(2,length(gr2)),rep (3,length(gr1))),y = c(rep(1,length(gr1)),
rep(2,length(gr1)),rep(2,length(gr1))))

df2 $ cpos < - cumsum(df2 $ pos)

cx < - ggplot(df2,aes(y = y,x = cpos))
cx + geom_point(aes(color = factor(group)),size = 4)+ geom_line(aes(x = c(0,500),y = c(1)),color =yellow)+
geom_line (aes(x = c(0,500),y = c(2)),color =blue)+ scale_y_continuous(limits = c(0,2),breaks = c(0,1,2))+
scale_x_continuous(labels = df2 $ pos,breaks = df2 $ cpos,limits = c(0,500))+ coord_polar()+
opts(panel.grid。 major = theme_line(color =gray),
panel.grid.minor = theme_line(color =gray,linetype =dashed),
panel.background = theme_rect(color =white ))


I am trying to create nice (!) polar plot with the following data.

gr1 <- c(0, 5, 15, 20, 30, 40)
gr3 <- c(0, 5, 10, 25, 40, 60, 80)
gr2 <- c(0, 15, 25, 30, 40)

df2<- data.frame (pos = c(gr1, gr2, gr3), group = c(rep(1, length(gr1)),
 rep(2, length(gr1)), rep(2, length(gr1))))



inner circle segment to mark
                   first tier, group 1, between 15, 20
                               group 3, between 5, 10
                                        between  40 to 60 
                   second tier, group 1, between 15, 20
                               group 3, between 5, 10
                                        between 10, 25
                                        between  40 to 60 

The angle between two lines between the interval between two pos.

there are more than two tier in my real data to plot.

The markers might be either segments or other marks such as filled colors.

Different group can color coded if possible.

Alternative with circle segment markings (preferred)

I tried it with ggplot2.

cx <- ggplot(df2, aes(x = pos, group = group) +   geom_bar(width = 1, colour = "black"))
Error in aes(x = pos, group = group) + geom_bar(width = 1, colour = "black") :
  non-numeric argument to binary operator
cx + coord_polar()

More trials:

df2$cpos <- cumsum (df2$pos)
cx <- ggplot(df2, aes(x = cpos, group = group) +   geom_bar(width = 1, colour = "black"))

Edits 1:

I think I might be terrible to explain the issue. Here is figure with explanation.

The pos (position) is a continuous scale, angle in polar should depend upon where value of position is. It will restart once one group is done. I did a trick to cumsum to this trick (I might be wrong).

Edits 2:

The following answer opened more thoughts into the question. This is improved version but color management is not working.

df2$cpos <- cumsum (df2$pos)
df2$y <- rep(3, length (df2$cpos))
cx <- ggplot(df2, aes(y = y, x = cpos))
cx + geom_bar(aes(stat = "identity",fill="yellow", colour = "yellow" )) + 
geom_point(aes(color = factor(group)), pch = 18, size = 2) + coord_polar() +
 scale_fill_manual(values=c("#CC6666", "#9999CC", "#66CC99"))+ ylim(0,3)

When I try to set ylim (2,3) so that they look like segments that do not work as well.

Error in data$y[data$y == -Inf] <- range$y.range[1] : 
  replacement has length zero

解决方案

I am having a hard time understanding what you want to do (bar-plot counts frequencies, you probably know that). However here is a way to assign the groups into colors:

gr1 <- c(0, 5, 15, 20, 30, 40)
gr3 <- c(0, 5, 10, 25, 40, 60, 80)
gr2 <- c(0, 15, 25, 30, 40)

df2<- data.frame (pos = c(gr1, gr2, gr3), group = c(rep(1, length(gr1)),
 rep(2, length(gr2)), rep(3, length(gr3))))
df2$cpos <- cumsum (df2$pos)

cx <- ggplot(df2, aes(fill = factor(group), x = cpos))

cx + geom_bar(width = 1, colour = "black", position = "dodge")  + coord_polar()

If you want to get pos as frequencies, use the melt() function in reshape2.

If you want to use dots as in your example, could following approach work?

cx <- ggplot(df2, aes(y = group, x = cpos))

cx + geom_point(aes(color = factor(group))) + coord_polar() + ylim(0,3)

Anyway, you see the pattern? Make a plot with normal coordinates using x-axis as angle and y-axis as distance from the middle and just convert it to polar coordinates.

Answer to Edit2

I am still wondering whether you could make a plot that makes more sense, but maybe you have a reason to do this. Here is a plot that is closer to your example. It isn't perfect. Maybe the gurus can give you a better suggestion tomorrow once they arrive their offices. In a mean while you can look for more specifications from the links of this tread.

library(ggplot2)

gr1 <- c(0, 5, 15, 20, 30, 40)
gr3 <- c(0, 5, 10, 25, 40, 60, 80)
gr2 <- c(0, 15, 25, 30, 40)

df2<- data.frame (pos = c(gr1, gr2, gr3), group = c(rep(1, length(gr1)),
 rep(2, length(gr2)), rep(3, length(gr3))), y  = c(rep(1, length(gr1)),
 rep(2, length(gr1)), rep(2, length(gr1))))

df2$cpos <- cumsum (df2$pos)

cx <- ggplot(df2, aes(y = y, x = cpos))
cx + geom_point(aes(color = factor(group)), size = 4) + geom_line(aes(x = c(0,500), y = c(1)), color = "yellow") + 
geom_line(aes(x = c(0,500), y = c(2)), color = "blue") + scale_y_continuous(limits=c(0,2), breaks = c(0,1,2)) +
scale_x_continuous(labels = df2$pos, breaks = df2$cpos, limits = c(0,500)) + coord_polar() +
opts(panel.grid.major = theme_line(colour = "grey"), 
panel.grid.minor = theme_line(colour = "grey", linetype = "dashed"), 
panel.background = theme_rect(colour = "white"))

这篇关于使用ggplots或使用R的其他替代包修改极坐标图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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