这是代码:
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)
Run Code Online (Sandbox Code Playgroud)
您可以看到颜色渐变遵循的值df_2$speedkt.最大值df_2$speedkt约为2.6.
因此,图例中色彩映射的最大值约为2.8.
但我希望将colormap的最大值更改为4.0.
我可以做什么?
要回答您的主要问题,您应该可以limits = c(0, 4)将参数作为传递给viridis::scale_color_viridis(),即
viridis::scale_color_viridis(option = "B", direction = -1, limits = c(0, 4))
Run Code Online (Sandbox Code Playgroud)
其他几点:
ggplot2内置了viridis秤,因此您应该可以使用:scale_color_viridis_c(option = "B", direction = -1, limits = c(0, 4))
Run Code Online (Sandbox Code Playgroud)
df$col中aes(),只需要使用列名,如ggplot(df_2, aes(x = X, xend = X + dx, y = Y))
Run Code Online (Sandbox Code Playgroud)
ggplot将在数据框中查找列名。