【问题标题】:Set the max value in colormap when using scale_color_viridis使用 scale_color_viridis 时设置颜色图中的最大值
【发布时间】:2018-11-14 04:57:20
【问题描述】:

代码如下:

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。

我能做什么?

【问题讨论】:

  • 请注意,ggplot2 现在内置了这些音阶,在这种情况下为 scale_color_viridis_c
  • 仅供参考,无需在ggplot 调用中使用df_2$。您可以只使用裸列名称

标签: r ggplot2 viridis


【解决方案1】:

要回答您的主要问题,您应该能够将limits = c(0, 4) 作为参数传递给viridis::scale_color_viridis(),即

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

其他几点:

  • 较新版本的ggplot2 内置了绿色鳞片,因此您应该可以使用:
scale_color_viridis_c(option = "B", direction = -1, limits = c(0, 4))
  • 一般情况下,aes()中最好不要使用df$col,直接使用列名即可,例如
ggplot(df_2, aes(x = X, xend = X + dx, y = Y))

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

【讨论】:

  • 你能插入输出吗?
  • @saisaran:这个问题没有提供一种简单的方法来重现数据/情节,所以我无法展示它如何改变情节。
猜你喜欢
  • 2017-10-01
  • 2016-02-20
  • 2015-12-28
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
相关资源
最近更新 更多