Kevin Roth
Kevin Roth

Reputation: 93

round tick marks dynamically with ggplot2

I have the following dataframe:

rExp <- read.table(text='     samples variable value
UT            F201/203    0.00255836649706595
siCTL         F201/203    0.00911222024608249
siF201/203    F201/203    0.00473993831657716
UT            F203        0.0108815320248598
siCTL         F203        0.0224404694390467
siF201/203    F203        0.0115692755110973
UT            F203/206    0.00392331892967821
siCTL         F203/206    0.010474094476624
siF201/203    F203/206    0.00494613385979193',   header=TRUE)

I create a grouped bar plot using the following code:

p_rEx<- ggplot (data=rExp, aes(x=samples, y=value)) + scale_x_discrete(limits=samples)
p_rEx<- p_rEx + geom_bar (aes(fill = variable),stat = "identity", position = "dodge", colour='black')
p_rEx<- p_rEx + labs(y="relative expression", x='')
p_rEx<- p_rEx + guides(fill=guide_legend(title=NULL))
p_rEx<- p_rEx + scale_fill_manual(values=gray.colors(3, start = 0.3, end = 0.9, gamma = 2.2, alpha = NULL))
p_rEx

The result is the following plot:

enter image description here

My problem with this is, that the tick marks are very weird numbers. I would like them to be more "round", e.g. 0.01 instead of 0.010474094476624

The y axis needs to stay continuous though, as y values could be anywhere between 10 and 1*10^-10.

I tried something like + scale_y_continuous(breaks= pretty_breaks()), but gave me an error (Error: Discrete value supplied to continuous scale)

Is there a way to fix this? Thank you!

Edit: I do not think this is the duplicate of this question: How do I change the number of decimal places on axis labels in ggplot2?

Here, I ask for a dynamic version, that works for all y values. so a dynamic adjustment of the decimal places

Upvotes: 0

Views: 815

Answers (1)

MrFlick
MrFlick

Reputation: 206401

It looks like you've just imprted your values are a factor variable rather than numeric. Therefor the problem really is with your data import and not ggplot. But you can maybe fix it with

ggplot(data=rExp, aes(x=samples, y=as.numeric(as.character(value))))

Upvotes: 1

Related Questions