Text size in geom_text

Hi, I have a question about text size using geom_text()

I manage to get the right location and font for my text, only the size does not work.
When I specify size=12, my text has the same size as when I specify size =20.

What am I doing wrong?

I get two different text sizes using the following code. Do you see the same?

library(ggplot2)
tot<-c(82/107,25/107)
ptot<-round(tot*100)
ptot


DAT <- data.frame(Type = c('vrai','faux'), Perc = ptot)

ggplot(DAT, aes(Type, Perc, fill = Type)) + geom_col(color = "black") +
  labs(x = 'Type de réponses', y = '% de réponses', title = 'Questionnaire 1: Tri global') +
  scale_fill_manual(values = c(vrai = "light blue", faux = "magenta"), guide = NULL) +
  geom_text(aes(y = 60, label = Perc)) + theme_bw() 

ggplot(DAT, aes(Type, Perc, fill = Type)) + geom_col(color = "black") +
  labs(x = 'Type de réponses', y = '% de réponses', title = 'Questionnaire 1: Tri global') +
  scale_fill_manual(values = c(vrai = "light blue", faux = "magenta"), guide = NULL) +
  geom_text(aes(y = 60, label = Perc), size = 20) + theme_bw() 

Yes, I get two different sizes as well.
I have a scatterplot and have used vjust and hjust as well in geom_text. Could that have something to do with it?

If you post some data and your code for plotting it, someone could try to replicate the problem. You can use the data.frame() function to invent some data or use the output of the dput() function applied to your data set to provide a data set others can use.

This is my code for the plot. I have tried different values for text size.
I used the featurescatter function because it comes with the package I used for data analysis, and this returns a ggplot object

FeatureScatter(object = hep_plot, feature1 = "A", 
                                  feature2 = "B", group.by = "filter",
               cols = c("#cccccc", "#de2d26"))+
  geom_hline(yintercept = 50, col= "black", linetype= "dashed")+
  geom_vline(xintercept = 800, col= "black", linetype = "dashed")+
  labs(x= "A", y="B")+
  geom_text(aes(Inf, 50, label= "50", hjust =1, vjust= -1, size= text.size, family = "sans"))+
  geom_text(aes(800, Inf, label="800", hjust=-0.5, vjust = 1, size=text.size, family= "sans"))+ 
  scale_x_log10()
hep_plot <- data.frame(A= rep(c(1:10), times=10), B=seq(from=0, to=198, by=2), filter= rep(c("yes", "no")))

It is quite big data and I do not think the data frame I made is sufficient, but here is a picture of what I have so far. It is about the 800 and 50 in the graph, I want them to be bigger

I can't get your reprex to run without throwing an error.
Can you please review that ?
You didn't specify the libraries you used. I searched Rdocumentation.org and that suggests you may be using functionality from Seurat, yet if thats so, FeatureScatter would not take a simple data.frame object as you have provided ... (I dont have prior experience with Seurat)

I am indeed using Seurat, but I do not know how to turn this into something reproducible, since the function uses my seurat object (hep_plot) which is a 500 mb dataset

However, this function returns a ggplot object so the geom_text functionalities should work right?

If its pages of code to make the seurat object I probably can't help you, but if it's a line or two you can share that and I'll attempt to advise you.

Creating the Seurat object is only 1 line of code, but the dataset I use is around 500 MB and I cannot share that

You could try arbitrarily reducing your dataset to something like 1k and then trying to make the Seurat Object out of that ?
Possibly the functions that you use to make the Seurat object have documented example objects that can be seen in their help files.

Here is an example I adapted from combining your code with the FeatureScatter documentation:


library(Seurat)
library(ggplot2)
data("pbmc_small")
text.size <- 1
FeatureScatter(object = pbmc_small, feature1 = 'CD9', feature2 = 'CD3E',
               cols = c("#cccccc", "#de2d26","#000000")) +
  geom_hline(yintercept = 50, col= "black", linetype= "dashed")+
  geom_vline(xintercept = 800, col= "black", linetype = "dashed")+
  labs(x= "A", y="B")+
  geom_text(aes(Inf, 50, label= "50", hjust =1, vjust= -1, family = "sans"), size= text.size)+
  geom_text(aes(800, Inf, label="800", hjust=-0.5, vjust = 1, family= "sans"), size=text.size)+ 
  scale_x_log10()

so if you try size 1 vs 4, you will see the labels at the extreme top right of chart be different sizes.
My main change here was in moving out , size= text.size from being an aesthetic mapping in geom_text, and rather a fixed param of the geom_text.

This worked, thank you!

This topic was automatically closed 7 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.