R plotly - Different colorscales for two scatter on the same plots
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to plot two different set of data on the same figure, using two àdd_trace
commands. I specify a different colorscales for each, but the second one is ignored , so my second scatter plot has the same color gradient than the first one. How can I fix this ?
I tried the solution here, but it doesn't work (I got a warning saying 'scatter' objects don't have these attributes: 'colorscale'
).
My code (with a dataframe with random numbers for testing) :
library(plotly)
library(FactoMineR)
n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))
res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)
biplot <- plot_ly(ind) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
color=cos2[,1],
colors="OrRd",
marker=list(symbol=27, size=11)) %>%
add_trace(var,
x=var[,1],
y=var[,2],
type = 'scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
color=cos2_v[,1],
colors="BuGn",
marker=list(symbol=4, size=11))
Thanks in advance (the actual result is on the picture below).
r r-plotly
add a comment |
I'm trying to plot two different set of data on the same figure, using two àdd_trace
commands. I specify a different colorscales for each, but the second one is ignored , so my second scatter plot has the same color gradient than the first one. How can I fix this ?
I tried the solution here, but it doesn't work (I got a warning saying 'scatter' objects don't have these attributes: 'colorscale'
).
My code (with a dataframe with random numbers for testing) :
library(plotly)
library(FactoMineR)
n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))
res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)
biplot <- plot_ly(ind) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
color=cos2[,1],
colors="OrRd",
marker=list(symbol=27, size=11)) %>%
add_trace(var,
x=var[,1],
y=var[,2],
type = 'scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
color=cos2_v[,1],
colors="BuGn",
marker=list(symbol=4, size=11))
Thanks in advance (the actual result is on the picture below).
r r-plotly
Please help ! I'm stuck ! :)
– Micawber
Apr 10 '18 at 13:13
add a comment |
I'm trying to plot two different set of data on the same figure, using two àdd_trace
commands. I specify a different colorscales for each, but the second one is ignored , so my second scatter plot has the same color gradient than the first one. How can I fix this ?
I tried the solution here, but it doesn't work (I got a warning saying 'scatter' objects don't have these attributes: 'colorscale'
).
My code (with a dataframe with random numbers for testing) :
library(plotly)
library(FactoMineR)
n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))
res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)
biplot <- plot_ly(ind) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
color=cos2[,1],
colors="OrRd",
marker=list(symbol=27, size=11)) %>%
add_trace(var,
x=var[,1],
y=var[,2],
type = 'scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
color=cos2_v[,1],
colors="BuGn",
marker=list(symbol=4, size=11))
Thanks in advance (the actual result is on the picture below).
r r-plotly
I'm trying to plot two different set of data on the same figure, using two àdd_trace
commands. I specify a different colorscales for each, but the second one is ignored , so my second scatter plot has the same color gradient than the first one. How can I fix this ?
I tried the solution here, but it doesn't work (I got a warning saying 'scatter' objects don't have these attributes: 'colorscale'
).
My code (with a dataframe with random numbers for testing) :
library(plotly)
library(FactoMineR)
n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))
res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)
biplot <- plot_ly(ind) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
color=cos2[,1],
colors="OrRd",
marker=list(symbol=27, size=11)) %>%
add_trace(var,
x=var[,1],
y=var[,2],
type = 'scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
color=cos2_v[,1],
colors="BuGn",
marker=list(symbol=4, size=11))
Thanks in advance (the actual result is on the picture below).
r r-plotly
r r-plotly
edited Mar 12 '18 at 8:57
Micawber
asked Mar 12 '18 at 8:41
MicawberMicawber
14119
14119
Please help ! I'm stuck ! :)
– Micawber
Apr 10 '18 at 13:13
add a comment |
Please help ! I'm stuck ! :)
– Micawber
Apr 10 '18 at 13:13
Please help ! I'm stuck ! :)
– Micawber
Apr 10 '18 at 13:13
Please help ! I'm stuck ! :)
– Micawber
Apr 10 '18 at 13:13
add a comment |
1 Answer
1
active
oldest
votes
This works. If you do something custom you have to format the arguments according to plotly::schema()
. The colors
argument in plotly is a helper to streamline the more complicated plotly.js
syntax. Note how color
and all the other marker arguments have to be within a list called marker
, under which you have to manually format both the colorscale
(to get the colors you want) and the colorbar
(to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).
library(plotly)
library(FactoMineR)
n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))
res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)
biplot <- plot_ly(ind,showlegend=F) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=27, size=11
,color=cos2[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
)
,colorbar=list(yanchor='bottom',len=.5)
)) %>%
add_trace(x=var[,1],
y=var[,2],
type='scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=4, size=11
,color=cos2_v[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
)
,colorbar=list(yanchor='top',len=.5)
))
biplot
result with two colorscales
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49231083%2fr-plotly-different-colorscales-for-two-scatter-on-the-same-plots%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
This works. If you do something custom you have to format the arguments according to plotly::schema()
. The colors
argument in plotly is a helper to streamline the more complicated plotly.js
syntax. Note how color
and all the other marker arguments have to be within a list called marker
, under which you have to manually format both the colorscale
(to get the colors you want) and the colorbar
(to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).
library(plotly)
library(FactoMineR)
n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))
res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)
biplot <- plot_ly(ind,showlegend=F) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=27, size=11
,color=cos2[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
)
,colorbar=list(yanchor='bottom',len=.5)
)) %>%
add_trace(x=var[,1],
y=var[,2],
type='scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=4, size=11
,color=cos2_v[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
)
,colorbar=list(yanchor='top',len=.5)
))
biplot
result with two colorscales
add a comment |
This works. If you do something custom you have to format the arguments according to plotly::schema()
. The colors
argument in plotly is a helper to streamline the more complicated plotly.js
syntax. Note how color
and all the other marker arguments have to be within a list called marker
, under which you have to manually format both the colorscale
(to get the colors you want) and the colorbar
(to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).
library(plotly)
library(FactoMineR)
n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))
res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)
biplot <- plot_ly(ind,showlegend=F) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=27, size=11
,color=cos2[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
)
,colorbar=list(yanchor='bottom',len=.5)
)) %>%
add_trace(x=var[,1],
y=var[,2],
type='scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=4, size=11
,color=cos2_v[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
)
,colorbar=list(yanchor='top',len=.5)
))
biplot
result with two colorscales
add a comment |
This works. If you do something custom you have to format the arguments according to plotly::schema()
. The colors
argument in plotly is a helper to streamline the more complicated plotly.js
syntax. Note how color
and all the other marker arguments have to be within a list called marker
, under which you have to manually format both the colorscale
(to get the colors you want) and the colorbar
(to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).
library(plotly)
library(FactoMineR)
n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))
res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)
biplot <- plot_ly(ind,showlegend=F) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=27, size=11
,color=cos2[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
)
,colorbar=list(yanchor='bottom',len=.5)
)) %>%
add_trace(x=var[,1],
y=var[,2],
type='scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=4, size=11
,color=cos2_v[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
)
,colorbar=list(yanchor='top',len=.5)
))
biplot
result with two colorscales
This works. If you do something custom you have to format the arguments according to plotly::schema()
. The colors
argument in plotly is a helper to streamline the more complicated plotly.js
syntax. Note how color
and all the other marker arguments have to be within a list called marker
, under which you have to manually format both the colorscale
(to get the colors you want) and the colorbar
(to get the colorscale positioned correctly). Note also that the legend is for the shape, while the colorscale is for the color (confusingly the colorscale is not the legend).
library(plotly)
library(FactoMineR)
n <- 10 ; m <- 20 ; reps <- 6
a <- as.data.frame(cbind(matrix(seq_len(m), n, m/n),
replicate(reps, sample(c(0, 1), n, replace = TRUE))))
res.pca = PCA(a, scale.unit=TRUE, graph=F, axes=c(1,2))
ind <- as.data.frame(res.pca$ind$coord)
cos2 <- as.data.frame(res.pca$ind$cos2)
var <- as.data.frame(res.pca$var$coord)
cos2_v <- as.data.frame(res.pca$var$cos2)
biplot <- plot_ly(ind,showlegend=F) %>%
add_trace(x=ind[,1],
y=ind[,2],
type='scatter',
text=rownames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=27, size=11
,color=cos2[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'OrRd')[1])
,list(1,RColorBrewer::brewer.pal(3,'OrRd')[3])
)
,colorbar=list(yanchor='bottom',len=.5)
)) %>%
add_trace(x=var[,1],
y=var[,2],
type='scatter',
text=colnames(a),
textposition='top',
mode="markers+text",
marker=list(symbol=4, size=11
,color=cos2_v[,1]
,colorscale=list(
list(0,RColorBrewer::brewer.pal(3,'BuGn')[1])
,list(1,RColorBrewer::brewer.pal(3,'BuGn')[3])
)
,colorbar=list(yanchor='top',len=.5)
))
biplot
result with two colorscales
edited Nov 17 '18 at 18:23
answered Nov 17 '18 at 4:36
Brooks AmbroseBrooks Ambrose
584
584
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49231083%2fr-plotly-different-colorscales-for-two-scatter-on-the-same-plots%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Please help ! I'm stuck ! :)
– Micawber
Apr 10 '18 at 13:13