Creating loop for plotting in ggplot2 leads to error
this has been posted before in a similar style, but I have adjusted a few things now and hopefully (!) things are clear now
I've tried to create a loop in order to create plots for several countries.
My dataframe: Plot_df
Here an excerpt of the data:
year country iso2 sector emissions
1990 Belgium BE ETS 0
1990 Belgium BE Regulated 78614107
1990 Belgium BE Unregulated 41870292
1991 Belgium BE ETS 0
1991 Belgium BE Regulated 79811521
1991 Belgium BE Unregulated 43733190
...
2011 Belgium BE ETS 46203056
2011 Belgium BE Regulated 61319344
2011 Belgium BE Unregulated 42839297
2012 Belgium BE ETS 43006980
2012 Belgium BE Regulated 58934979
2012 Belgium BE Unregulated 42459997
2013 Belgium BE ETS 45231176
2013 Belgium BE Regulated 58383554
2013 Belgium BE Unregulated 43586891
2014 Belgium BE ETS 43853144
2014 Belgium BE Regulated 56010346
2014 Belgium BE Unregulated 40380694
2015 Belgium BE ETS 44713916
2015 Belgium BE Regulated 57375031
2015 Belgium BE Unregulated 42854461
2016 Belgium BE ETS 43655728
2016 Belgium BE Regulated 56702848
2016 Belgium BE Unregulated 43540863
dput(head(Plot_df, 15))
delivers this
structure(list(year = c("1990", "1990", "1990", "1990", "1990",
"1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990",
"1990", "1990"), country = c("Austria", "Austria", "Austria",
"Belgium", "Belgium", "Belgium", "Bulgaria", "Bulgaria", "Bulgaria",
"Croatia", "Croatia", "Croatia", "Cyprus", "Cyprus", "Cyprus"
), iso2 = c("AT", "AT", "AT", "BE", "BE", "BE", "BG", "BG", "BG",
"HR", "HR", "HR", "CY", "CY", "CY"), sector = c("ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated"), emissions = c(0, 38264402.6689529, 24027827.7997971,
0, 78614106.9221497, 41870291.5153503, 0, 69103153.6445618,
9569791.66793823,
0, 17530229.1374207, 5911735.70632935, 0, 3135556.17528036, 1507499.48878214
)), row.names = c("378", "2836", "3100", "813", "8310", "8410",
"558", "16410", "16510", "438", "24510", "24610", "783", "3261",
"3271"), class = "data.frame")
I have only shown the whole data for one country, since every country looks the same (apart from different figures Plot_df$emissions
)
What I want to do is (most you will see from my code below):
- create loop, so plots will be created for all countries
- x axis = year; y=emissions (of a country as in
Plot_df$country
) - 3 groups (curves) should be from `Plot_df$sector: Regulated, Unregulated, ETS
Regulated and unregulated should be between 1990:2016; ETS between 2005:2017.
(these years are the years with data availability)
This is what I tried:
# Sets up the loop to run from i=1 through a list of countries from vector
`Plot_df$country`
for(i in (1:length(unique(Plot_df$country)))){
# Color settings: colorblind-friendly palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2",
"#D55E00", "#CC79A7")
# Plotting code where DATA, YEAR, etc need to be handed the right vectors
p <- ggplot() +
geom_line(Plot_df,aes(x=year,y=emissions,group=sector),
color=cols[1]) +
labs(x="Year",y="CO2 emissions",z="",title=paste("Emissions for",
country[i])) +
xlim(1990, 2016) +
ylim(-50,50) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm"))
p
# Save plot, where the file name automatically gets a country name suffix
ggsave(p,filename=paste("./FILENAME",country[i],".png",sep=""),width=6.5,
height=6)
}
I'm getting this error and I can't figure out why
Error: `data` must be a data frame, or other object coercible by
`fortify()`, not an S3 object with class uneval
Did you accidentally pass `aes()` to the `data` argument?
Any idea why this is happening?
Thanks in any case
r loops ggplot2
add a comment |
this has been posted before in a similar style, but I have adjusted a few things now and hopefully (!) things are clear now
I've tried to create a loop in order to create plots for several countries.
My dataframe: Plot_df
Here an excerpt of the data:
year country iso2 sector emissions
1990 Belgium BE ETS 0
1990 Belgium BE Regulated 78614107
1990 Belgium BE Unregulated 41870292
1991 Belgium BE ETS 0
1991 Belgium BE Regulated 79811521
1991 Belgium BE Unregulated 43733190
...
2011 Belgium BE ETS 46203056
2011 Belgium BE Regulated 61319344
2011 Belgium BE Unregulated 42839297
2012 Belgium BE ETS 43006980
2012 Belgium BE Regulated 58934979
2012 Belgium BE Unregulated 42459997
2013 Belgium BE ETS 45231176
2013 Belgium BE Regulated 58383554
2013 Belgium BE Unregulated 43586891
2014 Belgium BE ETS 43853144
2014 Belgium BE Regulated 56010346
2014 Belgium BE Unregulated 40380694
2015 Belgium BE ETS 44713916
2015 Belgium BE Regulated 57375031
2015 Belgium BE Unregulated 42854461
2016 Belgium BE ETS 43655728
2016 Belgium BE Regulated 56702848
2016 Belgium BE Unregulated 43540863
dput(head(Plot_df, 15))
delivers this
structure(list(year = c("1990", "1990", "1990", "1990", "1990",
"1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990",
"1990", "1990"), country = c("Austria", "Austria", "Austria",
"Belgium", "Belgium", "Belgium", "Bulgaria", "Bulgaria", "Bulgaria",
"Croatia", "Croatia", "Croatia", "Cyprus", "Cyprus", "Cyprus"
), iso2 = c("AT", "AT", "AT", "BE", "BE", "BE", "BG", "BG", "BG",
"HR", "HR", "HR", "CY", "CY", "CY"), sector = c("ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated"), emissions = c(0, 38264402.6689529, 24027827.7997971,
0, 78614106.9221497, 41870291.5153503, 0, 69103153.6445618,
9569791.66793823,
0, 17530229.1374207, 5911735.70632935, 0, 3135556.17528036, 1507499.48878214
)), row.names = c("378", "2836", "3100", "813", "8310", "8410",
"558", "16410", "16510", "438", "24510", "24610", "783", "3261",
"3271"), class = "data.frame")
I have only shown the whole data for one country, since every country looks the same (apart from different figures Plot_df$emissions
)
What I want to do is (most you will see from my code below):
- create loop, so plots will be created for all countries
- x axis = year; y=emissions (of a country as in
Plot_df$country
) - 3 groups (curves) should be from `Plot_df$sector: Regulated, Unregulated, ETS
Regulated and unregulated should be between 1990:2016; ETS between 2005:2017.
(these years are the years with data availability)
This is what I tried:
# Sets up the loop to run from i=1 through a list of countries from vector
`Plot_df$country`
for(i in (1:length(unique(Plot_df$country)))){
# Color settings: colorblind-friendly palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2",
"#D55E00", "#CC79A7")
# Plotting code where DATA, YEAR, etc need to be handed the right vectors
p <- ggplot() +
geom_line(Plot_df,aes(x=year,y=emissions,group=sector),
color=cols[1]) +
labs(x="Year",y="CO2 emissions",z="",title=paste("Emissions for",
country[i])) +
xlim(1990, 2016) +
ylim(-50,50) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm"))
p
# Save plot, where the file name automatically gets a country name suffix
ggsave(p,filename=paste("./FILENAME",country[i],".png",sep=""),width=6.5,
height=6)
}
I'm getting this error and I can't figure out why
Error: `data` must be a data frame, or other object coercible by
`fortify()`, not an S3 object with class uneval
Did you accidentally pass `aes()` to the `data` argument?
Any idea why this is happening?
Thanks in any case
r loops ggplot2
geom_line(data=Plot_df
– hrbrmstr
Nov 10 at 14:10
@Nordsee, I've answered this in the other topic.
– arg0naut
Nov 10 at 14:38
1
@arg0naut SO is a code sweat shop, after all (:
– Roman
Nov 10 at 20:52
@hrbrmstr Now I'm getting a different error ->Error: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
The issue was thatPlot_df$year
was not set as numeric! Thank you for your help
– Nordsee
Nov 14 at 18:10
add a comment |
this has been posted before in a similar style, but I have adjusted a few things now and hopefully (!) things are clear now
I've tried to create a loop in order to create plots for several countries.
My dataframe: Plot_df
Here an excerpt of the data:
year country iso2 sector emissions
1990 Belgium BE ETS 0
1990 Belgium BE Regulated 78614107
1990 Belgium BE Unregulated 41870292
1991 Belgium BE ETS 0
1991 Belgium BE Regulated 79811521
1991 Belgium BE Unregulated 43733190
...
2011 Belgium BE ETS 46203056
2011 Belgium BE Regulated 61319344
2011 Belgium BE Unregulated 42839297
2012 Belgium BE ETS 43006980
2012 Belgium BE Regulated 58934979
2012 Belgium BE Unregulated 42459997
2013 Belgium BE ETS 45231176
2013 Belgium BE Regulated 58383554
2013 Belgium BE Unregulated 43586891
2014 Belgium BE ETS 43853144
2014 Belgium BE Regulated 56010346
2014 Belgium BE Unregulated 40380694
2015 Belgium BE ETS 44713916
2015 Belgium BE Regulated 57375031
2015 Belgium BE Unregulated 42854461
2016 Belgium BE ETS 43655728
2016 Belgium BE Regulated 56702848
2016 Belgium BE Unregulated 43540863
dput(head(Plot_df, 15))
delivers this
structure(list(year = c("1990", "1990", "1990", "1990", "1990",
"1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990",
"1990", "1990"), country = c("Austria", "Austria", "Austria",
"Belgium", "Belgium", "Belgium", "Bulgaria", "Bulgaria", "Bulgaria",
"Croatia", "Croatia", "Croatia", "Cyprus", "Cyprus", "Cyprus"
), iso2 = c("AT", "AT", "AT", "BE", "BE", "BE", "BG", "BG", "BG",
"HR", "HR", "HR", "CY", "CY", "CY"), sector = c("ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated"), emissions = c(0, 38264402.6689529, 24027827.7997971,
0, 78614106.9221497, 41870291.5153503, 0, 69103153.6445618,
9569791.66793823,
0, 17530229.1374207, 5911735.70632935, 0, 3135556.17528036, 1507499.48878214
)), row.names = c("378", "2836", "3100", "813", "8310", "8410",
"558", "16410", "16510", "438", "24510", "24610", "783", "3261",
"3271"), class = "data.frame")
I have only shown the whole data for one country, since every country looks the same (apart from different figures Plot_df$emissions
)
What I want to do is (most you will see from my code below):
- create loop, so plots will be created for all countries
- x axis = year; y=emissions (of a country as in
Plot_df$country
) - 3 groups (curves) should be from `Plot_df$sector: Regulated, Unregulated, ETS
Regulated and unregulated should be between 1990:2016; ETS between 2005:2017.
(these years are the years with data availability)
This is what I tried:
# Sets up the loop to run from i=1 through a list of countries from vector
`Plot_df$country`
for(i in (1:length(unique(Plot_df$country)))){
# Color settings: colorblind-friendly palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2",
"#D55E00", "#CC79A7")
# Plotting code where DATA, YEAR, etc need to be handed the right vectors
p <- ggplot() +
geom_line(Plot_df,aes(x=year,y=emissions,group=sector),
color=cols[1]) +
labs(x="Year",y="CO2 emissions",z="",title=paste("Emissions for",
country[i])) +
xlim(1990, 2016) +
ylim(-50,50) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm"))
p
# Save plot, where the file name automatically gets a country name suffix
ggsave(p,filename=paste("./FILENAME",country[i],".png",sep=""),width=6.5,
height=6)
}
I'm getting this error and I can't figure out why
Error: `data` must be a data frame, or other object coercible by
`fortify()`, not an S3 object with class uneval
Did you accidentally pass `aes()` to the `data` argument?
Any idea why this is happening?
Thanks in any case
r loops ggplot2
this has been posted before in a similar style, but I have adjusted a few things now and hopefully (!) things are clear now
I've tried to create a loop in order to create plots for several countries.
My dataframe: Plot_df
Here an excerpt of the data:
year country iso2 sector emissions
1990 Belgium BE ETS 0
1990 Belgium BE Regulated 78614107
1990 Belgium BE Unregulated 41870292
1991 Belgium BE ETS 0
1991 Belgium BE Regulated 79811521
1991 Belgium BE Unregulated 43733190
...
2011 Belgium BE ETS 46203056
2011 Belgium BE Regulated 61319344
2011 Belgium BE Unregulated 42839297
2012 Belgium BE ETS 43006980
2012 Belgium BE Regulated 58934979
2012 Belgium BE Unregulated 42459997
2013 Belgium BE ETS 45231176
2013 Belgium BE Regulated 58383554
2013 Belgium BE Unregulated 43586891
2014 Belgium BE ETS 43853144
2014 Belgium BE Regulated 56010346
2014 Belgium BE Unregulated 40380694
2015 Belgium BE ETS 44713916
2015 Belgium BE Regulated 57375031
2015 Belgium BE Unregulated 42854461
2016 Belgium BE ETS 43655728
2016 Belgium BE Regulated 56702848
2016 Belgium BE Unregulated 43540863
dput(head(Plot_df, 15))
delivers this
structure(list(year = c("1990", "1990", "1990", "1990", "1990",
"1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990",
"1990", "1990"), country = c("Austria", "Austria", "Austria",
"Belgium", "Belgium", "Belgium", "Bulgaria", "Bulgaria", "Bulgaria",
"Croatia", "Croatia", "Croatia", "Cyprus", "Cyprus", "Cyprus"
), iso2 = c("AT", "AT", "AT", "BE", "BE", "BE", "BG", "BG", "BG",
"HR", "HR", "HR", "CY", "CY", "CY"), sector = c("ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated"), emissions = c(0, 38264402.6689529, 24027827.7997971,
0, 78614106.9221497, 41870291.5153503, 0, 69103153.6445618,
9569791.66793823,
0, 17530229.1374207, 5911735.70632935, 0, 3135556.17528036, 1507499.48878214
)), row.names = c("378", "2836", "3100", "813", "8310", "8410",
"558", "16410", "16510", "438", "24510", "24610", "783", "3261",
"3271"), class = "data.frame")
I have only shown the whole data for one country, since every country looks the same (apart from different figures Plot_df$emissions
)
What I want to do is (most you will see from my code below):
- create loop, so plots will be created for all countries
- x axis = year; y=emissions (of a country as in
Plot_df$country
) - 3 groups (curves) should be from `Plot_df$sector: Regulated, Unregulated, ETS
Regulated and unregulated should be between 1990:2016; ETS between 2005:2017.
(these years are the years with data availability)
This is what I tried:
# Sets up the loop to run from i=1 through a list of countries from vector
`Plot_df$country`
for(i in (1:length(unique(Plot_df$country)))){
# Color settings: colorblind-friendly palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2",
"#D55E00", "#CC79A7")
# Plotting code where DATA, YEAR, etc need to be handed the right vectors
p <- ggplot() +
geom_line(Plot_df,aes(x=year,y=emissions,group=sector),
color=cols[1]) +
labs(x="Year",y="CO2 emissions",z="",title=paste("Emissions for",
country[i])) +
xlim(1990, 2016) +
ylim(-50,50) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm"))
p
# Save plot, where the file name automatically gets a country name suffix
ggsave(p,filename=paste("./FILENAME",country[i],".png",sep=""),width=6.5,
height=6)
}
I'm getting this error and I can't figure out why
Error: `data` must be a data frame, or other object coercible by
`fortify()`, not an S3 object with class uneval
Did you accidentally pass `aes()` to the `data` argument?
Any idea why this is happening?
Thanks in any case
r loops ggplot2
r loops ggplot2
edited Nov 12 at 19:48
asked Nov 10 at 13:57
Nordsee
465
465
geom_line(data=Plot_df
– hrbrmstr
Nov 10 at 14:10
@Nordsee, I've answered this in the other topic.
– arg0naut
Nov 10 at 14:38
1
@arg0naut SO is a code sweat shop, after all (:
– Roman
Nov 10 at 20:52
@hrbrmstr Now I'm getting a different error ->Error: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
The issue was thatPlot_df$year
was not set as numeric! Thank you for your help
– Nordsee
Nov 14 at 18:10
add a comment |
geom_line(data=Plot_df
– hrbrmstr
Nov 10 at 14:10
@Nordsee, I've answered this in the other topic.
– arg0naut
Nov 10 at 14:38
1
@arg0naut SO is a code sweat shop, after all (:
– Roman
Nov 10 at 20:52
@hrbrmstr Now I'm getting a different error ->Error: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
The issue was thatPlot_df$year
was not set as numeric! Thank you for your help
– Nordsee
Nov 14 at 18:10
geom_line(data=Plot_df
– hrbrmstr
Nov 10 at 14:10
geom_line(data=Plot_df
– hrbrmstr
Nov 10 at 14:10
@Nordsee, I've answered this in the other topic.
– arg0naut
Nov 10 at 14:38
@Nordsee, I've answered this in the other topic.
– arg0naut
Nov 10 at 14:38
1
1
@arg0naut SO is a code sweat shop, after all (:
– Roman
Nov 10 at 20:52
@arg0naut SO is a code sweat shop, after all (:
– Roman
Nov 10 at 20:52
@hrbrmstr Now I'm getting a different error ->
Error: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
@hrbrmstr Now I'm getting a different error ->
Error: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
The issue was that
Plot_df$year
was not set as numeric! Thank you for your help– Nordsee
Nov 14 at 18:10
The issue was that
Plot_df$year
was not set as numeric! Thank you for your help– Nordsee
Nov 14 at 18:10
add a comment |
1 Answer
1
active
oldest
votes
FWIW a teensy bit of code styling goes a long way.
for(country in unique(Plot_df$country)) {
# YOU NEVER *REALLY* USE THIS VECTOR JUST ONE ELEMENT FROM IT
# Color settings: colorblind-friendly palette
c(
"#999999", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7"
) -> cols
# carve out the data for the plot
country_df <- Plot_df[Plot_df$country == country,]
# Plotting code where DATA, YEAR, etc need to be handed the right vectors
ggplot() +
geom_line(
data = country_df, # THIS IS WHAT YOU FORGOT
aes(year, emissions, group = sector),
color = cols[1] # WHY [1] IF YOU DEFINED A VECTOR
) +
xlim(1990, 2016) + # SHOULD LIKELY USE scale_x_… and set limits there + expand=c(0,0) insteasd
ylim(-50, 50) + # SAME
labs(
x = "Year", y = "CO2 emissions",
title = sprintf("Emissions for %s", country)
) +
theme(plot.margin = margin(.5, .5, .5, .5, "cm")) -> p # THERE IS A margin() function
print(p) # it won't print without print()
# Save plot, where the file name automatically gets a country name suffix
ggsave(
plot = p,
filename = sprintf("./FILENAME-%s.png", country), # I PREFER sprintf
width = 6.5,
height = 6
)
}
Thank you for this, but same hereError: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
It seems to be something about the color scales, but I'm not entirely clear on it yet
– Nordsee
Nov 12 at 19:19
str(Plot_df)
returns number forPlot_df$emissions
– Nordsee
Nov 12 at 19:30
Having actual data would be helpful (dput()
) is your bff.
– hrbrmstr
Nov 12 at 19:35
I have added 15 entries from dput, I hope this is enough
– Nordsee
Nov 12 at 19:48
|
show 2 more comments
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%2f53239683%2fcreating-loop-for-plotting-in-ggplot2-leads-to-error%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
FWIW a teensy bit of code styling goes a long way.
for(country in unique(Plot_df$country)) {
# YOU NEVER *REALLY* USE THIS VECTOR JUST ONE ELEMENT FROM IT
# Color settings: colorblind-friendly palette
c(
"#999999", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7"
) -> cols
# carve out the data for the plot
country_df <- Plot_df[Plot_df$country == country,]
# Plotting code where DATA, YEAR, etc need to be handed the right vectors
ggplot() +
geom_line(
data = country_df, # THIS IS WHAT YOU FORGOT
aes(year, emissions, group = sector),
color = cols[1] # WHY [1] IF YOU DEFINED A VECTOR
) +
xlim(1990, 2016) + # SHOULD LIKELY USE scale_x_… and set limits there + expand=c(0,0) insteasd
ylim(-50, 50) + # SAME
labs(
x = "Year", y = "CO2 emissions",
title = sprintf("Emissions for %s", country)
) +
theme(plot.margin = margin(.5, .5, .5, .5, "cm")) -> p # THERE IS A margin() function
print(p) # it won't print without print()
# Save plot, where the file name automatically gets a country name suffix
ggsave(
plot = p,
filename = sprintf("./FILENAME-%s.png", country), # I PREFER sprintf
width = 6.5,
height = 6
)
}
Thank you for this, but same hereError: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
It seems to be something about the color scales, but I'm not entirely clear on it yet
– Nordsee
Nov 12 at 19:19
str(Plot_df)
returns number forPlot_df$emissions
– Nordsee
Nov 12 at 19:30
Having actual data would be helpful (dput()
) is your bff.
– hrbrmstr
Nov 12 at 19:35
I have added 15 entries from dput, I hope this is enough
– Nordsee
Nov 12 at 19:48
|
show 2 more comments
FWIW a teensy bit of code styling goes a long way.
for(country in unique(Plot_df$country)) {
# YOU NEVER *REALLY* USE THIS VECTOR JUST ONE ELEMENT FROM IT
# Color settings: colorblind-friendly palette
c(
"#999999", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7"
) -> cols
# carve out the data for the plot
country_df <- Plot_df[Plot_df$country == country,]
# Plotting code where DATA, YEAR, etc need to be handed the right vectors
ggplot() +
geom_line(
data = country_df, # THIS IS WHAT YOU FORGOT
aes(year, emissions, group = sector),
color = cols[1] # WHY [1] IF YOU DEFINED A VECTOR
) +
xlim(1990, 2016) + # SHOULD LIKELY USE scale_x_… and set limits there + expand=c(0,0) insteasd
ylim(-50, 50) + # SAME
labs(
x = "Year", y = "CO2 emissions",
title = sprintf("Emissions for %s", country)
) +
theme(plot.margin = margin(.5, .5, .5, .5, "cm")) -> p # THERE IS A margin() function
print(p) # it won't print without print()
# Save plot, where the file name automatically gets a country name suffix
ggsave(
plot = p,
filename = sprintf("./FILENAME-%s.png", country), # I PREFER sprintf
width = 6.5,
height = 6
)
}
Thank you for this, but same hereError: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
It seems to be something about the color scales, but I'm not entirely clear on it yet
– Nordsee
Nov 12 at 19:19
str(Plot_df)
returns number forPlot_df$emissions
– Nordsee
Nov 12 at 19:30
Having actual data would be helpful (dput()
) is your bff.
– hrbrmstr
Nov 12 at 19:35
I have added 15 entries from dput, I hope this is enough
– Nordsee
Nov 12 at 19:48
|
show 2 more comments
FWIW a teensy bit of code styling goes a long way.
for(country in unique(Plot_df$country)) {
# YOU NEVER *REALLY* USE THIS VECTOR JUST ONE ELEMENT FROM IT
# Color settings: colorblind-friendly palette
c(
"#999999", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7"
) -> cols
# carve out the data for the plot
country_df <- Plot_df[Plot_df$country == country,]
# Plotting code where DATA, YEAR, etc need to be handed the right vectors
ggplot() +
geom_line(
data = country_df, # THIS IS WHAT YOU FORGOT
aes(year, emissions, group = sector),
color = cols[1] # WHY [1] IF YOU DEFINED A VECTOR
) +
xlim(1990, 2016) + # SHOULD LIKELY USE scale_x_… and set limits there + expand=c(0,0) insteasd
ylim(-50, 50) + # SAME
labs(
x = "Year", y = "CO2 emissions",
title = sprintf("Emissions for %s", country)
) +
theme(plot.margin = margin(.5, .5, .5, .5, "cm")) -> p # THERE IS A margin() function
print(p) # it won't print without print()
# Save plot, where the file name automatically gets a country name suffix
ggsave(
plot = p,
filename = sprintf("./FILENAME-%s.png", country), # I PREFER sprintf
width = 6.5,
height = 6
)
}
FWIW a teensy bit of code styling goes a long way.
for(country in unique(Plot_df$country)) {
# YOU NEVER *REALLY* USE THIS VECTOR JUST ONE ELEMENT FROM IT
# Color settings: colorblind-friendly palette
c(
"#999999", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7"
) -> cols
# carve out the data for the plot
country_df <- Plot_df[Plot_df$country == country,]
# Plotting code where DATA, YEAR, etc need to be handed the right vectors
ggplot() +
geom_line(
data = country_df, # THIS IS WHAT YOU FORGOT
aes(year, emissions, group = sector),
color = cols[1] # WHY [1] IF YOU DEFINED A VECTOR
) +
xlim(1990, 2016) + # SHOULD LIKELY USE scale_x_… and set limits there + expand=c(0,0) insteasd
ylim(-50, 50) + # SAME
labs(
x = "Year", y = "CO2 emissions",
title = sprintf("Emissions for %s", country)
) +
theme(plot.margin = margin(.5, .5, .5, .5, "cm")) -> p # THERE IS A margin() function
print(p) # it won't print without print()
# Save plot, where the file name automatically gets a country name suffix
ggsave(
plot = p,
filename = sprintf("./FILENAME-%s.png", country), # I PREFER sprintf
width = 6.5,
height = 6
)
}
answered Nov 10 at 14:23
hrbrmstr
60k685146
60k685146
Thank you for this, but same hereError: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
It seems to be something about the color scales, but I'm not entirely clear on it yet
– Nordsee
Nov 12 at 19:19
str(Plot_df)
returns number forPlot_df$emissions
– Nordsee
Nov 12 at 19:30
Having actual data would be helpful (dput()
) is your bff.
– hrbrmstr
Nov 12 at 19:35
I have added 15 entries from dput, I hope this is enough
– Nordsee
Nov 12 at 19:48
|
show 2 more comments
Thank you for this, but same hereError: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
It seems to be something about the color scales, but I'm not entirely clear on it yet
– Nordsee
Nov 12 at 19:19
str(Plot_df)
returns number forPlot_df$emissions
– Nordsee
Nov 12 at 19:30
Having actual data would be helpful (dput()
) is your bff.
– hrbrmstr
Nov 12 at 19:35
I have added 15 entries from dput, I hope this is enough
– Nordsee
Nov 12 at 19:48
Thank you for this, but same here
Error: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
Thank you for this, but same here
Error: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
It seems to be something about the color scales, but I'm not entirely clear on it yet
– Nordsee
Nov 12 at 19:19
It seems to be something about the color scales, but I'm not entirely clear on it yet
– Nordsee
Nov 12 at 19:19
str(Plot_df)
returns number for Plot_df$emissions
– Nordsee
Nov 12 at 19:30
str(Plot_df)
returns number for Plot_df$emissions
– Nordsee
Nov 12 at 19:30
Having actual data would be helpful (
dput()
) is your bff.– hrbrmstr
Nov 12 at 19:35
Having actual data would be helpful (
dput()
) is your bff.– hrbrmstr
Nov 12 at 19:35
I have added 15 entries from dput, I hope this is enough
– Nordsee
Nov 12 at 19:48
I have added 15 entries from dput, I hope this is enough
– Nordsee
Nov 12 at 19:48
|
show 2 more comments
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%2f53239683%2fcreating-loop-for-plotting-in-ggplot2-leads-to-error%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
geom_line(data=Plot_df
– hrbrmstr
Nov 10 at 14:10
@Nordsee, I've answered this in the other topic.
– arg0naut
Nov 10 at 14:38
1
@arg0naut SO is a code sweat shop, after all (:
– Roman
Nov 10 at 20:52
@hrbrmstr Now I'm getting a different error ->
Error: Discrete value supplied to continuous scale
– Nordsee
Nov 12 at 19:11
The issue was that
Plot_df$year
was not set as numeric! Thank you for your help– Nordsee
Nov 14 at 18:10