Set the max value in colormap when using scale_color_viridis












5















Here is the code:



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)


You can see the color gradient follows the value of df_2$speedkt . The maximum value of df_2$speedkt is around 2.6.



So, the maximum value of the colormap in the legend is around 2.8.



But I wish to change the maximum value of the colormap to 4.0.



What I can do?



Colormap in Legend Highlighted










share|improve this question




















  • 1





    Note that ggplot2 has these scales build in nowadays, scale_color_viridis_c in this case.

    – Axeman
    Nov 14 '18 at 5:03






  • 2





    FYI, no need to use df_2$ inside ggplot call. You can just use bare column names

    – Tung
    Nov 14 '18 at 5:04
















5















Here is the code:



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)


You can see the color gradient follows the value of df_2$speedkt . The maximum value of df_2$speedkt is around 2.6.



So, the maximum value of the colormap in the legend is around 2.8.



But I wish to change the maximum value of the colormap to 4.0.



What I can do?



Colormap in Legend Highlighted










share|improve this question




















  • 1





    Note that ggplot2 has these scales build in nowadays, scale_color_viridis_c in this case.

    – Axeman
    Nov 14 '18 at 5:03






  • 2





    FYI, no need to use df_2$ inside ggplot call. You can just use bare column names

    – Tung
    Nov 14 '18 at 5:04














5












5








5








Here is the code:



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)


You can see the color gradient follows the value of df_2$speedkt . The maximum value of df_2$speedkt is around 2.6.



So, the maximum value of the colormap in the legend is around 2.8.



But I wish to change the maximum value of the colormap to 4.0.



What I can do?



Colormap in Legend Highlighted










share|improve this question
















Here is the code:



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)


You can see the color gradient follows the value of df_2$speedkt . The maximum value of df_2$speedkt is around 2.6.



So, the maximum value of the colormap in the legend is around 2.8.



But I wish to change the maximum value of the colormap to 4.0.



What I can do?



Colormap in Legend Highlighted







r ggplot2 viridis






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 7:15









zx8754

29.7k76399




29.7k76399










asked Nov 14 '18 at 4:57









Justin Justin

666




666








  • 1





    Note that ggplot2 has these scales build in nowadays, scale_color_viridis_c in this case.

    – Axeman
    Nov 14 '18 at 5:03






  • 2





    FYI, no need to use df_2$ inside ggplot call. You can just use bare column names

    – Tung
    Nov 14 '18 at 5:04














  • 1





    Note that ggplot2 has these scales build in nowadays, scale_color_viridis_c in this case.

    – Axeman
    Nov 14 '18 at 5:03






  • 2





    FYI, no need to use df_2$ inside ggplot call. You can just use bare column names

    – Tung
    Nov 14 '18 at 5:04








1




1





Note that ggplot2 has these scales build in nowadays, scale_color_viridis_c in this case.

– Axeman
Nov 14 '18 at 5:03





Note that ggplot2 has these scales build in nowadays, scale_color_viridis_c in this case.

– Axeman
Nov 14 '18 at 5:03




2




2





FYI, no need to use df_2$ inside ggplot call. You can just use bare column names

– Tung
Nov 14 '18 at 5:04





FYI, no need to use df_2$ inside ggplot call. You can just use bare column names

– Tung
Nov 14 '18 at 5:04












1 Answer
1






active

oldest

votes


















4














To answer your main question, you should be able to pass limits = c(0, 4) as an argument to viridis::scale_color_viridis(), i.e.



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


A couple of other points:




  • Newer versions of ggplot2 have viridis scales built in, so you should be able to use:


scale_color_viridis_c(option = "B", direction = -1, limits = c(0, 4))



  • In general, it's better not to use df$col in aes(), just use the column name, e.g.


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


ggplot will look up the column names within the dataframe.






share|improve this answer
























  • can u insert the output ?

    – sai saran
    Nov 14 '18 at 5:26






  • 2





    @saisaran: The question doesn't provide an easy way to reproduce the data/plot, so I can't show how it changes the plot.

    – Marius
    Nov 14 '18 at 5:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293412%2fset-the-max-value-in-colormap-when-using-scale-color-viridis%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














To answer your main question, you should be able to pass limits = c(0, 4) as an argument to viridis::scale_color_viridis(), i.e.



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


A couple of other points:




  • Newer versions of ggplot2 have viridis scales built in, so you should be able to use:


scale_color_viridis_c(option = "B", direction = -1, limits = c(0, 4))



  • In general, it's better not to use df$col in aes(), just use the column name, e.g.


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


ggplot will look up the column names within the dataframe.






share|improve this answer
























  • can u insert the output ?

    – sai saran
    Nov 14 '18 at 5:26






  • 2





    @saisaran: The question doesn't provide an easy way to reproduce the data/plot, so I can't show how it changes the plot.

    – Marius
    Nov 14 '18 at 5:30
















4














To answer your main question, you should be able to pass limits = c(0, 4) as an argument to viridis::scale_color_viridis(), i.e.



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


A couple of other points:




  • Newer versions of ggplot2 have viridis scales built in, so you should be able to use:


scale_color_viridis_c(option = "B", direction = -1, limits = c(0, 4))



  • In general, it's better not to use df$col in aes(), just use the column name, e.g.


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


ggplot will look up the column names within the dataframe.






share|improve this answer
























  • can u insert the output ?

    – sai saran
    Nov 14 '18 at 5:26






  • 2





    @saisaran: The question doesn't provide an easy way to reproduce the data/plot, so I can't show how it changes the plot.

    – Marius
    Nov 14 '18 at 5:30














4












4








4







To answer your main question, you should be able to pass limits = c(0, 4) as an argument to viridis::scale_color_viridis(), i.e.



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


A couple of other points:




  • Newer versions of ggplot2 have viridis scales built in, so you should be able to use:


scale_color_viridis_c(option = "B", direction = -1, limits = c(0, 4))



  • In general, it's better not to use df$col in aes(), just use the column name, e.g.


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


ggplot will look up the column names within the dataframe.






share|improve this answer













To answer your main question, you should be able to pass limits = c(0, 4) as an argument to viridis::scale_color_viridis(), i.e.



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


A couple of other points:




  • Newer versions of ggplot2 have viridis scales built in, so you should be able to use:


scale_color_viridis_c(option = "B", direction = -1, limits = c(0, 4))



  • In general, it's better not to use df$col in aes(), just use the column name, e.g.


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


ggplot will look up the column names within the dataframe.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 5:02









MariusMarius

31.9k97174




31.9k97174













  • can u insert the output ?

    – sai saran
    Nov 14 '18 at 5:26






  • 2





    @saisaran: The question doesn't provide an easy way to reproduce the data/plot, so I can't show how it changes the plot.

    – Marius
    Nov 14 '18 at 5:30



















  • can u insert the output ?

    – sai saran
    Nov 14 '18 at 5:26






  • 2





    @saisaran: The question doesn't provide an easy way to reproduce the data/plot, so I can't show how it changes the plot.

    – Marius
    Nov 14 '18 at 5:30

















can u insert the output ?

– sai saran
Nov 14 '18 at 5:26





can u insert the output ?

– sai saran
Nov 14 '18 at 5:26




2




2





@saisaran: The question doesn't provide an easy way to reproduce the data/plot, so I can't show how it changes the plot.

– Marius
Nov 14 '18 at 5:30





@saisaran: The question doesn't provide an easy way to reproduce the data/plot, so I can't show how it changes the plot.

– Marius
Nov 14 '18 at 5:30


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293412%2fset-the-max-value-in-colormap-when-using-scale-color-viridis%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues