Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot2: Bar Plot – How to graph values which are close to 0?

I want to prevent label stacking seen here:
Sample Graph

The data being graphed:

   allele_count allele frequency population 
          <int> <chr>      <dbl> <chr>      
 1         1865 G      0.894     gnomADg:oth
 2         4801 G      0.925     gnomADg:eas
 3          894 G      0.980     gnomADg:ami
 4         3867 G      0.801     gnomADg:sas
 5        10175 G      0.968     gnomADg:fin
 6          273 G      0.864     gnomADg:mid
 7           21 T      0.00138   gnomADg:amr
 8        13046 G      0.856     gnomADg:amr
 9         2901 G      0.836     gnomADg:asj
10            4 C      0.0000264 gnomADg:ALL
11           21 T      0.000138  gnomADg:ALL

The code used to generate the graph:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

graphPREPdata2[1:11, ] %>% ggplot(aes(population, frequency)) + 
  geom_col(aes(fill = allele), width = .8, alpha = .5) + 
  theme_bw(base_size = 15) + ylim(0,1) + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = paste0('Variant ID: ', names(ancesA_graphData[77])), 
       subtitle = paste0('Ancestral Allele: ', attr(graphPREPdata2, 'Ancestral_Allele'))) +
  xlab('Population Ancestry')+ylab('Allele Frequency') + 
  geom_text(aes(label = mockLabels[1:11], angle = 35), size = 4) 

In my current graph some values are too small to see, and thus confusing and visually unappealing in their current representation.

I want to understand how to visually represent values of arbitrarily small value.

>Solution :

(I’ve removed components of your plot that rely on data we don’t have available.)

Remove ylim(0,1) and add scale_y_sqrt().

graphPREPdata2[1:11, ] %>% ggplot(aes(population, frequency)) + 
  geom_col(aes(fill = allele), width = .8, alpha = .5) + 
  theme_bw(base_size = 15) + scale_y_sqrt() +                ## CHANGE
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) + 
  # labs(title = paste0('Variant ID: ', names(ancesA_graphData[77])), 
  #      subtitle = paste0('Ancestral Allele: ', attr(graphPREPdata2, 'Ancestral_Allele'))) +
  xlab('Population Ancestry')+ylab('Allele Frequency')
  # geom_text(aes(label = mockLabels[1:11], angle = 35), size = 4) 

sqrt y-axis

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading