0

I have a script in R that is doing a print of graphics automatically in form of a loop. I try to implement into the ggplot an annotation using annotate. But it doesn't work.

Here's the code:

sp<-ggplot(data=suche,aes(x=Datum)) + 
ggtitle(paste("Temperatur und Niederschlag im Jahr ",year_s))+
geom_col(aes(y=Reg*10/30), fill="deepskyblue1")+ 
geom_bar(mapping=aes(y=Reg*10/30), stat="identity", 
fill="deepskyblue1")+
geom_text_repel(aes(y=Reg*10/3`0, label=Reg), vjust=-0.3, size=3.5)+
theme_minimal()+
geom_point(aes(y=MinT)) +
geom_line(aes(y=MinT,group=1),color="blue", size=1)+  
geom_point(aes(y=MitT)) + 
geom_line(aes(y=MitT,group=1),color="green", size=1)+  
geom_point(aes(y=MaxT)) +
geom_line(aes(y=MaxT,group=1),color="red", size=1)+
annotate("text", fontface="bold", hjust="left", size=2.5, x = 1, y = 28) 
label = paste("Temperaturmittel:", as.character(mean(tx$MitT),"°C"))
geom_text_repel(aes(y=MinT, label=MinT), direction="both", hjust=0.5, vjust=-0.5, size=3)+
geom_text_repel(aes(y=MaxT, label=MaxT), direction="both", hjust=0.5, vjust=-0.5, size=3)+
geom_text_repel(aes(y=MitT, label=MitT), direction="both", size=3)+
scale_y_continuous(
name=expression("Temperatur in °C"), limits=c(smin,smax), breaks=seq(-20,100,5),
sec.axis=sec_axis(~.*30/10, breaks=seq(0,150,10),
name="Niederschlag [mm]"))+
theme_bw(base_size = 12)+
theme(plot.title = element_text(size = 12, face = "bold"))+
theme(axis.text.y=element_text(size = 10, face="bold"))

I tried to use the annotate inside ggplot but I get the error:

Error: Invalid input: date_trans works with objects of class Date only

So the "annotate" doesn't work here. My data is a dataframe already in date format, but how to define on the x-axis that a certain annotation appears at the graphic always on the 7th month of a certain year? YYYY-07-DD

3
  • 1
    Welcome to StackOverflow! Please read the info about how to ask a good question and how to give a reproducible example. This will make it much easier for others to help you Commented Mar 24, 2023 at 18:13
  • label = ... is outside the annotate function. You also need a + following that function to include geom_text_repel and subsequent functions. Commented Mar 24, 2023 at 19:34
  • Thanks. I added the "+", but that was not the problem. How can I add the annotation in the graphic? The code is now: ''''annotate("text", fontface="bold", hjust="left", size=2.5, x = ymd(paste(as.character((year_s,"-","10","-","*" sep="")))), y = 28)+ label = paste("Temperaturmittel:", as.character(mean(tx$MitT),"°C")) '''' The year_s is defining a lot of years to be printed, the "10" means at the position 10= october month. I don't know how to use the definition on x-axis to define the start of the annotation. My data is dataframe with date column format: YYYY-MM-DD. Commented Mar 25, 2023 at 7:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.