I'm new to data scinece and I need any help, please.
I need to put labels from column (integers) on to the graphed line on the plot at certain x points, like x=20, then, x=50, then x=80 or at least every 20-30 steps. I am using geam_text, but it puts the labels on every point that it draws, and I need only on some certain points, so it is readable.
the code is:
ggp<-cut_offs %>%
ggplot(mapping=aes(x=IL6, y=RhoTSHT3_in_less))+
geom_line(color="blue")+
geom_point(col=ifelse(cut_offs$pvalTSHT3_in_less<0.05, "red", "black"))+
ylim(0.1,0.7)+
geom_text(aes(label=n_more))
So, I guess I need to change my last line of code to something like this:
geom_text(aes(label=ifelse(x in labels, cut_offs$n_more, "")))
where labels is a list with point where I wanna put labels.
currently, my graph looks like this, which is unreadable:
I tried this
geom_text(aes(label=ifelse(x in labels, cut_offs$n_more, "")))
and of course it's not working, how do I write it in R?
We don't have your actual data to demonstrate an answer, but I have constructed a very similar set with the same names, range and approximate shape as your own (see footnote).
Using this, we see that your code produces much the same set of problems:
library(tidyverse)
cut_offs %>%
ggplot(aes(IL6, RhoTSHT3_in_less)) +
geom_line(color = "blue")+
geom_point(col = ifelse(cut_offs$pvalTSHT3_in_less < 0.05, "red", "black"))+
ylim(0.1, 0.7) +
geom_text(aes(label = n_more))
To label, say, only every 25th measurement along the x axis, we can do:
cut_offs %>%
ggplot(aes(IL6, RhoTSHT3_in_less)) +
geom_line(color = "blue")+
geom_point(col = ifelse(cut_offs$pvalTSHT3_in_less < 0.05, "red", "black"))+
ylim(0.1, 0.7) +
geom_text(data = . %>% filter(row_number() %% 25 == 1), aes(label = n_more),
nudge_y = 0.05)
Footnote - data used
set.seed(1)
cut_offs <- data.frame(IL6 = seq(0, 500, len = 251),
RhoTSHT3_in_less = c(seq(0.45, 0.22, len = 20) +
rnorm(20, 0, 0.02),
runif(231, .2, .25)),
n_more = sample(300, 251),
pvalTSHT3_in_less = runif(251, 0, 0.2))