Adding dynamic chart titles in ggplot2Tidy evaluation programming and ggplot2Creating multiple graphs based upon the column namesSide-by-side plots with ggplot2Rotating and spacing axis labels in ggplot2Deserialize JSON into C# dynamic object?How to set limits for axes in ggplot2 R plots?Order Bars in ggplot2 bar graphChange size of axes title and labels in ggplot2ggplot: line plot for discrete x-axisDifferent colors in ggplot2 using groupsSubscript a title in a Graph (ggplot2) with label of another fileCenter Plot title in ggplot2
How would a order of Monks that renounce their names communicate effectively?
Did Chinese school textbook maps (c. 1951) "depict China as stretching even into the central Asian republics"?
Set vertical spacing between two particular items
Professor Roman gives unusual math quiz ahead of
How fast can a ship with rotating habitats be accelerated?
AT system without -5v
Dold-Kan correspondence in the category of symmetric spectra
Is adding a new player (or players) a DM decision, or a group decision?
Why does this fireplace work?
If my Scout rogue has used his full movement on his turn, can he later use the reaction from the Skirmisher feature to move again?
What do you call the action of someone tackling a stronger person?
The use of "I" and "we" used in the same sentence and other questions
What happens when your group is victim of a surprise attack but you can't be surprised?
Are Finite Automata Turing Complete?
If a high rpm motor is run at lower rpm, will it produce more torque?
How should I behave to assure my friends that I am not after their money?
Inverse-quotes-quine
If protons are the only stable baryons, why do they decay into neutrons in positron emission?
Zombie Diet, why humans
I played my first (rapid) tournament recently and I wanted to calculate my ELO
Generate and Graph the Recamán Sequence
Sir Alex Ferguson advice OR Sir Alex Ferguson's advice
Children's short story about material that accelerates away from gravity
Symbol for "not absolutely continuous" in Latex
Adding dynamic chart titles in ggplot2
Tidy evaluation programming and ggplot2Creating multiple graphs based upon the column namesSide-by-side plots with ggplot2Rotating and spacing axis labels in ggplot2Deserialize JSON into C# dynamic object?How to set limits for axes in ggplot2 R plots?Order Bars in ggplot2 bar graphChange size of axes title and labels in ggplot2ggplot: line plot for discrete x-axisDifferent colors in ggplot2 using groupsSubscript a title in a Graph (ggplot2) with label of another fileCenter Plot title in ggplot2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This follows on from my last question. I've spent an hour or so trying to work out how to pass the variable I use to filter my dataframe to the title of the graph that is generated.
Following on from my previous questions.
library (tidyverse)
library (epitools)
# here's my made up data
DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")
DATA = data.frame(DISEASE, YEAR, VALUE,AREA)
DATA<-
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")
I thought that this
x_label = "Area!!!"
y_label = "Rate!!!"
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DATA$DISEASE)
Why doesn't it?
It generates a chart for Marco Polio but uses Chicky Pox as the title.
What I want is (false code)
ggtitle == filter(disease)
Because what I'm going to do after this is walk and purr to get every chart for every infection and I'd like to title automatically.
Ta.
EDIT:
I've tried the suggestion below and it doesn't quite work.
I've tried this
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(paste(DISEASE))
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(as.character(DISEASE))
and no luck.
Does it have something to do with DISEASE becoming a FACTOR when it gets grouped?
r ggplot2 dynamic iteration
add a comment |
This follows on from my last question. I've spent an hour or so trying to work out how to pass the variable I use to filter my dataframe to the title of the graph that is generated.
Following on from my previous questions.
library (tidyverse)
library (epitools)
# here's my made up data
DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")
DATA = data.frame(DISEASE, YEAR, VALUE,AREA)
DATA<-
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")
I thought that this
x_label = "Area!!!"
y_label = "Rate!!!"
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DATA$DISEASE)
Why doesn't it?
It generates a chart for Marco Polio but uses Chicky Pox as the title.
What I want is (false code)
ggtitle == filter(disease)
Because what I'm going to do after this is walk and purr to get every chart for every infection and I'd like to title automatically.
Ta.
EDIT:
I've tried the suggestion below and it doesn't quite work.
I've tried this
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(paste(DISEASE))
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(as.character(DISEASE))
and no luck.
Does it have something to do with DISEASE becoming a FACTOR when it gets grouped?
r ggplot2 dynamic iteration
add a comment |
This follows on from my last question. I've spent an hour or so trying to work out how to pass the variable I use to filter my dataframe to the title of the graph that is generated.
Following on from my previous questions.
library (tidyverse)
library (epitools)
# here's my made up data
DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")
DATA = data.frame(DISEASE, YEAR, VALUE,AREA)
DATA<-
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")
I thought that this
x_label = "Area!!!"
y_label = "Rate!!!"
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DATA$DISEASE)
Why doesn't it?
It generates a chart for Marco Polio but uses Chicky Pox as the title.
What I want is (false code)
ggtitle == filter(disease)
Because what I'm going to do after this is walk and purr to get every chart for every infection and I'd like to title automatically.
Ta.
EDIT:
I've tried the suggestion below and it doesn't quite work.
I've tried this
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(paste(DISEASE))
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(as.character(DISEASE))
and no luck.
Does it have something to do with DISEASE becoming a FACTOR when it gets grouped?
r ggplot2 dynamic iteration
This follows on from my last question. I've spent an hour or so trying to work out how to pass the variable I use to filter my dataframe to the title of the graph that is generated.
Following on from my previous questions.
library (tidyverse)
library (epitools)
# here's my made up data
DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
"Mumps","Mumps","Mumps","Mumps","Mumps",
"Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015,
2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
79,91,69,89,78,
71,69,95,61,87)
AREA =c("A", "B","C")
DATA = data.frame(DISEASE, YEAR, VALUE,AREA)
DATA<-
DATA %>%
mutate(POPN = case_when(
AREA == "A" ~ 2.5,
AREA == "B" ~ 3,
AREA == "C" ~ 7,
TRUE ~ 0)) %>%
group_by(DISEASE,AREA,POPN) %>%
count(AREA) %>%
mutate(res = list(pois.byar(n, POPN))) %>%
unnest()
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")
I thought that this
x_label = "Area!!!"
y_label = "Rate!!!"
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DATA$DISEASE)
Why doesn't it?
It generates a chart for Marco Polio but uses Chicky Pox as the title.
What I want is (false code)
ggtitle == filter(disease)
Because what I'm going to do after this is walk and purr to get every chart for every infection and I'd like to title automatically.
Ta.
EDIT:
I've tried the suggestion below and it doesn't quite work.
I've tried this
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(paste(DISEASE))
DATA%>%filter(DISEASE== "Mumps")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
ggtitle(as.character(DISEASE))
and no luck.
Does it have something to do with DISEASE becoming a FACTOR when it gets grouped?
r ggplot2 dynamic iteration
r ggplot2 dynamic iteration
edited Mar 25 at 16:02
damo
asked Mar 25 at 11:39
damodamo
748 bronze badges
748 bronze badges
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
It seems like you want a function where you can input a disease and have the plot created.
disease_plot <- function(disease_of_interest)
DATA %>%
filter(DISEASE == disease_of_interest) %>%
ggplot(aes(x = AREA, y = rate)) +
geom_point() +
geom_hline(aes(yintercept = rate[AREA == "A"]),
linetype = "dashed", color = "red") +
# labs(x = x_label, y = y_label) +
ggtitle(disease_of_interest)
disease_plot("Marco Polio")
disease_plot("Chicky Pox")
disease_plot("Mumps")
Or to have them all created at once...
map(unique(DATA$DISEASE), disease_plot)
Clearly, I need to start looking at how to write functions... I think it's the way of thinking about problems. I appreciate your help and advice here Stephen and Mark.I'm going to show the function I already had and then include the ggtitle portion. I wouldn't have thought to include the "title" in the function, but I also didn't know how to do it. Hope this is ok? And makes sense?
– damo
Mar 25 at 20:38
1
Following on from @Tung : I rechecked the answer given here Stephen. It does work. And I think I know why: before running this bit: labs(x = x_label,y = y_label) I should have defined what they were. When it didn't work, and I saw I could use the function I already had I put it together in my answer. I'll mark yours as the answer! Apologies.
– damo
Mar 26 at 16:13
add a comment |
In the end, I took the advice and help from both Stephen and Mark and cobbled it together with my original plan to walk and purr my way through it.
Here it is:
walk(unique(DATA$DISEASE), function(disease_of_interest)
p <- DATA%>%filter(DISEASE== !!disease_of_interest)%>%
ggplot(aes(x=AREA, y=rate,y=rate,
ymin = rate-lower, ymax = rate+upper))+
geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label),+
ggtitle(paste0("Number of ",disease_of_interest,
" in 2018"))+
geom_errorbar(aes(ymin=lower, ymax=upper), width=.1)
print(p)
ggsave(paste("drive path",disease_of_interest, "plot.png"))+
scale_x_discrete(limits=c("C","A","B"))
)
Why did you use!!inside your function?
– Tung
Mar 26 at 6:41
Short answer: it's in the function I have inherited to do this.
– damo
Mar 26 at 9:14
Long answer : In dplyr (and in tidyeval in general) you use !! to say that you want to unquote an input so that it’s evaluated, not quoted. This gives us a function that actually does what we want. dplyr.tidyverse.org/articles/programming.html . So I guess it makes sure the input on the filter is correct.
– damo
Mar 26 at 9:15
You don't need!!for the function to work.!!is only needed when you quote your variable usingquo,enquo,sym,ensym, etc.
– Tung
Mar 26 at 16:00
1
Thanks Tung, I've explained why I hadn't marked the previous answer as the answer. I appreciate it because, I don't think it's fair that Stephen wouldn't have received any credit. Your suggestions look interesting and thanks for the explanation about !!
– damo
Mar 26 at 16:15
|
show 4 more comments
That is because you use the entire DATA$DISEASE as title, and it seems it just grabs the last value in that column. Much simpler is to make a filtered dataframe first, and then feed that into the plot I think.
df <- DATA%>%filter(DISEASE== "Marco Polio")
ggplot(data = df, aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(df$DISEASE)
In the end I though the solution would be not to call DATA$ but merely DISEASE
However, this doesn't seem to work as expected when filtering for another Disease. I think you would have to subset DISEASE also inside the ggtitle, or better use the first function, or the other answer posted by the other user.
NOT WORKING AS EXPECTED:
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DISEASE)
Frustratingly, it works on my trial data (used here). But when I test it on my actual data it doesn't. And I get the following error: Error in rlang::list2(..., title = title, subtitle = subtitle, caption = caption, : object 'DISEASE' not found
– damo
Mar 25 at 12:17
I would suspect a typo or str(df) shows a different format for some columns that differ from the test data and cause the problem? I can not help you with that based on a comment I fear. Please acccept the working answer for the test data though
– Mark
Mar 25 at 12:21
I also have that thought. Give me a couple of minutes....
– damo
Mar 25 at 12:22
Can I ask where you looked?
– damo
Mar 25 at 12:33
Oh I meant looking as in reading your code, figuring out what we did wrong and changing it till it works.
– Mark
Mar 25 at 12:55
|
show 3 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%2f55336983%2fadding-dynamic-chart-titles-in-ggplot2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It seems like you want a function where you can input a disease and have the plot created.
disease_plot <- function(disease_of_interest)
DATA %>%
filter(DISEASE == disease_of_interest) %>%
ggplot(aes(x = AREA, y = rate)) +
geom_point() +
geom_hline(aes(yintercept = rate[AREA == "A"]),
linetype = "dashed", color = "red") +
# labs(x = x_label, y = y_label) +
ggtitle(disease_of_interest)
disease_plot("Marco Polio")
disease_plot("Chicky Pox")
disease_plot("Mumps")
Or to have them all created at once...
map(unique(DATA$DISEASE), disease_plot)
Clearly, I need to start looking at how to write functions... I think it's the way of thinking about problems. I appreciate your help and advice here Stephen and Mark.I'm going to show the function I already had and then include the ggtitle portion. I wouldn't have thought to include the "title" in the function, but I also didn't know how to do it. Hope this is ok? And makes sense?
– damo
Mar 25 at 20:38
1
Following on from @Tung : I rechecked the answer given here Stephen. It does work. And I think I know why: before running this bit: labs(x = x_label,y = y_label) I should have defined what they were. When it didn't work, and I saw I could use the function I already had I put it together in my answer. I'll mark yours as the answer! Apologies.
– damo
Mar 26 at 16:13
add a comment |
It seems like you want a function where you can input a disease and have the plot created.
disease_plot <- function(disease_of_interest)
DATA %>%
filter(DISEASE == disease_of_interest) %>%
ggplot(aes(x = AREA, y = rate)) +
geom_point() +
geom_hline(aes(yintercept = rate[AREA == "A"]),
linetype = "dashed", color = "red") +
# labs(x = x_label, y = y_label) +
ggtitle(disease_of_interest)
disease_plot("Marco Polio")
disease_plot("Chicky Pox")
disease_plot("Mumps")
Or to have them all created at once...
map(unique(DATA$DISEASE), disease_plot)
Clearly, I need to start looking at how to write functions... I think it's the way of thinking about problems. I appreciate your help and advice here Stephen and Mark.I'm going to show the function I already had and then include the ggtitle portion. I wouldn't have thought to include the "title" in the function, but I also didn't know how to do it. Hope this is ok? And makes sense?
– damo
Mar 25 at 20:38
1
Following on from @Tung : I rechecked the answer given here Stephen. It does work. And I think I know why: before running this bit: labs(x = x_label,y = y_label) I should have defined what they were. When it didn't work, and I saw I could use the function I already had I put it together in my answer. I'll mark yours as the answer! Apologies.
– damo
Mar 26 at 16:13
add a comment |
It seems like you want a function where you can input a disease and have the plot created.
disease_plot <- function(disease_of_interest)
DATA %>%
filter(DISEASE == disease_of_interest) %>%
ggplot(aes(x = AREA, y = rate)) +
geom_point() +
geom_hline(aes(yintercept = rate[AREA == "A"]),
linetype = "dashed", color = "red") +
# labs(x = x_label, y = y_label) +
ggtitle(disease_of_interest)
disease_plot("Marco Polio")
disease_plot("Chicky Pox")
disease_plot("Mumps")
Or to have them all created at once...
map(unique(DATA$DISEASE), disease_plot)
It seems like you want a function where you can input a disease and have the plot created.
disease_plot <- function(disease_of_interest)
DATA %>%
filter(DISEASE == disease_of_interest) %>%
ggplot(aes(x = AREA, y = rate)) +
geom_point() +
geom_hline(aes(yintercept = rate[AREA == "A"]),
linetype = "dashed", color = "red") +
# labs(x = x_label, y = y_label) +
ggtitle(disease_of_interest)
disease_plot("Marco Polio")
disease_plot("Chicky Pox")
disease_plot("Mumps")
Or to have them all created at once...
map(unique(DATA$DISEASE), disease_plot)
edited Mar 26 at 23:05
Tung
11.5k2 gold badges42 silver badges51 bronze badges
11.5k2 gold badges42 silver badges51 bronze badges
answered Mar 25 at 17:27
StephenKStephenK
3461 silver badge6 bronze badges
3461 silver badge6 bronze badges
Clearly, I need to start looking at how to write functions... I think it's the way of thinking about problems. I appreciate your help and advice here Stephen and Mark.I'm going to show the function I already had and then include the ggtitle portion. I wouldn't have thought to include the "title" in the function, but I also didn't know how to do it. Hope this is ok? And makes sense?
– damo
Mar 25 at 20:38
1
Following on from @Tung : I rechecked the answer given here Stephen. It does work. And I think I know why: before running this bit: labs(x = x_label,y = y_label) I should have defined what they were. When it didn't work, and I saw I could use the function I already had I put it together in my answer. I'll mark yours as the answer! Apologies.
– damo
Mar 26 at 16:13
add a comment |
Clearly, I need to start looking at how to write functions... I think it's the way of thinking about problems. I appreciate your help and advice here Stephen and Mark.I'm going to show the function I already had and then include the ggtitle portion. I wouldn't have thought to include the "title" in the function, but I also didn't know how to do it. Hope this is ok? And makes sense?
– damo
Mar 25 at 20:38
1
Following on from @Tung : I rechecked the answer given here Stephen. It does work. And I think I know why: before running this bit: labs(x = x_label,y = y_label) I should have defined what they were. When it didn't work, and I saw I could use the function I already had I put it together in my answer. I'll mark yours as the answer! Apologies.
– damo
Mar 26 at 16:13
Clearly, I need to start looking at how to write functions... I think it's the way of thinking about problems. I appreciate your help and advice here Stephen and Mark.I'm going to show the function I already had and then include the ggtitle portion. I wouldn't have thought to include the "title" in the function, but I also didn't know how to do it. Hope this is ok? And makes sense?
– damo
Mar 25 at 20:38
Clearly, I need to start looking at how to write functions... I think it's the way of thinking about problems. I appreciate your help and advice here Stephen and Mark.I'm going to show the function I already had and then include the ggtitle portion. I wouldn't have thought to include the "title" in the function, but I also didn't know how to do it. Hope this is ok? And makes sense?
– damo
Mar 25 at 20:38
1
1
Following on from @Tung : I rechecked the answer given here Stephen. It does work. And I think I know why: before running this bit: labs(x = x_label,y = y_label) I should have defined what they were. When it didn't work, and I saw I could use the function I already had I put it together in my answer. I'll mark yours as the answer! Apologies.
– damo
Mar 26 at 16:13
Following on from @Tung : I rechecked the answer given here Stephen. It does work. And I think I know why: before running this bit: labs(x = x_label,y = y_label) I should have defined what they were. When it didn't work, and I saw I could use the function I already had I put it together in my answer. I'll mark yours as the answer! Apologies.
– damo
Mar 26 at 16:13
add a comment |
In the end, I took the advice and help from both Stephen and Mark and cobbled it together with my original plan to walk and purr my way through it.
Here it is:
walk(unique(DATA$DISEASE), function(disease_of_interest)
p <- DATA%>%filter(DISEASE== !!disease_of_interest)%>%
ggplot(aes(x=AREA, y=rate,y=rate,
ymin = rate-lower, ymax = rate+upper))+
geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label),+
ggtitle(paste0("Number of ",disease_of_interest,
" in 2018"))+
geom_errorbar(aes(ymin=lower, ymax=upper), width=.1)
print(p)
ggsave(paste("drive path",disease_of_interest, "plot.png"))+
scale_x_discrete(limits=c("C","A","B"))
)
Why did you use!!inside your function?
– Tung
Mar 26 at 6:41
Short answer: it's in the function I have inherited to do this.
– damo
Mar 26 at 9:14
Long answer : In dplyr (and in tidyeval in general) you use !! to say that you want to unquote an input so that it’s evaluated, not quoted. This gives us a function that actually does what we want. dplyr.tidyverse.org/articles/programming.html . So I guess it makes sure the input on the filter is correct.
– damo
Mar 26 at 9:15
You don't need!!for the function to work.!!is only needed when you quote your variable usingquo,enquo,sym,ensym, etc.
– Tung
Mar 26 at 16:00
1
Thanks Tung, I've explained why I hadn't marked the previous answer as the answer. I appreciate it because, I don't think it's fair that Stephen wouldn't have received any credit. Your suggestions look interesting and thanks for the explanation about !!
– damo
Mar 26 at 16:15
|
show 4 more comments
In the end, I took the advice and help from both Stephen and Mark and cobbled it together with my original plan to walk and purr my way through it.
Here it is:
walk(unique(DATA$DISEASE), function(disease_of_interest)
p <- DATA%>%filter(DISEASE== !!disease_of_interest)%>%
ggplot(aes(x=AREA, y=rate,y=rate,
ymin = rate-lower, ymax = rate+upper))+
geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label),+
ggtitle(paste0("Number of ",disease_of_interest,
" in 2018"))+
geom_errorbar(aes(ymin=lower, ymax=upper), width=.1)
print(p)
ggsave(paste("drive path",disease_of_interest, "plot.png"))+
scale_x_discrete(limits=c("C","A","B"))
)
Why did you use!!inside your function?
– Tung
Mar 26 at 6:41
Short answer: it's in the function I have inherited to do this.
– damo
Mar 26 at 9:14
Long answer : In dplyr (and in tidyeval in general) you use !! to say that you want to unquote an input so that it’s evaluated, not quoted. This gives us a function that actually does what we want. dplyr.tidyverse.org/articles/programming.html . So I guess it makes sure the input on the filter is correct.
– damo
Mar 26 at 9:15
You don't need!!for the function to work.!!is only needed when you quote your variable usingquo,enquo,sym,ensym, etc.
– Tung
Mar 26 at 16:00
1
Thanks Tung, I've explained why I hadn't marked the previous answer as the answer. I appreciate it because, I don't think it's fair that Stephen wouldn't have received any credit. Your suggestions look interesting and thanks for the explanation about !!
– damo
Mar 26 at 16:15
|
show 4 more comments
In the end, I took the advice and help from both Stephen and Mark and cobbled it together with my original plan to walk and purr my way through it.
Here it is:
walk(unique(DATA$DISEASE), function(disease_of_interest)
p <- DATA%>%filter(DISEASE== !!disease_of_interest)%>%
ggplot(aes(x=AREA, y=rate,y=rate,
ymin = rate-lower, ymax = rate+upper))+
geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label),+
ggtitle(paste0("Number of ",disease_of_interest,
" in 2018"))+
geom_errorbar(aes(ymin=lower, ymax=upper), width=.1)
print(p)
ggsave(paste("drive path",disease_of_interest, "plot.png"))+
scale_x_discrete(limits=c("C","A","B"))
)
In the end, I took the advice and help from both Stephen and Mark and cobbled it together with my original plan to walk and purr my way through it.
Here it is:
walk(unique(DATA$DISEASE), function(disease_of_interest)
p <- DATA%>%filter(DISEASE== !!disease_of_interest)%>%
ggplot(aes(x=AREA, y=rate,y=rate,
ymin = rate-lower, ymax = rate+upper))+
geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label),+
ggtitle(paste0("Number of ",disease_of_interest,
" in 2018"))+
geom_errorbar(aes(ymin=lower, ymax=upper), width=.1)
print(p)
ggsave(paste("drive path",disease_of_interest, "plot.png"))+
scale_x_discrete(limits=c("C","A","B"))
)
edited Mar 26 at 11:59
answered Mar 25 at 20:41
damodamo
748 bronze badges
748 bronze badges
Why did you use!!inside your function?
– Tung
Mar 26 at 6:41
Short answer: it's in the function I have inherited to do this.
– damo
Mar 26 at 9:14
Long answer : In dplyr (and in tidyeval in general) you use !! to say that you want to unquote an input so that it’s evaluated, not quoted. This gives us a function that actually does what we want. dplyr.tidyverse.org/articles/programming.html . So I guess it makes sure the input on the filter is correct.
– damo
Mar 26 at 9:15
You don't need!!for the function to work.!!is only needed when you quote your variable usingquo,enquo,sym,ensym, etc.
– Tung
Mar 26 at 16:00
1
Thanks Tung, I've explained why I hadn't marked the previous answer as the answer. I appreciate it because, I don't think it's fair that Stephen wouldn't have received any credit. Your suggestions look interesting and thanks for the explanation about !!
– damo
Mar 26 at 16:15
|
show 4 more comments
Why did you use!!inside your function?
– Tung
Mar 26 at 6:41
Short answer: it's in the function I have inherited to do this.
– damo
Mar 26 at 9:14
Long answer : In dplyr (and in tidyeval in general) you use !! to say that you want to unquote an input so that it’s evaluated, not quoted. This gives us a function that actually does what we want. dplyr.tidyverse.org/articles/programming.html . So I guess it makes sure the input on the filter is correct.
– damo
Mar 26 at 9:15
You don't need!!for the function to work.!!is only needed when you quote your variable usingquo,enquo,sym,ensym, etc.
– Tung
Mar 26 at 16:00
1
Thanks Tung, I've explained why I hadn't marked the previous answer as the answer. I appreciate it because, I don't think it's fair that Stephen wouldn't have received any credit. Your suggestions look interesting and thanks for the explanation about !!
– damo
Mar 26 at 16:15
Why did you use
!! inside your function?– Tung
Mar 26 at 6:41
Why did you use
!! inside your function?– Tung
Mar 26 at 6:41
Short answer: it's in the function I have inherited to do this.
– damo
Mar 26 at 9:14
Short answer: it's in the function I have inherited to do this.
– damo
Mar 26 at 9:14
Long answer : In dplyr (and in tidyeval in general) you use !! to say that you want to unquote an input so that it’s evaluated, not quoted. This gives us a function that actually does what we want. dplyr.tidyverse.org/articles/programming.html . So I guess it makes sure the input on the filter is correct.
– damo
Mar 26 at 9:15
Long answer : In dplyr (and in tidyeval in general) you use !! to say that you want to unquote an input so that it’s evaluated, not quoted. This gives us a function that actually does what we want. dplyr.tidyverse.org/articles/programming.html . So I guess it makes sure the input on the filter is correct.
– damo
Mar 26 at 9:15
You don't need
!! for the function to work. !! is only needed when you quote your variable using quo, enquo, sym, ensym, etc.– Tung
Mar 26 at 16:00
You don't need
!! for the function to work. !! is only needed when you quote your variable using quo, enquo, sym, ensym, etc.– Tung
Mar 26 at 16:00
1
1
Thanks Tung, I've explained why I hadn't marked the previous answer as the answer. I appreciate it because, I don't think it's fair that Stephen wouldn't have received any credit. Your suggestions look interesting and thanks for the explanation about !!
– damo
Mar 26 at 16:15
Thanks Tung, I've explained why I hadn't marked the previous answer as the answer. I appreciate it because, I don't think it's fair that Stephen wouldn't have received any credit. Your suggestions look interesting and thanks for the explanation about !!
– damo
Mar 26 at 16:15
|
show 4 more comments
That is because you use the entire DATA$DISEASE as title, and it seems it just grabs the last value in that column. Much simpler is to make a filtered dataframe first, and then feed that into the plot I think.
df <- DATA%>%filter(DISEASE== "Marco Polio")
ggplot(data = df, aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(df$DISEASE)
In the end I though the solution would be not to call DATA$ but merely DISEASE
However, this doesn't seem to work as expected when filtering for another Disease. I think you would have to subset DISEASE also inside the ggtitle, or better use the first function, or the other answer posted by the other user.
NOT WORKING AS EXPECTED:
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DISEASE)
Frustratingly, it works on my trial data (used here). But when I test it on my actual data it doesn't. And I get the following error: Error in rlang::list2(..., title = title, subtitle = subtitle, caption = caption, : object 'DISEASE' not found
– damo
Mar 25 at 12:17
I would suspect a typo or str(df) shows a different format for some columns that differ from the test data and cause the problem? I can not help you with that based on a comment I fear. Please acccept the working answer for the test data though
– Mark
Mar 25 at 12:21
I also have that thought. Give me a couple of minutes....
– damo
Mar 25 at 12:22
Can I ask where you looked?
– damo
Mar 25 at 12:33
Oh I meant looking as in reading your code, figuring out what we did wrong and changing it till it works.
– Mark
Mar 25 at 12:55
|
show 3 more comments
That is because you use the entire DATA$DISEASE as title, and it seems it just grabs the last value in that column. Much simpler is to make a filtered dataframe first, and then feed that into the plot I think.
df <- DATA%>%filter(DISEASE== "Marco Polio")
ggplot(data = df, aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(df$DISEASE)
In the end I though the solution would be not to call DATA$ but merely DISEASE
However, this doesn't seem to work as expected when filtering for another Disease. I think you would have to subset DISEASE also inside the ggtitle, or better use the first function, or the other answer posted by the other user.
NOT WORKING AS EXPECTED:
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DISEASE)
Frustratingly, it works on my trial data (used here). But when I test it on my actual data it doesn't. And I get the following error: Error in rlang::list2(..., title = title, subtitle = subtitle, caption = caption, : object 'DISEASE' not found
– damo
Mar 25 at 12:17
I would suspect a typo or str(df) shows a different format for some columns that differ from the test data and cause the problem? I can not help you with that based on a comment I fear. Please acccept the working answer for the test data though
– Mark
Mar 25 at 12:21
I also have that thought. Give me a couple of minutes....
– damo
Mar 25 at 12:22
Can I ask where you looked?
– damo
Mar 25 at 12:33
Oh I meant looking as in reading your code, figuring out what we did wrong and changing it till it works.
– Mark
Mar 25 at 12:55
|
show 3 more comments
That is because you use the entire DATA$DISEASE as title, and it seems it just grabs the last value in that column. Much simpler is to make a filtered dataframe first, and then feed that into the plot I think.
df <- DATA%>%filter(DISEASE== "Marco Polio")
ggplot(data = df, aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(df$DISEASE)
In the end I though the solution would be not to call DATA$ but merely DISEASE
However, this doesn't seem to work as expected when filtering for another Disease. I think you would have to subset DISEASE also inside the ggtitle, or better use the first function, or the other answer posted by the other user.
NOT WORKING AS EXPECTED:
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DISEASE)
That is because you use the entire DATA$DISEASE as title, and it seems it just grabs the last value in that column. Much simpler is to make a filtered dataframe first, and then feed that into the plot I think.
df <- DATA%>%filter(DISEASE== "Marco Polio")
ggplot(data = df, aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(df$DISEASE)
In the end I though the solution would be not to call DATA$ but merely DISEASE
However, this doesn't seem to work as expected when filtering for another Disease. I think you would have to subset DISEASE also inside the ggtitle, or better use the first function, or the other answer posted by the other user.
NOT WORKING AS EXPECTED:
DATA%>%filter(DISEASE== "Marco Polio")%>%
ggplot(aes(x=AREA, y=rate)) +geom_point() +
geom_hline(aes(yintercept=rate[AREA == "A"]),
linetype="dashed", color = "red")+
labs(x = x_label,y = y_label)+
ggtitle(DISEASE)
edited Mar 26 at 21:08
answered Mar 25 at 12:02
MarkMark
1,0608 silver badges28 bronze badges
1,0608 silver badges28 bronze badges
Frustratingly, it works on my trial data (used here). But when I test it on my actual data it doesn't. And I get the following error: Error in rlang::list2(..., title = title, subtitle = subtitle, caption = caption, : object 'DISEASE' not found
– damo
Mar 25 at 12:17
I would suspect a typo or str(df) shows a different format for some columns that differ from the test data and cause the problem? I can not help you with that based on a comment I fear. Please acccept the working answer for the test data though
– Mark
Mar 25 at 12:21
I also have that thought. Give me a couple of minutes....
– damo
Mar 25 at 12:22
Can I ask where you looked?
– damo
Mar 25 at 12:33
Oh I meant looking as in reading your code, figuring out what we did wrong and changing it till it works.
– Mark
Mar 25 at 12:55
|
show 3 more comments
Frustratingly, it works on my trial data (used here). But when I test it on my actual data it doesn't. And I get the following error: Error in rlang::list2(..., title = title, subtitle = subtitle, caption = caption, : object 'DISEASE' not found
– damo
Mar 25 at 12:17
I would suspect a typo or str(df) shows a different format for some columns that differ from the test data and cause the problem? I can not help you with that based on a comment I fear. Please acccept the working answer for the test data though
– Mark
Mar 25 at 12:21
I also have that thought. Give me a couple of minutes....
– damo
Mar 25 at 12:22
Can I ask where you looked?
– damo
Mar 25 at 12:33
Oh I meant looking as in reading your code, figuring out what we did wrong and changing it till it works.
– Mark
Mar 25 at 12:55
Frustratingly, it works on my trial data (used here). But when I test it on my actual data it doesn't. And I get the following error: Error in rlang::list2(..., title = title, subtitle = subtitle, caption = caption, : object 'DISEASE' not found
– damo
Mar 25 at 12:17
Frustratingly, it works on my trial data (used here). But when I test it on my actual data it doesn't. And I get the following error: Error in rlang::list2(..., title = title, subtitle = subtitle, caption = caption, : object 'DISEASE' not found
– damo
Mar 25 at 12:17
I would suspect a typo or str(df) shows a different format for some columns that differ from the test data and cause the problem? I can not help you with that based on a comment I fear. Please acccept the working answer for the test data though
– Mark
Mar 25 at 12:21
I would suspect a typo or str(df) shows a different format for some columns that differ from the test data and cause the problem? I can not help you with that based on a comment I fear. Please acccept the working answer for the test data though
– Mark
Mar 25 at 12:21
I also have that thought. Give me a couple of minutes....
– damo
Mar 25 at 12:22
I also have that thought. Give me a couple of minutes....
– damo
Mar 25 at 12:22
Can I ask where you looked?
– damo
Mar 25 at 12:33
Can I ask where you looked?
– damo
Mar 25 at 12:33
Oh I meant looking as in reading your code, figuring out what we did wrong and changing it till it works.
– Mark
Mar 25 at 12:55
Oh I meant looking as in reading your code, figuring out what we did wrong and changing it till it works.
– Mark
Mar 25 at 12:55
|
show 3 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.
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%2f55336983%2fadding-dynamic-chart-titles-in-ggplot2%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