如何在ggplot中获得带有颜色渐变的线(最好有多个停止点)作为(y)-axis?
我想将其添加为小提琴情节的颜色编码评价提示.'好'将编码为绿色,而'坏'应为红色.
我想出的是添加一个geom_segment侧面,但这还不起作用:
R代码:
# create some test data
type_list <- c("first", "second", "third")
test_data <- data.frame()
N <- 16
for( i in 1:length(type_list))
{
test_data = rbind( test_data,
data.frame(
idx = 1:N,
vals = runif( n = N, min = 0, max = 1),
type = type_list[i]
)
)
}
# plot data as violin
ggplot( test_data, aes( y = vals, fill = type)) +
geom_violin( aes( x = type )) +
geom_segment( aes(x = 0, xend = 0, y = 0, yend = 1, color = vals, size = 2))+
scale_colour_gradientn( colours = c( "darkred", "yellow", "darkgreen"),
breaks = c( 0, 0.5, 1),
labels = c( "bad", "medium", "good"),
limits = c( 0,1)) +
guides( size = FALSE, colour = guide_legend( title="Evaluation", reverse = TRUE,
override.aes = list( size = 5)))
Run Code Online (Sandbox Code Playgroud)

小智 5
geom_segment 不要在这里工作
ggplot( test_data, aes( y = vals)) +
geom_violin( aes( x = type, fill = type )) +
geom_line(data = data.frame(x = c(rep(0,100)), y = seq(0,1,length.out=100)), aes(x=x, y=y, color=y),size=2)+
scale_colour_gradientn( colours = c( "darkred", "yellow", "darkgreen"),
breaks = c( 0, 0.5, 1),
labels = c( "bad", "medium", "good"),
limits = c( 0,1)) +
guides( size = FALSE, colour = guide_legend( title="Evaluation", reverse = TRUE,
override.aes = list( size = 5)))
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
640 次 |
| 最近记录: |