gpt4 book ai didi

r - 使用 scale_color_viridis 时设置颜色图中的最大值

转载 作者:行者123 更新时间:2023-12-04 09:42:52 24 4
gpt4 key购买 nike

这是代码:

pic = ggplot(df_2, 
aes(x = df_2$X, xend = df_2$X + df_2$dx, y = df_2$Y, yend = df_2$Y + df_2$dy, color = df_2$speedkt)) +
labs(title ="Surface Currents", x = "Longitude", y = "Latitude", colour="Speed (kts)") +
geom_segment(alpha = 0.7, arrow = arrow(length = unit(0.1,"cm"))) + coord_fixed() +
theme(panel.background = element_rect(fill = "transparent",colour = NA), plot.background = element_rect(fill = "transparent",colour = NA)) +
viridis::scale_color_viridis(option = "B", direction = -1)

您可以看到颜色渐变遵循 df_2$speedkt 的值. df_2$speedkt的最大值大约是 2.6。

因此,图例中颜色图的最大值约为 2.8。

但我希望将颜色图的最大值更改为 4.0。

我可以做什么?

Colormap in Legend Highlighted

最佳答案

要回答您的主要问题,您应该可以通过 limits = c(0, 4)作为 viridis::scale_color_viridis() 的参数, IE。

viridis::scale_color_viridis(option = "B", direction = -1, limits = c(0, 4))

其他几点:
  • ggplot2 的较新版本内置了 viridis 秤,因此您应该可以使用:

  • scale_color_viridis_c(option = "B", direction = -1, limits = c(0, 4))
  • 一般来说,最好不要使用df$colaes() ,只需使用列名,例如

  • ggplot(df_2, aes(x = X, xend = X + dx, y = Y))

    ggplot 将在数据框中查找列名。

    关于r - 使用 scale_color_viridis 时设置颜色图中的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53293412/

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