Plotting Multiple Lines in GGplot with Different lines corresponding to a different year
I am trying to use ggplot to plot multiple lines (categorized by years) for means for a categorical variable. I am stumped and have tried a variety of things but can't get exactly what I want. I have raw observations that have a year flag on each observation and a Loss value attached to them but here is a snapshot of me trying to group the mean losses by year/judicial orientation.
I want to aggregate all the Loss values by categorical variable level and then aggregate those further by the Year
My goal is this:
I want one graph that has a variable number of levels depending on variable (for JudicialOrientation I have 3 levels: Defense, Neutral, Plaintiff) so those would be the x values, I then want to have a line graph connecting the means between each 3 levels but I want multiple lines that represent 2006, 2007, 2008 etc.
So I would have different colored lines that correspond to a different year's MeanLoss value for that particular level. I hope this makes sense.
I am new to ggplot and I see some people use one line and others use multiple lines. I am ok with either approach.
An attempt so far:
ggplot() +geom_line(data=df1, aes(x=JudicialOrientation, y = MeanLoss, color=Year))
An example data frame with some dplyr code to obtain aggregate means:
df <-data.frame(Year=c("2006","2006","2006","2007","2007","2007","2008","2009","2010","2010","2009","2009"),
JudicialOrientation=c("Defense","Plaintiff","Plaintiff","Neutral","Defense","Plaintiff","Defense","Plaintiff","Neutral","Neutral","Plaintiff","Defense"),
Loss = c(100000,100,2500,100000,25000,0,7500,5200, 900,100,0,50)
)
df1 <- df%>% group_by(Year,JudicialOrientation) %>% summarise(MeanLoss =mean(Loss))
Let me know any tips you may have. Thanks!
r ggplot2 aggregate
add a comment |
I am trying to use ggplot to plot multiple lines (categorized by years) for means for a categorical variable. I am stumped and have tried a variety of things but can't get exactly what I want. I have raw observations that have a year flag on each observation and a Loss value attached to them but here is a snapshot of me trying to group the mean losses by year/judicial orientation.
I want to aggregate all the Loss values by categorical variable level and then aggregate those further by the Year
My goal is this:
I want one graph that has a variable number of levels depending on variable (for JudicialOrientation I have 3 levels: Defense, Neutral, Plaintiff) so those would be the x values, I then want to have a line graph connecting the means between each 3 levels but I want multiple lines that represent 2006, 2007, 2008 etc.
So I would have different colored lines that correspond to a different year's MeanLoss value for that particular level. I hope this makes sense.
I am new to ggplot and I see some people use one line and others use multiple lines. I am ok with either approach.
An attempt so far:
ggplot() +geom_line(data=df1, aes(x=JudicialOrientation, y = MeanLoss, color=Year))
An example data frame with some dplyr code to obtain aggregate means:
df <-data.frame(Year=c("2006","2006","2006","2007","2007","2007","2008","2009","2010","2010","2009","2009"),
JudicialOrientation=c("Defense","Plaintiff","Plaintiff","Neutral","Defense","Plaintiff","Defense","Plaintiff","Neutral","Neutral","Plaintiff","Defense"),
Loss = c(100000,100,2500,100000,25000,0,7500,5200, 900,100,0,50)
)
df1 <- df%>% group_by(Year,JudicialOrientation) %>% summarise(MeanLoss =mean(Loss))
Let me know any tips you may have. Thanks!
r ggplot2 aggregate
Please post the code you have tried so far?
– TeeKea
Nov 20 '18 at 20:54
I edited my post.This should give you a sample data frame you can work with. I want to aggregate the Loss column by JudicialOrientation and then plot a different line for each year in the data set to give aggregate meanLoss for each JudicialOrientation (on the x-axis preferably).
– Coldchain9
Nov 20 '18 at 21:01
add a comment |
I am trying to use ggplot to plot multiple lines (categorized by years) for means for a categorical variable. I am stumped and have tried a variety of things but can't get exactly what I want. I have raw observations that have a year flag on each observation and a Loss value attached to them but here is a snapshot of me trying to group the mean losses by year/judicial orientation.
I want to aggregate all the Loss values by categorical variable level and then aggregate those further by the Year
My goal is this:
I want one graph that has a variable number of levels depending on variable (for JudicialOrientation I have 3 levels: Defense, Neutral, Plaintiff) so those would be the x values, I then want to have a line graph connecting the means between each 3 levels but I want multiple lines that represent 2006, 2007, 2008 etc.
So I would have different colored lines that correspond to a different year's MeanLoss value for that particular level. I hope this makes sense.
I am new to ggplot and I see some people use one line and others use multiple lines. I am ok with either approach.
An attempt so far:
ggplot() +geom_line(data=df1, aes(x=JudicialOrientation, y = MeanLoss, color=Year))
An example data frame with some dplyr code to obtain aggregate means:
df <-data.frame(Year=c("2006","2006","2006","2007","2007","2007","2008","2009","2010","2010","2009","2009"),
JudicialOrientation=c("Defense","Plaintiff","Plaintiff","Neutral","Defense","Plaintiff","Defense","Plaintiff","Neutral","Neutral","Plaintiff","Defense"),
Loss = c(100000,100,2500,100000,25000,0,7500,5200, 900,100,0,50)
)
df1 <- df%>% group_by(Year,JudicialOrientation) %>% summarise(MeanLoss =mean(Loss))
Let me know any tips you may have. Thanks!
r ggplot2 aggregate
I am trying to use ggplot to plot multiple lines (categorized by years) for means for a categorical variable. I am stumped and have tried a variety of things but can't get exactly what I want. I have raw observations that have a year flag on each observation and a Loss value attached to them but here is a snapshot of me trying to group the mean losses by year/judicial orientation.
I want to aggregate all the Loss values by categorical variable level and then aggregate those further by the Year
My goal is this:
I want one graph that has a variable number of levels depending on variable (for JudicialOrientation I have 3 levels: Defense, Neutral, Plaintiff) so those would be the x values, I then want to have a line graph connecting the means between each 3 levels but I want multiple lines that represent 2006, 2007, 2008 etc.
So I would have different colored lines that correspond to a different year's MeanLoss value for that particular level. I hope this makes sense.
I am new to ggplot and I see some people use one line and others use multiple lines. I am ok with either approach.
An attempt so far:
ggplot() +geom_line(data=df1, aes(x=JudicialOrientation, y = MeanLoss, color=Year))
An example data frame with some dplyr code to obtain aggregate means:
df <-data.frame(Year=c("2006","2006","2006","2007","2007","2007","2008","2009","2010","2010","2009","2009"),
JudicialOrientation=c("Defense","Plaintiff","Plaintiff","Neutral","Defense","Plaintiff","Defense","Plaintiff","Neutral","Neutral","Plaintiff","Defense"),
Loss = c(100000,100,2500,100000,25000,0,7500,5200, 900,100,0,50)
)
df1 <- df%>% group_by(Year,JudicialOrientation) %>% summarise(MeanLoss =mean(Loss))
Let me know any tips you may have. Thanks!
r ggplot2 aggregate
r ggplot2 aggregate
edited Nov 20 '18 at 21:05
Coldchain9
asked Nov 20 '18 at 20:51
Coldchain9Coldchain9
325
325
Please post the code you have tried so far?
– TeeKea
Nov 20 '18 at 20:54
I edited my post.This should give you a sample data frame you can work with. I want to aggregate the Loss column by JudicialOrientation and then plot a different line for each year in the data set to give aggregate meanLoss for each JudicialOrientation (on the x-axis preferably).
– Coldchain9
Nov 20 '18 at 21:01
add a comment |
Please post the code you have tried so far?
– TeeKea
Nov 20 '18 at 20:54
I edited my post.This should give you a sample data frame you can work with. I want to aggregate the Loss column by JudicialOrientation and then plot a different line for each year in the data set to give aggregate meanLoss for each JudicialOrientation (on the x-axis preferably).
– Coldchain9
Nov 20 '18 at 21:01
Please post the code you have tried so far?
– TeeKea
Nov 20 '18 at 20:54
Please post the code you have tried so far?
– TeeKea
Nov 20 '18 at 20:54
I edited my post.This should give you a sample data frame you can work with. I want to aggregate the Loss column by JudicialOrientation and then plot a different line for each year in the data set to give aggregate meanLoss for each JudicialOrientation (on the x-axis preferably).
– Coldchain9
Nov 20 '18 at 21:01
I edited my post.This should give you a sample data frame you can work with. I want to aggregate the Loss column by JudicialOrientation and then plot a different line for each year in the data set to give aggregate meanLoss for each JudicialOrientation (on the x-axis preferably).
– Coldchain9
Nov 20 '18 at 21:01
add a comment |
1 Answer
1
active
oldest
votes
I suppose your saw the message after you run your code.
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
So adjusting the group aesthetic would give you this
ggplot(data = df1, aes(x = JudicialOrientation, y = MeanLoss, color=Year, group = Year)) +
geom_line() +
geom_point()
I added geom_point
so you see that the values for e.g. 2008 are actually there. You wouldn't see them with only geom_line
. Hope this helps.
You could even let stat_summary
do the aggregation for you and use df
directly.
Here is how
ggplot(df, aes(x = JudicialOrientation, y = Loss, color = Year, group = Year)) +
stat_summary(geom = "line", fun.y = mean) +
stat_summary(geom = "point", fun.y = mean)
1
This is very useful thanks. I did not know I needed a point when using categorical data to make it work properly.
– Coldchain9
Nov 20 '18 at 21:17
@Coldchain9 You don't need the points actually - you simply cannot draw a line for just one observation (that's what you brought you here, I guess).
– markus
Nov 20 '18 at 21:24
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%2f53401326%2fplotting-multiple-lines-in-ggplot-with-different-lines-corresponding-to-a-differ%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
I suppose your saw the message after you run your code.
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
So adjusting the group aesthetic would give you this
ggplot(data = df1, aes(x = JudicialOrientation, y = MeanLoss, color=Year, group = Year)) +
geom_line() +
geom_point()
I added geom_point
so you see that the values for e.g. 2008 are actually there. You wouldn't see them with only geom_line
. Hope this helps.
You could even let stat_summary
do the aggregation for you and use df
directly.
Here is how
ggplot(df, aes(x = JudicialOrientation, y = Loss, color = Year, group = Year)) +
stat_summary(geom = "line", fun.y = mean) +
stat_summary(geom = "point", fun.y = mean)
1
This is very useful thanks. I did not know I needed a point when using categorical data to make it work properly.
– Coldchain9
Nov 20 '18 at 21:17
@Coldchain9 You don't need the points actually - you simply cannot draw a line for just one observation (that's what you brought you here, I guess).
– markus
Nov 20 '18 at 21:24
add a comment |
I suppose your saw the message after you run your code.
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
So adjusting the group aesthetic would give you this
ggplot(data = df1, aes(x = JudicialOrientation, y = MeanLoss, color=Year, group = Year)) +
geom_line() +
geom_point()
I added geom_point
so you see that the values for e.g. 2008 are actually there. You wouldn't see them with only geom_line
. Hope this helps.
You could even let stat_summary
do the aggregation for you and use df
directly.
Here is how
ggplot(df, aes(x = JudicialOrientation, y = Loss, color = Year, group = Year)) +
stat_summary(geom = "line", fun.y = mean) +
stat_summary(geom = "point", fun.y = mean)
1
This is very useful thanks. I did not know I needed a point when using categorical data to make it work properly.
– Coldchain9
Nov 20 '18 at 21:17
@Coldchain9 You don't need the points actually - you simply cannot draw a line for just one observation (that's what you brought you here, I guess).
– markus
Nov 20 '18 at 21:24
add a comment |
I suppose your saw the message after you run your code.
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
So adjusting the group aesthetic would give you this
ggplot(data = df1, aes(x = JudicialOrientation, y = MeanLoss, color=Year, group = Year)) +
geom_line() +
geom_point()
I added geom_point
so you see that the values for e.g. 2008 are actually there. You wouldn't see them with only geom_line
. Hope this helps.
You could even let stat_summary
do the aggregation for you and use df
directly.
Here is how
ggplot(df, aes(x = JudicialOrientation, y = Loss, color = Year, group = Year)) +
stat_summary(geom = "line", fun.y = mean) +
stat_summary(geom = "point", fun.y = mean)
I suppose your saw the message after you run your code.
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
So adjusting the group aesthetic would give you this
ggplot(data = df1, aes(x = JudicialOrientation, y = MeanLoss, color=Year, group = Year)) +
geom_line() +
geom_point()
I added geom_point
so you see that the values for e.g. 2008 are actually there. You wouldn't see them with only geom_line
. Hope this helps.
You could even let stat_summary
do the aggregation for you and use df
directly.
Here is how
ggplot(df, aes(x = JudicialOrientation, y = Loss, color = Year, group = Year)) +
stat_summary(geom = "line", fun.y = mean) +
stat_summary(geom = "point", fun.y = mean)
edited Nov 20 '18 at 21:20
answered Nov 20 '18 at 21:14
markusmarkus
11.7k1233
11.7k1233
1
This is very useful thanks. I did not know I needed a point when using categorical data to make it work properly.
– Coldchain9
Nov 20 '18 at 21:17
@Coldchain9 You don't need the points actually - you simply cannot draw a line for just one observation (that's what you brought you here, I guess).
– markus
Nov 20 '18 at 21:24
add a comment |
1
This is very useful thanks. I did not know I needed a point when using categorical data to make it work properly.
– Coldchain9
Nov 20 '18 at 21:17
@Coldchain9 You don't need the points actually - you simply cannot draw a line for just one observation (that's what you brought you here, I guess).
– markus
Nov 20 '18 at 21:24
1
1
This is very useful thanks. I did not know I needed a point when using categorical data to make it work properly.
– Coldchain9
Nov 20 '18 at 21:17
This is very useful thanks. I did not know I needed a point when using categorical data to make it work properly.
– Coldchain9
Nov 20 '18 at 21:17
@Coldchain9 You don't need the points actually - you simply cannot draw a line for just one observation (that's what you brought you here, I guess).
– markus
Nov 20 '18 at 21:24
@Coldchain9 You don't need the points actually - you simply cannot draw a line for just one observation (that's what you brought you here, I guess).
– markus
Nov 20 '18 at 21:24
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%2f53401326%2fplotting-multiple-lines-in-ggplot-with-different-lines-corresponding-to-a-differ%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 post the code you have tried so far?
– TeeKea
Nov 20 '18 at 20:54
I edited my post.This should give you a sample data frame you can work with. I want to aggregate the Loss column by JudicialOrientation and then plot a different line for each year in the data set to give aggregate meanLoss for each JudicialOrientation (on the x-axis preferably).
– Coldchain9
Nov 20 '18 at 21:01