gpt4 book ai didi

r - 颜色渐变线为轴

转载 作者:行者123 更新时间:2023-12-03 23:08:07 26 4
gpt4 key购买 nike

如何在 ggplot 中获得一 strip 有颜色渐变(最好有多个停止点)的线作为 (y) 轴?

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

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

enter image description here

最佳答案

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

enter image description here

关于r - 颜色渐变线为轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25036230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com