Vertical Text Alignment in R

Vertical Text Alignment in R

library(readr)
github="https://raw.githubusercontent.com/agronomy4future/raw_data_practice/main/wheat_chlorophyll_content_over_time.csv"
dataA= data.frame(read_csv(url(github), show_col_types= FALSE))

library(tidyr)
wheat= data.frame(dataA %>%
                    pivot_longer(
                      cols= c(rep1, rep2, rep3),
                      names_to= "rep",
                      values_to= "chlorophyll"))

library(dplyr)
wheat_summary= data.frame(wheat %>%
                          group_by(treatment, nitrogen, days_after_sowing) %>%
                          dplyr::summarize(across(c(chlorophyll), 
                                    .fns= list(Mean=~mean(., na.rm= TRUE), 
                                     SD= ~sd(., na.rm= TRUE), 
                                      n=~length(.),
                                     se=~sd(.,na.rm= TRUE) / sqrt(length(.))))))

##
library(ggplot2)
library(scales)

ggplot(data=wheat_summary , aes(x=days_after_sowing, y=chlorophyll_Mean, fill=treatment))+
  geom_line(linewidth=0.5, linetype="solid") +
  geom_errorbar(aes(ymin=chlorophyll_Mean-chlorophyll_se, ymax=chlorophyll_Mean+chlorophyll_se), 
                position=position_dodge(0.9), width=10) +
  geom_point(aes(shape=nitrogen),size=5) +
  scale_fill_manual(values=viridis_pal(option="D")(2),guide=guide_legend(override.aes=list(shape=21))) +
  scale_shape_manual(values=c(22,23,24), guide=guide_legend(override.aes=list(colour=1))) + 
  geom_vline(xintercept= 68, linetype="dashed", color="black") +
  geom_vline(xintercept= 75, linetype="dashed", color="red") +
  geom_vline(xintercept= 82, linetype="dashed", color="black") +
  geom_vline(xintercept= 110, linetype="dashed", color="black") +
  
  annotate("text", label=paste("Heading (50%)"),
           x=68, y=70, angle=90, hjust=0, vjust=1.5)+
  
  annotate("text", label=paste("Flowering (50%)"),
           x=75, y=70, angle=90, hjust=0, vjust=1.5, color="red")+
  
  annotate("text", label=paste("Soft dough"),
           x=82, y=70, angle=90, hjust=0, vjust=1.5)+
  
  annotate("text", label=paste("Hard dough"),
           x=110, y=70, angle=90, hjust=0, vjust=1.5)+
  
  scale_x_continuous(breaks = seq(60,130,10),limits = c(60,130)) +
  scale_y_continuous(breaks = seq(40,80,10),limits = c(40,80)) +
  labs(x="Days after sowing (Wheat)", y="Chlorophyll contents (µg/cm²)") +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position="right",
        legend.box= "vertical",
        legend.title=element_blank(),
        legend.key=element_rect(color="white", fill="white"),
        legend.text=element_text(family="serif", face="plain",
                                 size=15, color= "Black"),
        legend.background=element_rect(fill="white"),
        axis.line=element_line(linewidth=0.5, colour="black")) +
  windows(width=7, height=5)

Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.