ggplot2: Combining group, color and linetype
up vote
8
down vote
favorite
I have a data base with 3 factors (condition, measure and time) and would like to plot them using the x-axis, the color/group and the linetype.
As an exemple, my data looks like this:
DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8)
> DT
condition measure time score
1 control A pre-test 1
2 control A post-test 2
3 control B pre-test 3
4 control B post-test 4
5 experimental A pre-test 5
6 experimental A post-test 6
7 experimental B pre-test 7
8 experimental B post-test 8
My goal is to draw a graph like this:

I tried:
ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
geom_line() +
geom_point()
But it returns this error:
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
What am I missing?
r ggplot2
add a comment |
up vote
8
down vote
favorite
I have a data base with 3 factors (condition, measure and time) and would like to plot them using the x-axis, the color/group and the linetype.
As an exemple, my data looks like this:
DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8)
> DT
condition measure time score
1 control A pre-test 1
2 control A post-test 2
3 control B pre-test 3
4 control B post-test 4
5 experimental A pre-test 5
6 experimental A post-test 6
7 experimental B pre-test 7
8 experimental B post-test 8
My goal is to draw a graph like this:

I tried:
ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
geom_line() +
geom_point()
But it returns this error:
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
What am I missing?
r ggplot2
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I have a data base with 3 factors (condition, measure and time) and would like to plot them using the x-axis, the color/group and the linetype.
As an exemple, my data looks like this:
DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8)
> DT
condition measure time score
1 control A pre-test 1
2 control A post-test 2
3 control B pre-test 3
4 control B post-test 4
5 experimental A pre-test 5
6 experimental A post-test 6
7 experimental B pre-test 7
8 experimental B post-test 8
My goal is to draw a graph like this:

I tried:
ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
geom_line() +
geom_point()
But it returns this error:
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
What am I missing?
r ggplot2
I have a data base with 3 factors (condition, measure and time) and would like to plot them using the x-axis, the color/group and the linetype.
As an exemple, my data looks like this:
DT <- data.frame(condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8)
> DT
condition measure time score
1 control A pre-test 1
2 control A post-test 2
3 control B pre-test 3
4 control B post-test 4
5 experimental A pre-test 5
6 experimental A post-test 6
7 experimental B pre-test 7
8 experimental B post-test 8
My goal is to draw a graph like this:

I tried:
ggplot(DT, aes(time, score, group = measure, color = measure, linetype = condition)) +
geom_line() +
geom_point()
But it returns this error:
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
What am I missing?
r ggplot2
r ggplot2
edited Dec 2 at 16:08
asked Dec 2 at 15:36
mat
514730
514730
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
7
down vote
accepted
You want to use
ggplot(DT, aes(time, score, group = interaction(measure, condition),
color = measure, linetype = condition)) +
geom_line() + geom_point()

because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.
add a comment |
up vote
4
down vote
data.frame(
condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8
) -> DT
DT_wide <- tidyr::spread(DT, time, score)
ggplot() +
geom_segment(
data = DT_wide,
aes(
x = "pre-test", xend = "post-test",
y = `pre-test`, yend = `post-test`,
color = measure,
linetype = condition
)
) +
geom_point(
data = DT,
aes(time, score, color = measure)
)

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',
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%2f53581787%2fggplot2-combining-group-color-and-linetype%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
You want to use
ggplot(DT, aes(time, score, group = interaction(measure, condition),
color = measure, linetype = condition)) +
geom_line() + geom_point()

because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.
add a comment |
up vote
7
down vote
accepted
You want to use
ggplot(DT, aes(time, score, group = interaction(measure, condition),
color = measure, linetype = condition)) +
geom_line() + geom_point()

because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.
add a comment |
up vote
7
down vote
accepted
up vote
7
down vote
accepted
You want to use
ggplot(DT, aes(time, score, group = interaction(measure, condition),
color = measure, linetype = condition)) +
geom_line() + geom_point()

because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.
You want to use
ggplot(DT, aes(time, score, group = interaction(measure, condition),
color = measure, linetype = condition)) +
geom_line() + geom_point()

because the actual grouping is not only by measure but also by condition. When grouping by measure alone, I guess it's asking for kind of parallelograms rather than lines.
answered Dec 2 at 15:49
Julius Vainora
30.5k75878
30.5k75878
add a comment |
add a comment |
up vote
4
down vote
data.frame(
condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8
) -> DT
DT_wide <- tidyr::spread(DT, time, score)
ggplot() +
geom_segment(
data = DT_wide,
aes(
x = "pre-test", xend = "post-test",
y = `pre-test`, yend = `post-test`,
color = measure,
linetype = condition
)
) +
geom_point(
data = DT,
aes(time, score, color = measure)
)

add a comment |
up vote
4
down vote
data.frame(
condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8
) -> DT
DT_wide <- tidyr::spread(DT, time, score)
ggplot() +
geom_segment(
data = DT_wide,
aes(
x = "pre-test", xend = "post-test",
y = `pre-test`, yend = `post-test`,
color = measure,
linetype = condition
)
) +
geom_point(
data = DT,
aes(time, score, color = measure)
)

add a comment |
up vote
4
down vote
up vote
4
down vote
data.frame(
condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8
) -> DT
DT_wide <- tidyr::spread(DT, time, score)
ggplot() +
geom_segment(
data = DT_wide,
aes(
x = "pre-test", xend = "post-test",
y = `pre-test`, yend = `post-test`,
color = measure,
linetype = condition
)
) +
geom_point(
data = DT,
aes(time, score, color = measure)
)

data.frame(
condition = rep(c("control", "experimental"), each = 4),
measure = rep(c("A", "A", "B", "B"), 2),
time = rep(c("pre-test", "post-test"), 4),
score = 1:8
) -> DT
DT_wide <- tidyr::spread(DT, time, score)
ggplot() +
geom_segment(
data = DT_wide,
aes(
x = "pre-test", xend = "post-test",
y = `pre-test`, yend = `post-test`,
color = measure,
linetype = condition
)
) +
geom_point(
data = DT,
aes(time, score, color = measure)
)

answered Dec 2 at 15:49
hrbrmstr
59.8k685146
59.8k685146
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53581787%2fggplot2-combining-group-color-and-linetype%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