formatting circular area plot with point in ggplot2 using r
-
06-06-2021 - |
質問
Here is my plot:
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()
I could not get rid of some of issues
(1) I could not change color of the points, color change only applied to circles.
(2) the plot additional points plotted at the center circle, which I do not want to plot. (they are additional csum points)
declaration: this post is related with the previous post
Bonus accidental art
As am working hard to solve the issues, I do an sunflower plot.
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)) ))
makes flower
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())
解決
For your second question, is this what you want? The inner circle of points correspond to yd = 0.35
or x2 = 2
in the df3 data frame. Therefore, in the geom_point
call, drop those points from the df3 data frame. Here, I've use 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()
The result is:
所属していません StackOverflow