1

I have a df like this:

Name <- c('A','A','A','A','A','A','A','A','A','A')
Measurement <- c('Length','Length','Length','Length','Length','Length','Length','Length','Length','Length')
Meas_Avg <- c(0.125,0.150,0.122,0.177,0.145,0.135,0.180,0.133,0.174,0.149)
Tool <- c('Tool1','Tool3','Tool2','Tool3','Tool3','Tool1','Tool1','Tool2','Tool2','Tool1')

df <- data.frame(Name,Measurement,Meas_Avg,Tool)
pvalue <- 0.056

and I have a text "pvalue" that I manually position inside a ggplot while plotting. While this seems good for one plot, but if I were to use it for the rest of the plots, this way of doing it is not right since the values in the plot change and the boxplots move all over the plotting area. Here is my ggplot function

ggplot(data = df, aes(x=Tool, y=Meas_Avg)) + 
  theme(axis.text=element_text(size=14)
        , axis.title=element_text(size=14,face="bold")) +
  geom_boxplot(outlier.colour = "red", outlier.size=3, outlier.shape=15, fill="gray95", colour="blue") + 
  geom_point(alpha=0.6, position=position_jitter(w=0.05, h=0.0), aes(colour=Tool), size=5) + 
  facet_grid(Measurement~Name,scales = "free", space = "free") +
  scale_color_manual(values = c("red", "magenta", "dodgerblue2","green","blue","orange","black","violet")) +
  scale_size_area() +
  geom_text(data = pval_TAP_5, aes(label=paste("pvalue= ",pvalue), x=2.0, y=0.25),size=7) +
  theme(strip.text.x = element_text(size = 15, colour = "red",face="bold")
        ,strip.text.y = element_text(size = 15, colour = "red",face="bold"))

This obtains

enter image description here

I would like to plot the text "pvalue = 0.056" on top of the plot (here in this case on top of "A" rather than manually doing it inside. How can this be achieved?

1 Answer 1

1

You could try renaming the Name column as below:

df$Name <- paste0(df$Name, "\n pvalue = ", pvalue)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Wow that was cool. Thank you so much. I will use this logic in my large dataset.

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.