使用g使用ggplots或其他替代包修改极坐标图

时间:2012-05-06 04:01:57

标签: r ggplot2 polar-coordinates

我正在尝试使用以下数据创建漂亮的(!)极坐标图。

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 

两个pos之间的间隔之间的角度。

我的真实数据中有两个以上的图层要绘制。

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

如果可能,不同的组可以进行颜色编码。

enter image description here

替代圆弧段标记(首选)

enter image description here

我用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()

更多试验:

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

编辑1:

我想我解释这个问题可能很糟糕。这是图解释。 enter image description here

pos(位置)是一个连续的刻度,极坐标的角度应该取决于位置的值。一组完成后它将重新启动。我为这个技巧做了一个伎俩(我可能错了)。

编辑2:

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

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)

enter image description here

当我尝试设置ylim(2,3)时,它们看起来像是不能正常工作的段。

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

1 个答案:

答案 0 :(得分:5)

我很难理解你想做什么(条形图计数频率,你可能知道这一点)。但是,这是一种将组分配为颜色的方法:

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()

enter image description here

如果您希望将pos作为频率,请使用melt()中的reshape2功能。

如果你想在你的例子中使用点,可以采用以下方法吗?

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

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

无论如何,你看到了模式?使用x轴作为角度绘制具有法线坐标的图,使用y轴作为距中间的距离,并将其转换为极坐标。

enter image description here

回答Edit2

我仍然想知道你是否可以制作一个更有意义的情节,但也许你有理由这样做。这是一个更接近你的例子的情节。它并不完美。也许大师们明天到达办公室后会给你一个更好的建议。您可以查找更多规范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"))

enter image description here