使用r格式化带有ggplot2中的点的圆形区域图

时间:2012-05-06 16:04:19

标签: r ggplot2 polar-coordinates

这是我的情节:

gr1 <- c(1, 5, 15, 20, 30, 40)
gr3 <- c(1, 5, 10, 25, 40, 60, 80)
gr2 <- c(1, 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)
df2$y <- rep(3, length (df2$cpos))
df2$y1 <- rep(0.35, length (df2$cpos))

df3 <- data.frame (cpos = rep(df2$cpos, 2), 
group = rep(df2$group,2), yd = c(df2$y, df2$y1),
x2 = c(rep(1, length (df2$y)),
rep(2, length (df2$y1))))

cx <- ggplot(df3, aes(y = yd, x = cpos, fill=factor(x2)))
cx1 <- cx + geom_area(aes(stat = "identity", position="fill" ))
cx1 +  geom_point(aes(color = factor(group)), pch = 19, size = 4) + 
scale_fill_manual(values=c("blue", "yellow", "green")) + coord_polar()

enter image description here

我无法摆脱一些问题

(1)我无法改变点的颜色,颜色变化仅适用于圆圈。

(2)绘制在中心圆上绘制的附加点,我不想绘制。 (他们是额外的积分)

声明:此帖子与previous post

相关

奖励意外艺术

在努力解决问题时,我做了一个向日葵的情节。

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)
df2$y <- rep(3, length (df2$cpos))
df2$y1 <- rep(0.01, length (df2$cpos))
df2$y3 <- rep(2, length (df2$cpos))

df3 <- data.frame (cpos = rep(df2$cpos, 3), group = c(rep(1, length (df2$y)),
 rep(2, length (df2$y)), rep(3, length (df2$y))),
 yd = c(df2$y, df2$y1, df2$y3), x3 = c(rep(1, length (df2$y)), 
 rep(2, length (df2$y1)),rep(2, length (df2$y1)) ))

制作花

cx <- ggplot(df3, aes(y = yd, x = cpos, fill=factor(x3)))
cx1 <- cx + geom_area(aes(stat = "identity", position="fill" ))
cx2 <- cx1 +  geom_point(aes(color = factor(group)), pch = 19, size = 4) + 
  scale_fill_manual(values=c("yellow", "pink")) + coord_polar()
cx2 + theme_bw()+ opts(axis.line=theme_blank(),axis.text.y=theme_blank(),
 axis.ticks = theme_blank(), axis.title.y=theme_blank(), 
 plot.background=theme_blank(), panel.border=theme_blank())

enter image description here

1 个答案:

答案 0 :(得分:1)

对于你的第二个问题,这是你想要的吗?点的内圆对应于df3数据框中的yd = 0.35x2 = 2。因此,在geom_point调用中,从df3数据帧中删除这些点。在这里,我使用subset

# Your data
gr1 <- c(1, 5, 15, 20, 30, 40)
gr3 <- c(1, 5, 10, 25, 40, 60, 80)
gr2 <- c(1, 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)
df2$y <- rep(3, length (df2$cpos))
df2$y1 <- rep(0.35, length (df2$cpos))

df3 <- data.frame (cpos = rep(df2$cpos, 2), 
  group = rep(df2$group,2), yd = c(df2$y, df2$y1),
  x2 = c(rep(1, length (df2$y)),
  rep(2, length (df2$y1))))

# The plot
library(ggplot2)
cx <- ggplot(df3, aes(y = yd, x = cpos, fill=factor(x2)))
cx1 <- cx + geom_area(aes(stat = "identity", position="fill" ))
cx1 +  geom_point(data = subset(df3, x2 == 1), aes(color = factor(group)), 
  pch = 19, size = 4) + 
  scale_color_manual(values=c("blue", "yellow", "green"))  + coord_polar()

结果是:

enter image description here