繁体   English   中英

颜色渐变线为轴

[英]Colour gradient line as axis

如何在ggplot中获得一条带有颜色渐变(最好有多个光圈)的线作为(y)轴?

我想将此作为颜色编码的评估提示添加到小提琴图。 “好”将被编码为绿色,而“坏”应被编码为红色。

我想出的是在侧面添加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)))

在此处输入图片说明

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)))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM