用R中的渐变比例填充多边形 [英] Fill a polygon with gradient scale in R
问题描述
我想创建一个量表以可视化一个变量对另一个变量有或多或少的影响.
I would like to create a gauge to visualize whether a variable has more or less effect on another.
我建立了一个绘制仪表的功能,并用3种不同的颜色填充它.
I built a function to plot a gauge and I am filling it with 3 distinct colours.
gg.gauge <- function(pos,breaks=c(0,33,66,100),determinent) {
require(ggplot2)
get.poly <- function(a,b,r1=0.5,r2=1.0) {
th.start <- pi*(1-a/100)
th.end <- pi*(1-b/100)
th <- seq(th.start,th.end,length=100)
x <- c(r1*cos(th),rev(r2*cos(th)))
y <- c(r1*sin(th),rev(r2*sin(th)))
return(data.frame(x,y))
}
ggplot()+
geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y), fill = "red")+
geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y), fill = "gold")+
geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y), fill = "green")+
geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y))+
geom_text(data=as.data.frame(breaks), size=5, fontface="bold", vjust=0,
aes(x=0.8*cos(pi*(1- breaks/100)),y=-0.1),label=c('Less','','',"More"))+
annotate("text",x=0,y=0,label=determinent,vjust=0,size=8,fontface="bold")+
coord_fixed()+
theme_bw()+
theme(axis.text=element_blank(),
axis.title=element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank(),
panel.border=element_blank(),
legend.position = "none")
}
输出:
我想用生动的颜色填充,以使它从红色(低)到绿色(高)具有淡入淡出的效果,而不会产生明显的切痕.
I would like to fill is with gratient colour so that it gives a fading effect from red (low) to green (high) without very distinct cuts.
我尝试使用 scale_fill_grdientn
却没有得到肯定的结果.
I attempted to use scale_fill_grdientn
without positive result.
谢谢
推荐答案
最简单的方法是使用单个线段而不是多边形.以下面的修改示例为例,其中我仅更改了 get_poly
的定义,并使用了 geom_segment
而不是 geom_polygon
:
The easiest way to achieve this is to use individual segments instead of a polygon. Take the following modified example where I only changed the definition of get_poly
and used geom_segment
instead of geom_polygon
:
gg.gauge <- function(pos, breaks = c(0, 33, 66, 100), determinent) {
require(ggplot2)
get.poly <- function(a, b, r1 = 0.5, r2 = 1.0) {
th.start <- pi * (1 - a / 100)
th.end <- pi * (1 - b / 100)
th <- seq(th.start, th.end, length = 1000)
x <- r1 * cos(th)
xend <- r2 * cos(th)
y <- r1 * sin(th)
yend <- r2 * sin(th)
data.frame(x, y, xend, yend)
}
ggplot() +
geom_segment(data = get.poly(breaks[1],breaks[4]),
aes(x = x, y = y, xend = xend, yend = yend, color = xend)) +
scale_color_gradientn(colors = c("red", "gold", "green")) +
geom_segment(data = get.poly(pos - 1, pos + 1, 0.2), aes(x = x, y =y, xend = xend, yend = yend)) +
geom_text(data=as.data.frame(breaks), size = 5, fontface = "bold", vjust = 0,
aes(x = 0.8 * cos(pi * (1 - breaks / 100)), y = -0.1), label = c('Less', '', '', "More")) +
annotate("text", x = 0, y = 0,label=determinent,vjust=0,size=8,fontface="bold")+
coord_fixed()+
theme_bw()+
theme(axis.text=element_blank(),
axis.title=element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank(),
panel.border=element_blank(),
legend.position = "none")
}
gg.gauge(pos = 10, determinent = "Test")
这篇关于用R中的渐变比例填充多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!