如何在 R 中绘制手动色标?
How to make manual colorscale in plotly in R?
我正在 R
中使用 plotly
制作 3D 曲面图,如下所示;
#create dataframe
df <-data.frame(x = c(rnorm(300, 3, 2.5), rnorm(150, 7, 2)), # create random data
y = c(rnorm(300, 6, 2.5), rnorm(150, 2, 2)),
z = c(rnorm(300, 6, 2.5), rnorm(150, 2, 2)))
#prepare surface and plot
s = interp(x = df$x, y = df$y, z = df$z,duplicate = "mean") # prepare for plot_ly plot
p <- plot_ly(x = s$x, y = s$y, z = s$z,colorscale = 'RdYlBu')%>% # plot_ly
add_surface()
,结果如下图;
在这里,我想将 "color transition from blue to red" 设置为 z = 2.5。
有什么方法可以巧妙地做到这一点吗?
虽然我现在用的是colorscale = 'RdYlBu'
,但不一定要用。无论如何,理想的颜色模式是在 z = 2.5 处将 "color transition" 置于蓝色和红色之间,因为它可以在 ggolot2
中手动实现,如下所示;
gg <-ggplot(df, aes(x=x, y=y, color=z)) +
geom_point() +
scale_color_gradient2(midpoint=2.5, low="blue", mid="white",high="red", space ="Lab")
,结果如下图;
您有什么解决方案建议吗?
library(plotly)
library(akima)
df <-data.frame(x = c(rnorm(300, 3, 2.5), rnorm(150, 7, 2)), # create random data
y = c(rnorm(300, 6, 2.5), rnorm(150, 2, 2)),
z = c(rnorm(300, 6, 2.5), rnorm(150, 2, 2)))
s <- interp(x = df$x, y = df$y, z = df$z, duplicate = "mean")
t <- (2.5 - min(df$z)) / (max(df$z) - min(df$z))
plot_ly(x = s$x, y = s$y, z = s$z) %>%
add_surface(colorscale = list(list(0,"blue"), list(t,"white"), list(1,"red")))
我正在 R
中使用 plotly
制作 3D 曲面图,如下所示;
#create dataframe
df <-data.frame(x = c(rnorm(300, 3, 2.5), rnorm(150, 7, 2)), # create random data
y = c(rnorm(300, 6, 2.5), rnorm(150, 2, 2)),
z = c(rnorm(300, 6, 2.5), rnorm(150, 2, 2)))
#prepare surface and plot
s = interp(x = df$x, y = df$y, z = df$z,duplicate = "mean") # prepare for plot_ly plot
p <- plot_ly(x = s$x, y = s$y, z = s$z,colorscale = 'RdYlBu')%>% # plot_ly
add_surface()
,结果如下图;
在这里,我想将 "color transition from blue to red" 设置为 z = 2.5。
有什么方法可以巧妙地做到这一点吗?
虽然我现在用的是colorscale = 'RdYlBu'
,但不一定要用。无论如何,理想的颜色模式是在 z = 2.5 处将 "color transition" 置于蓝色和红色之间,因为它可以在 ggolot2
中手动实现,如下所示;
gg <-ggplot(df, aes(x=x, y=y, color=z)) +
geom_point() +
scale_color_gradient2(midpoint=2.5, low="blue", mid="white",high="red", space ="Lab")
,结果如下图;
您有什么解决方案建议吗?
library(plotly)
library(akima)
df <-data.frame(x = c(rnorm(300, 3, 2.5), rnorm(150, 7, 2)), # create random data
y = c(rnorm(300, 6, 2.5), rnorm(150, 2, 2)),
z = c(rnorm(300, 6, 2.5), rnorm(150, 2, 2)))
s <- interp(x = df$x, y = df$y, z = df$z, duplicate = "mean")
t <- (2.5 - min(df$z)) / (max(df$z) - min(df$z))
plot_ly(x = s$x, y = s$y, z = s$z) %>%
add_surface(colorscale = list(list(0,"blue"), list(t,"white"), list(1,"red")))