Change fill color of dotplot_geom but retain fill color of boxplot
I have included reproducible code from the mtcars data set below:
library(ggplot2)
ggplot(mtcars,aes(as.factor(vs),disp))+
stat_boxplot(geom ='errorbar',position=position_dodge(width=.88),aes(fill=as.factor(am))) +
geom_boxplot(position=position_dodge(width=.88),aes(fill=as.factor(am)))+
geom_dotplot(binaxis='y', stackdir='center',dotsize=0.25,position=position_dodge(width=.88),stackratio=1.1,aes(fill=as.factor(am),color=as.factor(am)))
I would like to make the dots from the dotplot grey/black, while having control over the fill colour of the boxplots.
As you can see, I need the fill colour aesthetic for both the boxplots and dotplots in order to separate the three factors on the x axis by the necessary group. Additionally, the dotplot's color aesthetic maps to the outline of the dots, while the fill aesthetic maps to the inside of the dots, so I need to use both, since I would like one seamless point.
Using the scale_fill_manual function changes the colour of both boxplots and dots, but I need to have control over the colour of both separately.
I have found this other question on stackoverflow but I'm struggling to apply it to my case.
How to scale the size of line and point separately in ggplot2
r ggplot2
add a comment |
I have included reproducible code from the mtcars data set below:
library(ggplot2)
ggplot(mtcars,aes(as.factor(vs),disp))+
stat_boxplot(geom ='errorbar',position=position_dodge(width=.88),aes(fill=as.factor(am))) +
geom_boxplot(position=position_dodge(width=.88),aes(fill=as.factor(am)))+
geom_dotplot(binaxis='y', stackdir='center',dotsize=0.25,position=position_dodge(width=.88),stackratio=1.1,aes(fill=as.factor(am),color=as.factor(am)))
I would like to make the dots from the dotplot grey/black, while having control over the fill colour of the boxplots.
As you can see, I need the fill colour aesthetic for both the boxplots and dotplots in order to separate the three factors on the x axis by the necessary group. Additionally, the dotplot's color aesthetic maps to the outline of the dots, while the fill aesthetic maps to the inside of the dots, so I need to use both, since I would like one seamless point.
Using the scale_fill_manual function changes the colour of both boxplots and dots, but I need to have control over the colour of both separately.
I have found this other question on stackoverflow but I'm struggling to apply it to my case.
How to scale the size of line and point separately in ggplot2
r ggplot2
add a comment |
I have included reproducible code from the mtcars data set below:
library(ggplot2)
ggplot(mtcars,aes(as.factor(vs),disp))+
stat_boxplot(geom ='errorbar',position=position_dodge(width=.88),aes(fill=as.factor(am))) +
geom_boxplot(position=position_dodge(width=.88),aes(fill=as.factor(am)))+
geom_dotplot(binaxis='y', stackdir='center',dotsize=0.25,position=position_dodge(width=.88),stackratio=1.1,aes(fill=as.factor(am),color=as.factor(am)))
I would like to make the dots from the dotplot grey/black, while having control over the fill colour of the boxplots.
As you can see, I need the fill colour aesthetic for both the boxplots and dotplots in order to separate the three factors on the x axis by the necessary group. Additionally, the dotplot's color aesthetic maps to the outline of the dots, while the fill aesthetic maps to the inside of the dots, so I need to use both, since I would like one seamless point.
Using the scale_fill_manual function changes the colour of both boxplots and dots, but I need to have control over the colour of both separately.
I have found this other question on stackoverflow but I'm struggling to apply it to my case.
How to scale the size of line and point separately in ggplot2
r ggplot2
I have included reproducible code from the mtcars data set below:
library(ggplot2)
ggplot(mtcars,aes(as.factor(vs),disp))+
stat_boxplot(geom ='errorbar',position=position_dodge(width=.88),aes(fill=as.factor(am))) +
geom_boxplot(position=position_dodge(width=.88),aes(fill=as.factor(am)))+
geom_dotplot(binaxis='y', stackdir='center',dotsize=0.25,position=position_dodge(width=.88),stackratio=1.1,aes(fill=as.factor(am),color=as.factor(am)))
I would like to make the dots from the dotplot grey/black, while having control over the fill colour of the boxplots.
As you can see, I need the fill colour aesthetic for both the boxplots and dotplots in order to separate the three factors on the x axis by the necessary group. Additionally, the dotplot's color aesthetic maps to the outline of the dots, while the fill aesthetic maps to the inside of the dots, so I need to use both, since I would like one seamless point.
Using the scale_fill_manual function changes the colour of both boxplots and dots, but I need to have control over the colour of both separately.
I have found this other question on stackoverflow but I'm struggling to apply it to my case.
How to scale the size of line and point separately in ggplot2
r ggplot2
r ggplot2
asked Nov 23 '18 at 8:22
GerardGerard
105
105
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In geom_dotplot you could only map factor(am) to color and override the values at end using scale_color_manual
ggplot(mtcars, aes(as.factor(vs), disp)) +
stat_boxplot(geom = 'errorbar',
position = position_dodge(width = .88),
aes(fill = as.factor(am))) +
geom_boxplot(position = position_dodge(width = .88), aes(fill = as.factor(am))) +
geom_dotplot(
binaxis = 'y',
stackdir = 'center',
dotsize = .5, # changed dotsize to .5 to better see the points
position = position_dodge(width = .88),
stackratio = 1.1,
aes(color = as.factor(am)),
fill = "grey70",
show.legend = FALSE
) +
scale_color_manual(values = c("black", "black"))

Yes! Thank you, this is a great work-around. Sad I didn't come up with it myself (I swear I tried everything else)
– Gerard
Nov 23 '18 at 12:16
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%2f53442952%2fchange-fill-color-of-dotplot-geom-but-retain-fill-color-of-boxplot%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
In geom_dotplot you could only map factor(am) to color and override the values at end using scale_color_manual
ggplot(mtcars, aes(as.factor(vs), disp)) +
stat_boxplot(geom = 'errorbar',
position = position_dodge(width = .88),
aes(fill = as.factor(am))) +
geom_boxplot(position = position_dodge(width = .88), aes(fill = as.factor(am))) +
geom_dotplot(
binaxis = 'y',
stackdir = 'center',
dotsize = .5, # changed dotsize to .5 to better see the points
position = position_dodge(width = .88),
stackratio = 1.1,
aes(color = as.factor(am)),
fill = "grey70",
show.legend = FALSE
) +
scale_color_manual(values = c("black", "black"))

Yes! Thank you, this is a great work-around. Sad I didn't come up with it myself (I swear I tried everything else)
– Gerard
Nov 23 '18 at 12:16
add a comment |
In geom_dotplot you could only map factor(am) to color and override the values at end using scale_color_manual
ggplot(mtcars, aes(as.factor(vs), disp)) +
stat_boxplot(geom = 'errorbar',
position = position_dodge(width = .88),
aes(fill = as.factor(am))) +
geom_boxplot(position = position_dodge(width = .88), aes(fill = as.factor(am))) +
geom_dotplot(
binaxis = 'y',
stackdir = 'center',
dotsize = .5, # changed dotsize to .5 to better see the points
position = position_dodge(width = .88),
stackratio = 1.1,
aes(color = as.factor(am)),
fill = "grey70",
show.legend = FALSE
) +
scale_color_manual(values = c("black", "black"))

Yes! Thank you, this is a great work-around. Sad I didn't come up with it myself (I swear I tried everything else)
– Gerard
Nov 23 '18 at 12:16
add a comment |
In geom_dotplot you could only map factor(am) to color and override the values at end using scale_color_manual
ggplot(mtcars, aes(as.factor(vs), disp)) +
stat_boxplot(geom = 'errorbar',
position = position_dodge(width = .88),
aes(fill = as.factor(am))) +
geom_boxplot(position = position_dodge(width = .88), aes(fill = as.factor(am))) +
geom_dotplot(
binaxis = 'y',
stackdir = 'center',
dotsize = .5, # changed dotsize to .5 to better see the points
position = position_dodge(width = .88),
stackratio = 1.1,
aes(color = as.factor(am)),
fill = "grey70",
show.legend = FALSE
) +
scale_color_manual(values = c("black", "black"))

In geom_dotplot you could only map factor(am) to color and override the values at end using scale_color_manual
ggplot(mtcars, aes(as.factor(vs), disp)) +
stat_boxplot(geom = 'errorbar',
position = position_dodge(width = .88),
aes(fill = as.factor(am))) +
geom_boxplot(position = position_dodge(width = .88), aes(fill = as.factor(am))) +
geom_dotplot(
binaxis = 'y',
stackdir = 'center',
dotsize = .5, # changed dotsize to .5 to better see the points
position = position_dodge(width = .88),
stackratio = 1.1,
aes(color = as.factor(am)),
fill = "grey70",
show.legend = FALSE
) +
scale_color_manual(values = c("black", "black"))

answered Nov 23 '18 at 8:34
markusmarkus
14.8k11336
14.8k11336
Yes! Thank you, this is a great work-around. Sad I didn't come up with it myself (I swear I tried everything else)
– Gerard
Nov 23 '18 at 12:16
add a comment |
Yes! Thank you, this is a great work-around. Sad I didn't come up with it myself (I swear I tried everything else)
– Gerard
Nov 23 '18 at 12:16
Yes! Thank you, this is a great work-around. Sad I didn't come up with it myself (I swear I tried everything else)
– Gerard
Nov 23 '18 at 12:16
Yes! Thank you, this is a great work-around. Sad I didn't come up with it myself (I swear I tried everything else)
– Gerard
Nov 23 '18 at 12:16
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%2f53442952%2fchange-fill-color-of-dotplot-geom-but-retain-fill-color-of-boxplot%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