0
votes

So first of all, I'm entirely new to programming and R (one week in), so apologies in advance.

How would I format the y axis in the following fashion with ggplot2?:

  1. The number of intervals I want. (eg, 10, visually equidistant intervals)
  2. Logarithmic scale
  3. Exponents instead of scientific (I want 10¹,10²,10³ instead of 1e+01,1e+02,1e+03)

I can find the answers to some of these individual issues, but they don't work in conjunction.

Here's my graph. I don't know if this helps.

ggplot(dfm,aes(Strain,value))+ geom_bar(aes(fill=variable),stat="identity",position="dodge")

Bottom line is: Currently the y axis is: 1e+02,1e+05,1e+08 I want it to be: 10¹,10²,10³,10⁴,10⁵,10⁶,10⁷,10⁸,10⁹,10¹⁰

1
it would be more helpful if you used a builtin dataframe, or provided code to create a minimally reproducible oneyeedle
I honestly don't know how. I'm trying to google it at the moment,myflow
Have a look here - library(ggplot2);library(scales);df <- data.frame(x=1:100,y=10^(1:100));ggplot(df, aes(x,y)) + geom_point() + scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x, n = 10), labels = trans_format("log10", math_format(10^.x))) for example should get you close. Also read the help ?scales::trans_breaks etc. to see how to tune all this.lukeA

1 Answers

0
votes

Here are some guidance (I'm using tidyr::population dataset)

1.

library(ggplot2)
library(scales)
library(tidyr)
ggplot(population, aes(country, population)) + 
  geom_bar(aes(fill=year),stat="identity",position="dodge") +
  scale_y_continuous(breaks = pretty_breaks(n = 10))

2.

library(ggplot2)
library(scales)
library(tidyr)
ggplot(population, aes(country, population)) + 
  geom_bar(aes(fill=year),stat="identity",position="dodge") +
  scale_y_log10()

putting it all together:


library(ggplot2)
library(scales)
library(tidyr)
ggplot(population, aes(country, population)) + 
  geom_bar(aes(fill=year),stat="identity",position="dodge") +
  scale_y_log10(labels= trans_format(log10, math_format(10^.x)), 
                breaks =trans_breaks(log10, function(x) 10^x, 10))