add_trace: control the linetype without warning












1















I am writing a function which returns a plotly object. I managed to control the colors already. However I have trouble controlling the linetype. Currently I use something like:



plot_ly(colors=c(rep(c("#CD0C18","#1660A7"),each=3),'#9467bd'),linetypes = c(rep(c("dot","dash","solid"),2),"dot")) %>% 
add_trace(data=long_data,x=~month,y=~temperature,color=~measure,linetype=~measure,type="scatter",mode="lines",line=list(width=4)) %>%
layout(title = "Average High and Low Temperatures in New York",
xaxis = list(title = "Months", categoryorder="array", categoryarray=month),
yaxis = list (title = "Temperature (degrees F)"))


which returns me a warning:




Warning message:



plotly.js only supports 6 different linetypes




The warning makes sense, since measure has seven levels. However I would like to control the linetype without getting a warning every time I have more than 6 traces to plot - is there a way?



My sample data:



month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
high_2000 <- c(32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3)
low_2000 <- c(13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9)
mid_2000 <-apply(rbind(high_2000,low_2000),2,mean)
high_2007 <- c(36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0)
low_2007 <- c(23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6)
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)

data <- data.frame(month, high_2000, low_2000,mid_2000, high_2007, low_2007, high_2014, low_2014)

long_data<-tidyr::gather(data,measure,temperature,-month)









share|improve this question

























  • I want a solution using plotly. I know I can get this from ggplot2.

    – 5th
    Dec 29 '18 at 13:33











  • Did you try suppressWarnings?

    – Maximilian Peters
    Dec 29 '18 at 15:50











  • Nah. I want to use this in a function. Therefore suppressing the warning cripples the function - I mean if I set e.g. the grid-value to "dashed", I want to receive a warning :)

    – 5th
    Dec 29 '18 at 16:10
















1















I am writing a function which returns a plotly object. I managed to control the colors already. However I have trouble controlling the linetype. Currently I use something like:



plot_ly(colors=c(rep(c("#CD0C18","#1660A7"),each=3),'#9467bd'),linetypes = c(rep(c("dot","dash","solid"),2),"dot")) %>% 
add_trace(data=long_data,x=~month,y=~temperature,color=~measure,linetype=~measure,type="scatter",mode="lines",line=list(width=4)) %>%
layout(title = "Average High and Low Temperatures in New York",
xaxis = list(title = "Months", categoryorder="array", categoryarray=month),
yaxis = list (title = "Temperature (degrees F)"))


which returns me a warning:




Warning message:



plotly.js only supports 6 different linetypes




The warning makes sense, since measure has seven levels. However I would like to control the linetype without getting a warning every time I have more than 6 traces to plot - is there a way?



My sample data:



month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
high_2000 <- c(32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3)
low_2000 <- c(13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9)
mid_2000 <-apply(rbind(high_2000,low_2000),2,mean)
high_2007 <- c(36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0)
low_2007 <- c(23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6)
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)

data <- data.frame(month, high_2000, low_2000,mid_2000, high_2007, low_2007, high_2014, low_2014)

long_data<-tidyr::gather(data,measure,temperature,-month)









share|improve this question

























  • I want a solution using plotly. I know I can get this from ggplot2.

    – 5th
    Dec 29 '18 at 13:33











  • Did you try suppressWarnings?

    – Maximilian Peters
    Dec 29 '18 at 15:50











  • Nah. I want to use this in a function. Therefore suppressing the warning cripples the function - I mean if I set e.g. the grid-value to "dashed", I want to receive a warning :)

    – 5th
    Dec 29 '18 at 16:10














1












1








1








I am writing a function which returns a plotly object. I managed to control the colors already. However I have trouble controlling the linetype. Currently I use something like:



plot_ly(colors=c(rep(c("#CD0C18","#1660A7"),each=3),'#9467bd'),linetypes = c(rep(c("dot","dash","solid"),2),"dot")) %>% 
add_trace(data=long_data,x=~month,y=~temperature,color=~measure,linetype=~measure,type="scatter",mode="lines",line=list(width=4)) %>%
layout(title = "Average High and Low Temperatures in New York",
xaxis = list(title = "Months", categoryorder="array", categoryarray=month),
yaxis = list (title = "Temperature (degrees F)"))


which returns me a warning:




Warning message:



plotly.js only supports 6 different linetypes




The warning makes sense, since measure has seven levels. However I would like to control the linetype without getting a warning every time I have more than 6 traces to plot - is there a way?



My sample data:



month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
high_2000 <- c(32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3)
low_2000 <- c(13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9)
mid_2000 <-apply(rbind(high_2000,low_2000),2,mean)
high_2007 <- c(36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0)
low_2007 <- c(23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6)
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)

data <- data.frame(month, high_2000, low_2000,mid_2000, high_2007, low_2007, high_2014, low_2014)

long_data<-tidyr::gather(data,measure,temperature,-month)









share|improve this question
















I am writing a function which returns a plotly object. I managed to control the colors already. However I have trouble controlling the linetype. Currently I use something like:



plot_ly(colors=c(rep(c("#CD0C18","#1660A7"),each=3),'#9467bd'),linetypes = c(rep(c("dot","dash","solid"),2),"dot")) %>% 
add_trace(data=long_data,x=~month,y=~temperature,color=~measure,linetype=~measure,type="scatter",mode="lines",line=list(width=4)) %>%
layout(title = "Average High and Low Temperatures in New York",
xaxis = list(title = "Months", categoryorder="array", categoryarray=month),
yaxis = list (title = "Temperature (degrees F)"))


which returns me a warning:




Warning message:



plotly.js only supports 6 different linetypes




The warning makes sense, since measure has seven levels. However I would like to control the linetype without getting a warning every time I have more than 6 traces to plot - is there a way?



My sample data:



month <- c('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
high_2000 <- c(32.5, 37.6, 49.9, 53.0, 69.1, 75.4, 76.5, 76.6, 70.7, 60.6, 45.1, 29.3)
low_2000 <- c(13.8, 22.3, 32.5, 37.2, 49.9, 56.1, 57.7, 58.3, 51.2, 42.8, 31.6, 15.9)
mid_2000 <-apply(rbind(high_2000,low_2000),2,mean)
high_2007 <- c(36.5, 26.6, 43.6, 52.3, 71.5, 81.4, 80.5, 82.2, 76.0, 67.3, 46.1, 35.0)
low_2007 <- c(23.6, 14.0, 27.0, 36.8, 47.6, 57.7, 58.9, 61.2, 53.3, 48.5, 31.0, 23.6)
high_2014 <- c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)
low_2014 <- c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)

data <- data.frame(month, high_2000, low_2000,mid_2000, high_2007, low_2007, high_2014, low_2014)

long_data<-tidyr::gather(data,measure,temperature,-month)






r plotly






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 10 at 15:08







5th

















asked Dec 29 '18 at 13:30









5th5th

8271922




8271922













  • I want a solution using plotly. I know I can get this from ggplot2.

    – 5th
    Dec 29 '18 at 13:33











  • Did you try suppressWarnings?

    – Maximilian Peters
    Dec 29 '18 at 15:50











  • Nah. I want to use this in a function. Therefore suppressing the warning cripples the function - I mean if I set e.g. the grid-value to "dashed", I want to receive a warning :)

    – 5th
    Dec 29 '18 at 16:10



















  • I want a solution using plotly. I know I can get this from ggplot2.

    – 5th
    Dec 29 '18 at 13:33











  • Did you try suppressWarnings?

    – Maximilian Peters
    Dec 29 '18 at 15:50











  • Nah. I want to use this in a function. Therefore suppressing the warning cripples the function - I mean if I set e.g. the grid-value to "dashed", I want to receive a warning :)

    – 5th
    Dec 29 '18 at 16:10

















I want a solution using plotly. I know I can get this from ggplot2.

– 5th
Dec 29 '18 at 13:33





I want a solution using plotly. I know I can get this from ggplot2.

– 5th
Dec 29 '18 at 13:33













Did you try suppressWarnings?

– Maximilian Peters
Dec 29 '18 at 15:50





Did you try suppressWarnings?

– Maximilian Peters
Dec 29 '18 at 15:50













Nah. I want to use this in a function. Therefore suppressing the warning cripples the function - I mean if I set e.g. the grid-value to "dashed", I want to receive a warning :)

– 5th
Dec 29 '18 at 16:10





Nah. I want to use this in a function. Therefore suppressing the warning cripples the function - I mean if I set e.g. the grid-value to "dashed", I want to receive a warning :)

– 5th
Dec 29 '18 at 16:10












1 Answer
1






active

oldest

votes


















2














As can be seen here, the warning arises in



validLinetypes <- as.character(Schema$traces$scatter$attributes$line$dash$values)
if (length(pal) > length(validLinetypes)) {
warning("plotly.js only supports 6 different linetypes", call. = FALSE)
}


So, if you want to disable this warning alone, there are only two things you can do: override the whole function or manually extend Schema$traces$scatter$attributes$line$dash$values. The latter is somewhat less intrusive and can be done with



tmp <- plotly:::Schema
tmp$traces$scatter$attributes$line$dash$values <- c(tmp$traces$scatter$attributes$line$dash$values, rep(NA, 100))
assignInNamespace("Schema", tmp, ns = "plotly")


Here we add NA 100 times so that up to 106 line types now wouldn't provoke a warning. The last line overrides the Schema variable with tmp in the plotly package environment.



The vector Schema$traces$scatter$attributes$line$dash$values only gets used (through validLinetypes) here four times, and looking at those it seems like this cheating doesn't have any likely side effects.






share|improve this answer


























  • @5th, did that help?

    – Julius Vainora
    Jan 1 at 19:00











  • yes it solved the issue. I decided to make one version where I go around this problem and another where I use your solution.

    – 5th
    Jan 10 at 15:07











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%2f53970017%2fadd-trace-control-the-linetype-without-warning%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









2














As can be seen here, the warning arises in



validLinetypes <- as.character(Schema$traces$scatter$attributes$line$dash$values)
if (length(pal) > length(validLinetypes)) {
warning("plotly.js only supports 6 different linetypes", call. = FALSE)
}


So, if you want to disable this warning alone, there are only two things you can do: override the whole function or manually extend Schema$traces$scatter$attributes$line$dash$values. The latter is somewhat less intrusive and can be done with



tmp <- plotly:::Schema
tmp$traces$scatter$attributes$line$dash$values <- c(tmp$traces$scatter$attributes$line$dash$values, rep(NA, 100))
assignInNamespace("Schema", tmp, ns = "plotly")


Here we add NA 100 times so that up to 106 line types now wouldn't provoke a warning. The last line overrides the Schema variable with tmp in the plotly package environment.



The vector Schema$traces$scatter$attributes$line$dash$values only gets used (through validLinetypes) here four times, and looking at those it seems like this cheating doesn't have any likely side effects.






share|improve this answer


























  • @5th, did that help?

    – Julius Vainora
    Jan 1 at 19:00











  • yes it solved the issue. I decided to make one version where I go around this problem and another where I use your solution.

    – 5th
    Jan 10 at 15:07
















2














As can be seen here, the warning arises in



validLinetypes <- as.character(Schema$traces$scatter$attributes$line$dash$values)
if (length(pal) > length(validLinetypes)) {
warning("plotly.js only supports 6 different linetypes", call. = FALSE)
}


So, if you want to disable this warning alone, there are only two things you can do: override the whole function or manually extend Schema$traces$scatter$attributes$line$dash$values. The latter is somewhat less intrusive and can be done with



tmp <- plotly:::Schema
tmp$traces$scatter$attributes$line$dash$values <- c(tmp$traces$scatter$attributes$line$dash$values, rep(NA, 100))
assignInNamespace("Schema", tmp, ns = "plotly")


Here we add NA 100 times so that up to 106 line types now wouldn't provoke a warning. The last line overrides the Schema variable with tmp in the plotly package environment.



The vector Schema$traces$scatter$attributes$line$dash$values only gets used (through validLinetypes) here four times, and looking at those it seems like this cheating doesn't have any likely side effects.






share|improve this answer


























  • @5th, did that help?

    – Julius Vainora
    Jan 1 at 19:00











  • yes it solved the issue. I decided to make one version where I go around this problem and another where I use your solution.

    – 5th
    Jan 10 at 15:07














2












2








2







As can be seen here, the warning arises in



validLinetypes <- as.character(Schema$traces$scatter$attributes$line$dash$values)
if (length(pal) > length(validLinetypes)) {
warning("plotly.js only supports 6 different linetypes", call. = FALSE)
}


So, if you want to disable this warning alone, there are only two things you can do: override the whole function or manually extend Schema$traces$scatter$attributes$line$dash$values. The latter is somewhat less intrusive and can be done with



tmp <- plotly:::Schema
tmp$traces$scatter$attributes$line$dash$values <- c(tmp$traces$scatter$attributes$line$dash$values, rep(NA, 100))
assignInNamespace("Schema", tmp, ns = "plotly")


Here we add NA 100 times so that up to 106 line types now wouldn't provoke a warning. The last line overrides the Schema variable with tmp in the plotly package environment.



The vector Schema$traces$scatter$attributes$line$dash$values only gets used (through validLinetypes) here four times, and looking at those it seems like this cheating doesn't have any likely side effects.






share|improve this answer















As can be seen here, the warning arises in



validLinetypes <- as.character(Schema$traces$scatter$attributes$line$dash$values)
if (length(pal) > length(validLinetypes)) {
warning("plotly.js only supports 6 different linetypes", call. = FALSE)
}


So, if you want to disable this warning alone, there are only two things you can do: override the whole function or manually extend Schema$traces$scatter$attributes$line$dash$values. The latter is somewhat less intrusive and can be done with



tmp <- plotly:::Schema
tmp$traces$scatter$attributes$line$dash$values <- c(tmp$traces$scatter$attributes$line$dash$values, rep(NA, 100))
assignInNamespace("Schema", tmp, ns = "plotly")


Here we add NA 100 times so that up to 106 line types now wouldn't provoke a warning. The last line overrides the Schema variable with tmp in the plotly package environment.



The vector Schema$traces$scatter$attributes$line$dash$values only gets used (through validLinetypes) here four times, and looking at those it seems like this cheating doesn't have any likely side effects.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 30 '18 at 13:37

























answered Dec 29 '18 at 23:34









Julius VainoraJulius Vainora

35.3k76079




35.3k76079













  • @5th, did that help?

    – Julius Vainora
    Jan 1 at 19:00











  • yes it solved the issue. I decided to make one version where I go around this problem and another where I use your solution.

    – 5th
    Jan 10 at 15:07



















  • @5th, did that help?

    – Julius Vainora
    Jan 1 at 19:00











  • yes it solved the issue. I decided to make one version where I go around this problem and another where I use your solution.

    – 5th
    Jan 10 at 15:07

















@5th, did that help?

– Julius Vainora
Jan 1 at 19:00





@5th, did that help?

– Julius Vainora
Jan 1 at 19:00













yes it solved the issue. I decided to make one version where I go around this problem and another where I use your solution.

– 5th
Jan 10 at 15:07





yes it solved the issue. I decided to make one version where I go around this problem and another where I use your solution.

– 5th
Jan 10 at 15:07


















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%2f53970017%2fadd-trace-control-the-linetype-without-warning%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

Monofisismo

compose and upload a new article using a custom form

How to correct the classpath of spring boot application so that it contains a single, compatible version of...