R 以颜色梯度线为轴
如何在ggplot中获得一条带有颜色渐变(最好是带有多个停止点)的线作为(y)轴? 我想把它作为彩色编码的评估提示添加到小提琴的情节中“好”将编码为绿色,而“坏”应编码为红色 我想到的是在侧面添加一个R 以颜色梯度线为轴,r,ggplot2,R,Ggplot2,如何在ggplot中获得一条带有颜色渐变(最好是带有多个停止点)的线作为(y)轴? 我想把它作为彩色编码的评估提示添加到小提琴的情节中“好”将编码为绿色,而“坏”应编码为红色 我想到的是在侧面添加一个geom_段,但这还不起作用: 没有渐变颜色,只有一种颜色,从给定范围内随机选择 (本例中有些为浅绿色) 将轴手动设置为直线感觉不正确-应该有一些自动设置 R代码: # create some test data type_list <- c("first", "second", "th
geom_段
,但这还不起作用:
- 没有渐变颜色,只有一种颜色,从给定范围内随机选择
(本例中有些为浅绿色) - 将轴手动设置为直线感觉不正确-应该有一些自动设置
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\u段
在此不起作用
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)))
非常有效,谢谢。不过,你知道一个更优雅的解决方案吗?你觉得这个建议的解决方案有什么不优雅的地方?对我来说,对geom\u line
和scale\u color\u gradientn
的一次调用似乎非常少。@drammock:事实上,我必须调整值范围并中断。否则就很容易了。