How to add customizable legend to ggplot based on geom_text

Hi,
I would like to add legend to odds ratios ggplot like that:

geom_text(data = df2, hjust = 0, vjust = -1.1, nudge_x = -0.2, size = 5,
 aes(x = boxOdds, label = format(p_values, nsmall = 3), color = p_values > 0.05), show.legend = TRUE) 

and there is a legend made by geom_text() looking like this:
obraz

My desired output would be something like this:
obraz

My question is how can I customize such a legend in order to remove "a" and add: p-values < 0.05 in red colour, and the rest values for example in green or yellow or black colour ? How can I add a title to such a legend ?
I have got a df2 dataframe with column p_values that contains all p-values I need.
On the X-axis there are odds ratios values that belongs to variable boxOdds in my df2 dataframe.
Thank you for any ideas.

library(tidyverse)

boxLabels2 <-  c("r1", "r2", "r3", "r4")


df2 <- data.frame(yAxis = boxLabels2, boxOdds = c(1.136, 3.000, 1.724, 5.909), 
boxCILow = c(0.628, 1.216, 0.997, 2.485), boxCIHigh = c(2.056,  7.404,  2.981, 14.054),
 p_values = c(0.673, 0.014, 0.051, 0.000))


p2 <- ggplot(df2, aes(y=yAxis)) +
  geom_point(aes(x=boxOdds)) + 
  geom_segment(aes(x=boxCILow,xend=boxCIHigh,yend=yAxis)) 


p2 <- p2 + geom_vline(xintercept=1) + theme_bw()

p3 <- p2+scale_x_continuous(breaks = seq(0,10, 1)) +
  ylab("Symptoms") +
  xlab("Odds ratio") +
  ggtitle("Odd ratios (OR) with 95% COnfidence Interval")+
  ylab("Symptoms")+
  geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed", color = "red")+
 coord_trans(x = "log10") +
 geom_text(data = df2, hjust = 0, vjust = -1.1, nudge_x = -0.2, size = 5, 
aes(x = boxOdds, label = format(p_values, nsmall = 3), color = p_values > 0.05), show.legend = TRUE) 

p3

Created on 2022-01-31 by the reprex package (v2.0.1)

1 Like

Create your own discrete scale — scale_manual • ggplot2 (tidyverse.org)

Can you explain using reprex code please ?

Each of the features you suggested are covered by the examples in the link.

I have done this:

p3 +
 scale_colour_manual(values = c("red", "blue"))

which indicates progress as this way I can change the colours.

When I have done this:

p3 +
 scale_colour_manual(values = c("red", "blue"),
      scale_color_manual(values = if(df2$p_values < 0.05) c("red") 
                                else c("blue")))

it throws an error. Can you please guide me further ?

Use the labels argument.

Here is a very minimal example that should hopefully sort you out!

library(tidyverse)

# CURRENT

df = tibble(x = 1:10,
            p = seq(0.01, 0.10, 0.01))

df |> 
  ggplot(aes(y = p, x = x)) +
  geom_text(aes(label = x, color = p > 0.05))

# UPDATE

df |> 
  mutate(P = if_else(
    p <= 0.05, "P < 0.05", "P > 0.05"
  )) |> 
  ggplot(aes(y = p, x = x)) +
  scale_color_manual(values = c("red", "black")) +
  labs(color = NULL) +
  geom_text(aes(label = x, color = P), key_glyph = "rect") + # NOTE - specify key glyph
  guides(color = guide_legend(byrow = T, keyheight = unit(.1, "cm"))) # NOTE - specify byrow and key height

Thank you very much @JackDavison, I will try to adapt it to my code.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.