Having trouble with facet wrap in ggplot












2















I'm having a bit of difficulty setting up a facet wrap in ggplot with three variables of which two are points and the third is a line. There should be twelve plots (one for each month, which is the facet wrap) with locations along the x and numparts along the y. The three variables are OSM, DRP and Demand. Here is sample data (the actual data consists of 12 months of 30 locations per month). I added it as code as I can't figure out how to add Excel data to Stack Overflow.



The code I've generated so far is as follws, but it doesn't work. I also can't figure out how to get the three variables into a legend. Any assistance would be appreciated.



library(ggplot2)
parts$Location<-as.factor(parts$Location)
parts$OSM<-as.factor(parts$OSM)
parts$DRP<-as.factor(parts$DRP)
parts$Demand<-as.factor(parts$Demand)
parts$Month<-as.factor(parts$Month)
data=data.frame(parts$Location,parts$Month,parts$OSM,parts$DRP,parts$Demand)

# Faceting
ggplot(data)+geom_point(stat = "identity", aes(y=parts$OSMparts, x=parts$Location,pch=4,col="red" )) +
geom_point(stat = "identity", aes(y=parts$DRPparts, x=parts$Location,pch=16,col="blue" ))+
geom_line(stat = "identity", aes(y=parts$Demandparts, x=parts$Location,col="green" ))
facet_wrap(~parts$Month)



Location Month OSM Demand DRP
1 January 0 0 1
2 January 2 0 2
3 January 0 1 0
4 January 0 0 1
5 January 2 0 2
1 February 0 0 0
2 February 2 2 2
3 February 0 0 0
4 February 1 1 0
5 February 1 1 2
1 March 0 0 0
2 March 2 4 2
3 March 0 1 0
4 March 2 2 1
5 March 2 2 2
1 April 0 0 NA
2 April 2 2 NA
3 April 0 0 NA
4 April 1 1 NA
5 April 2 4 NA
1 May 0 0 NA
2 May 1 0 NA
3 May 0 0 NA
4 May 2 2 NA
5 May 2 0 NA
1 June 0 0 0
2 June 3 6 2
3 June 0 0 0
4 June 1 3 1
5 June 2 7 2
1 July 0 0 0
2 July 3 3 3
3 July 0 1 0
4 July 3 4 2
5 July 3 4 3
1 August 0 1 0
2 August 3 3 3
3 August 0 0 0
4 August 4 6 3
5 August 3 6 3
1 September 0 0 1
2 September 3 0 3
3 September 0 0 1
4 September 2 3 3
5 September 3 1 4
1 October 0 0 0
2 October 2 1 2
3 October 0 0 0
4 October 4 3 3
5 October 3 1 3
1 November 0 0 0
2 November 2 1 2
3 November 0 1 0
4 November 5 7 3
5 November 4 5 3
1 December 0 0 0
2 December 2 5 2
3 December 0 0 0
4 December 2 0 4
5 December 5 13 3









share|improve this question

























  • Please post code or data as text, and not as images. Also see how to make a great reproducible example.

    – Anonymous coward
    Nov 28 '18 at 22:05













  • Welcome to SO (I say that as you posted a picture of data). Pictures are neither code nor data unless the topic is image processing. Please remove the image, click on the "r" below your question, click on "info" and study how to post a proper question.

    – hrbrmstr
    Nov 28 '18 at 22:59
















2















I'm having a bit of difficulty setting up a facet wrap in ggplot with three variables of which two are points and the third is a line. There should be twelve plots (one for each month, which is the facet wrap) with locations along the x and numparts along the y. The three variables are OSM, DRP and Demand. Here is sample data (the actual data consists of 12 months of 30 locations per month). I added it as code as I can't figure out how to add Excel data to Stack Overflow.



The code I've generated so far is as follws, but it doesn't work. I also can't figure out how to get the three variables into a legend. Any assistance would be appreciated.



library(ggplot2)
parts$Location<-as.factor(parts$Location)
parts$OSM<-as.factor(parts$OSM)
parts$DRP<-as.factor(parts$DRP)
parts$Demand<-as.factor(parts$Demand)
parts$Month<-as.factor(parts$Month)
data=data.frame(parts$Location,parts$Month,parts$OSM,parts$DRP,parts$Demand)

# Faceting
ggplot(data)+geom_point(stat = "identity", aes(y=parts$OSMparts, x=parts$Location,pch=4,col="red" )) +
geom_point(stat = "identity", aes(y=parts$DRPparts, x=parts$Location,pch=16,col="blue" ))+
geom_line(stat = "identity", aes(y=parts$Demandparts, x=parts$Location,col="green" ))
facet_wrap(~parts$Month)



Location Month OSM Demand DRP
1 January 0 0 1
2 January 2 0 2
3 January 0 1 0
4 January 0 0 1
5 January 2 0 2
1 February 0 0 0
2 February 2 2 2
3 February 0 0 0
4 February 1 1 0
5 February 1 1 2
1 March 0 0 0
2 March 2 4 2
3 March 0 1 0
4 March 2 2 1
5 March 2 2 2
1 April 0 0 NA
2 April 2 2 NA
3 April 0 0 NA
4 April 1 1 NA
5 April 2 4 NA
1 May 0 0 NA
2 May 1 0 NA
3 May 0 0 NA
4 May 2 2 NA
5 May 2 0 NA
1 June 0 0 0
2 June 3 6 2
3 June 0 0 0
4 June 1 3 1
5 June 2 7 2
1 July 0 0 0
2 July 3 3 3
3 July 0 1 0
4 July 3 4 2
5 July 3 4 3
1 August 0 1 0
2 August 3 3 3
3 August 0 0 0
4 August 4 6 3
5 August 3 6 3
1 September 0 0 1
2 September 3 0 3
3 September 0 0 1
4 September 2 3 3
5 September 3 1 4
1 October 0 0 0
2 October 2 1 2
3 October 0 0 0
4 October 4 3 3
5 October 3 1 3
1 November 0 0 0
2 November 2 1 2
3 November 0 1 0
4 November 5 7 3
5 November 4 5 3
1 December 0 0 0
2 December 2 5 2
3 December 0 0 0
4 December 2 0 4
5 December 5 13 3









share|improve this question

























  • Please post code or data as text, and not as images. Also see how to make a great reproducible example.

    – Anonymous coward
    Nov 28 '18 at 22:05













  • Welcome to SO (I say that as you posted a picture of data). Pictures are neither code nor data unless the topic is image processing. Please remove the image, click on the "r" below your question, click on "info" and study how to post a proper question.

    – hrbrmstr
    Nov 28 '18 at 22:59














2












2








2








I'm having a bit of difficulty setting up a facet wrap in ggplot with three variables of which two are points and the third is a line. There should be twelve plots (one for each month, which is the facet wrap) with locations along the x and numparts along the y. The three variables are OSM, DRP and Demand. Here is sample data (the actual data consists of 12 months of 30 locations per month). I added it as code as I can't figure out how to add Excel data to Stack Overflow.



The code I've generated so far is as follws, but it doesn't work. I also can't figure out how to get the three variables into a legend. Any assistance would be appreciated.



library(ggplot2)
parts$Location<-as.factor(parts$Location)
parts$OSM<-as.factor(parts$OSM)
parts$DRP<-as.factor(parts$DRP)
parts$Demand<-as.factor(parts$Demand)
parts$Month<-as.factor(parts$Month)
data=data.frame(parts$Location,parts$Month,parts$OSM,parts$DRP,parts$Demand)

# Faceting
ggplot(data)+geom_point(stat = "identity", aes(y=parts$OSMparts, x=parts$Location,pch=4,col="red" )) +
geom_point(stat = "identity", aes(y=parts$DRPparts, x=parts$Location,pch=16,col="blue" ))+
geom_line(stat = "identity", aes(y=parts$Demandparts, x=parts$Location,col="green" ))
facet_wrap(~parts$Month)



Location Month OSM Demand DRP
1 January 0 0 1
2 January 2 0 2
3 January 0 1 0
4 January 0 0 1
5 January 2 0 2
1 February 0 0 0
2 February 2 2 2
3 February 0 0 0
4 February 1 1 0
5 February 1 1 2
1 March 0 0 0
2 March 2 4 2
3 March 0 1 0
4 March 2 2 1
5 March 2 2 2
1 April 0 0 NA
2 April 2 2 NA
3 April 0 0 NA
4 April 1 1 NA
5 April 2 4 NA
1 May 0 0 NA
2 May 1 0 NA
3 May 0 0 NA
4 May 2 2 NA
5 May 2 0 NA
1 June 0 0 0
2 June 3 6 2
3 June 0 0 0
4 June 1 3 1
5 June 2 7 2
1 July 0 0 0
2 July 3 3 3
3 July 0 1 0
4 July 3 4 2
5 July 3 4 3
1 August 0 1 0
2 August 3 3 3
3 August 0 0 0
4 August 4 6 3
5 August 3 6 3
1 September 0 0 1
2 September 3 0 3
3 September 0 0 1
4 September 2 3 3
5 September 3 1 4
1 October 0 0 0
2 October 2 1 2
3 October 0 0 0
4 October 4 3 3
5 October 3 1 3
1 November 0 0 0
2 November 2 1 2
3 November 0 1 0
4 November 5 7 3
5 November 4 5 3
1 December 0 0 0
2 December 2 5 2
3 December 0 0 0
4 December 2 0 4
5 December 5 13 3









share|improve this question
















I'm having a bit of difficulty setting up a facet wrap in ggplot with three variables of which two are points and the third is a line. There should be twelve plots (one for each month, which is the facet wrap) with locations along the x and numparts along the y. The three variables are OSM, DRP and Demand. Here is sample data (the actual data consists of 12 months of 30 locations per month). I added it as code as I can't figure out how to add Excel data to Stack Overflow.



The code I've generated so far is as follws, but it doesn't work. I also can't figure out how to get the three variables into a legend. Any assistance would be appreciated.



library(ggplot2)
parts$Location<-as.factor(parts$Location)
parts$OSM<-as.factor(parts$OSM)
parts$DRP<-as.factor(parts$DRP)
parts$Demand<-as.factor(parts$Demand)
parts$Month<-as.factor(parts$Month)
data=data.frame(parts$Location,parts$Month,parts$OSM,parts$DRP,parts$Demand)

# Faceting
ggplot(data)+geom_point(stat = "identity", aes(y=parts$OSMparts, x=parts$Location,pch=4,col="red" )) +
geom_point(stat = "identity", aes(y=parts$DRPparts, x=parts$Location,pch=16,col="blue" ))+
geom_line(stat = "identity", aes(y=parts$Demandparts, x=parts$Location,col="green" ))
facet_wrap(~parts$Month)



Location Month OSM Demand DRP
1 January 0 0 1
2 January 2 0 2
3 January 0 1 0
4 January 0 0 1
5 January 2 0 2
1 February 0 0 0
2 February 2 2 2
3 February 0 0 0
4 February 1 1 0
5 February 1 1 2
1 March 0 0 0
2 March 2 4 2
3 March 0 1 0
4 March 2 2 1
5 March 2 2 2
1 April 0 0 NA
2 April 2 2 NA
3 April 0 0 NA
4 April 1 1 NA
5 April 2 4 NA
1 May 0 0 NA
2 May 1 0 NA
3 May 0 0 NA
4 May 2 2 NA
5 May 2 0 NA
1 June 0 0 0
2 June 3 6 2
3 June 0 0 0
4 June 1 3 1
5 June 2 7 2
1 July 0 0 0
2 July 3 3 3
3 July 0 1 0
4 July 3 4 2
5 July 3 4 3
1 August 0 1 0
2 August 3 3 3
3 August 0 0 0
4 August 4 6 3
5 August 3 6 3
1 September 0 0 1
2 September 3 0 3
3 September 0 0 1
4 September 2 3 3
5 September 3 1 4
1 October 0 0 0
2 October 2 1 2
3 October 0 0 0
4 October 4 3 3
5 October 3 1 3
1 November 0 0 0
2 November 2 1 2
3 November 0 1 0
4 November 5 7 3
5 November 4 5 3
1 December 0 0 0
2 December 2 5 2
3 December 0 0 0
4 December 2 0 4
5 December 5 13 3






r ggplot2 charts facet-wrap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 23:41







Angus

















asked Nov 28 '18 at 21:25









AngusAngus

677




677













  • Please post code or data as text, and not as images. Also see how to make a great reproducible example.

    – Anonymous coward
    Nov 28 '18 at 22:05













  • Welcome to SO (I say that as you posted a picture of data). Pictures are neither code nor data unless the topic is image processing. Please remove the image, click on the "r" below your question, click on "info" and study how to post a proper question.

    – hrbrmstr
    Nov 28 '18 at 22:59



















  • Please post code or data as text, and not as images. Also see how to make a great reproducible example.

    – Anonymous coward
    Nov 28 '18 at 22:05













  • Welcome to SO (I say that as you posted a picture of data). Pictures are neither code nor data unless the topic is image processing. Please remove the image, click on the "r" below your question, click on "info" and study how to post a proper question.

    – hrbrmstr
    Nov 28 '18 at 22:59

















Please post code or data as text, and not as images. Also see how to make a great reproducible example.

– Anonymous coward
Nov 28 '18 at 22:05







Please post code or data as text, and not as images. Also see how to make a great reproducible example.

– Anonymous coward
Nov 28 '18 at 22:05















Welcome to SO (I say that as you posted a picture of data). Pictures are neither code nor data unless the topic is image processing. Please remove the image, click on the "r" below your question, click on "info" and study how to post a proper question.

– hrbrmstr
Nov 28 '18 at 22:59





Welcome to SO (I say that as you posted a picture of data). Pictures are neither code nor data unless the topic is image processing. Please remove the image, click on the "r" below your question, click on "info" and study how to post a proper question.

– hrbrmstr
Nov 28 '18 at 22:59












1 Answer
1






active

oldest

votes


















2














The solution is far more simple than you are trying to make it.



ggplot(parts) +
geom_point(aes(x=Location, y=OSM, col="blue"), pch=16)+
geom_point(aes(x=Location, y=DRP, col="red"), pch=16)+
geom_line(aes(x=Location, y=Demand, col="green"))+
facet_wrap(~factor(Month, levels = month.name))+
scale_colour_discrete(name ="Label",
labels=c("DRP", "Demand","OSM"))





share|improve this answer


























  • Thanks, but how do I get the months in the correct order?

    – Angus
    Nov 28 '18 at 23:47











  • Sorry, just add levels = month.name to the factor. See updated code.

    – Jared C
    Nov 28 '18 at 23:56












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%2f53528361%2fhaving-trouble-with-facet-wrap-in-ggplot%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














The solution is far more simple than you are trying to make it.



ggplot(parts) +
geom_point(aes(x=Location, y=OSM, col="blue"), pch=16)+
geom_point(aes(x=Location, y=DRP, col="red"), pch=16)+
geom_line(aes(x=Location, y=Demand, col="green"))+
facet_wrap(~factor(Month, levels = month.name))+
scale_colour_discrete(name ="Label",
labels=c("DRP", "Demand","OSM"))





share|improve this answer


























  • Thanks, but how do I get the months in the correct order?

    – Angus
    Nov 28 '18 at 23:47











  • Sorry, just add levels = month.name to the factor. See updated code.

    – Jared C
    Nov 28 '18 at 23:56
















2














The solution is far more simple than you are trying to make it.



ggplot(parts) +
geom_point(aes(x=Location, y=OSM, col="blue"), pch=16)+
geom_point(aes(x=Location, y=DRP, col="red"), pch=16)+
geom_line(aes(x=Location, y=Demand, col="green"))+
facet_wrap(~factor(Month, levels = month.name))+
scale_colour_discrete(name ="Label",
labels=c("DRP", "Demand","OSM"))





share|improve this answer


























  • Thanks, but how do I get the months in the correct order?

    – Angus
    Nov 28 '18 at 23:47











  • Sorry, just add levels = month.name to the factor. See updated code.

    – Jared C
    Nov 28 '18 at 23:56














2












2








2







The solution is far more simple than you are trying to make it.



ggplot(parts) +
geom_point(aes(x=Location, y=OSM, col="blue"), pch=16)+
geom_point(aes(x=Location, y=DRP, col="red"), pch=16)+
geom_line(aes(x=Location, y=Demand, col="green"))+
facet_wrap(~factor(Month, levels = month.name))+
scale_colour_discrete(name ="Label",
labels=c("DRP", "Demand","OSM"))





share|improve this answer















The solution is far more simple than you are trying to make it.



ggplot(parts) +
geom_point(aes(x=Location, y=OSM, col="blue"), pch=16)+
geom_point(aes(x=Location, y=DRP, col="red"), pch=16)+
geom_line(aes(x=Location, y=Demand, col="green"))+
facet_wrap(~factor(Month, levels = month.name))+
scale_colour_discrete(name ="Label",
labels=c("DRP", "Demand","OSM"))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '18 at 23:55

























answered Nov 28 '18 at 23:35









Jared CJared C

232314




232314













  • Thanks, but how do I get the months in the correct order?

    – Angus
    Nov 28 '18 at 23:47











  • Sorry, just add levels = month.name to the factor. See updated code.

    – Jared C
    Nov 28 '18 at 23:56



















  • Thanks, but how do I get the months in the correct order?

    – Angus
    Nov 28 '18 at 23:47











  • Sorry, just add levels = month.name to the factor. See updated code.

    – Jared C
    Nov 28 '18 at 23:56

















Thanks, but how do I get the months in the correct order?

– Angus
Nov 28 '18 at 23:47





Thanks, but how do I get the months in the correct order?

– Angus
Nov 28 '18 at 23:47













Sorry, just add levels = month.name to the factor. See updated code.

– Jared C
Nov 28 '18 at 23:56





Sorry, just add levels = month.name to the factor. See updated code.

– Jared C
Nov 28 '18 at 23:56




















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%2f53528361%2fhaving-trouble-with-facet-wrap-in-ggplot%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)