在R ggplot2中制作环
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在R ggplot2中制作环相关的知识,希望对你有一定的参考价值。
我正在尝试为我创建的目标的各个部分上色。我喜欢颜色选择。我正在尝试使单个环的透明度小于周围的环。例如,在下面的图片中,第二个圆圈位于...我只希望突出显示(或更暗)来自外部的第二个圆圈。这完全有意义吗?到目前为止,我的代码如下:
circle_data <- data.frame(
x0 = c(rep(0.5,4),rep(3,4),rep(5.5,4),rep(8,4)),
y0 = c(rep(3,4),rep(3,4),rep(3,4),rep(3,4)),
r = c(rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4))),
col = c(rev(c(seq(1,4))),rev(c(seq(1,4))),rev(c(seq(1,4))),rev(c(seq(1,4))))
)
circle_data_add2 <- data.frame(
x0 = rep(3,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[2],
col = rev(c(seq(1,4)))[2]
)
circle_data_add1 <- data.frame(
x0 = rep(0.5,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[1],
col = rev(c(seq(1,4)))[1]
)
circle_data_add3 <- data.frame(
x0 = rep(5.5,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[3],
col = rev(c(seq(1,4)))[3]
)
circle_data_add4 <- data.frame(
x0 = rep(8,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[4],
col = rev(c(seq(1,4)))[4]
)
ggplot()+
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.15,data = circle_data)+
coord_fixed()+
scale_fill_gradient(low = "red", high = "blue")+
scale_color_gradient(low = "red", high = "blue")+
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add1,inherit.aes = FALSE)+
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add2,inherit.aes = FALSE)+
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add3,inherit.aes = FALSE)+
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add4,inherit.aes = FALSE)+
theme(
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
legend.position = "none"
)+
xlab("")+
ylab("")
这是当前代码的输出:
并且我想要的输出,其中仅突出显示某个环,如下:
答案
要制作戒指,您需要在较大的圆圈上放一个较小的圆圈。因此,您可以通过在绘图中添加更多的圆并仔细考虑添加圆的顺序来一起破解。在某种程度上,这类似于Tower of Hanoi游戏。
circle_data <- data.frame(
x0 = c(rep(0.5,4),rep(3,4),rep(5.5,4),rep(8,4)),
y0 = c(rep(3,4),rep(3,4),rep(3,4),rep(3,4)),
r = c(rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4)),rev(seq(0.1, 1, length.out = 4))),
col = c(rev(c(seq(1,4))),rev(c(seq(1,4))),rev(c(seq(1,4))),rev(c(seq(1,4))))
)
circle_data_add2 <- data.frame(
x0 = rep(3,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[2],
col = rev(c(seq(1,4)))[2]
)
circle_data_add2_negative <- data.frame(
x0 = rep(3,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[3]
)
circle_data_add1 <- data.frame(
x0 = rep(0.5,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[1],
col = rev(c(seq(1,4)))[1]
)
circle_data_add1_negative <- data.frame(
x0 = rep(0.5,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[2]
)
circle_data_add3 <- data.frame(
x0 = rep(5.5,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[3],
col = rev(c(seq(1,4)))[3]
)
circle_data_add3_negative <- data.frame(
x0 = rep(5.5,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[4]
)
circle_data_add4 <- data.frame(
x0 = rep(8,1),
y0 = rep(3,1),
r = rev(seq(0.1, 1, length.out = 4))[4],
col = rev(c(seq(1,4)))[4]
)
ggplot()+
coord_fixed()+
scale_fill_gradient(low = "red", high = "blue")+
scale_color_gradient(low = "red", high = "blue")+
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add1,inherit.aes = FALSE)+
geom_circle(aes(x0 = x0,y0 = y0,r = r), fill = "white",alpha = 1,data = circle_data_add1_negative,inherit.aes = FALSE)+
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add2,inherit.aes = FALSE)+
geom_circle(aes(x0 = x0,y0 = y0,r = r), fill = "white",alpha = 1,data = circle_data_add2_negative,inherit.aes = FALSE)+
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add3,inherit.aes = FALSE)+
geom_circle(aes(x0 = x0,y0 = y0,r = r), fill = "white",alpha = 1,data = circle_data_add3_negative,inherit.aes = FALSE)+
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.6,data = circle_data_add4,inherit.aes = FALSE)+
# and this one is moved to the end
geom_circle(aes(x0 = x0,y0 = y0,r = r,fill = col,col = col),alpha = 0.15,data = circle_data)+
theme(
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
legend.position = "none"
)+
xlab("")+
ylab("")
您可以进行半径计算,使其看起来完全像您的示例,但想法是相同的。
以上是关于在R ggplot2中制作环的主要内容,如果未能解决你的问题,请参考以下文章