Bade
Bade

Reputation: 747

ggplot: x-axis in linear scale

I am generating a graph by taking log of the values like this:

## Example
x <- data.frame(v1=rnorm(100),v2=rnorm(100,1,1),v3=rnorm(100,0,2))
library(ggplot2);library(reshape2)
ex<- melt(x)
p1 = ggplot(ex,aes(x=log(value,2),color=variable))
p1 + geom_freqpoly(alpha=0.25)

But need to show linear values on x-axis rather then log2 ones. Could you suggest how to do it?

Upvotes: 0

Views: 1522

Answers (1)

Rorschach
Rorschach

Reputation: 32426

You can change the labels with scale_x_continuous. Here, the breaks are extracted from the plot and relabeled.

## Construct graph as you have done
p1 <- ggplot(ex, aes(x=log(value,2), color=variable)) +
  geom_freqpoly(alpha=0.5, lwd=1.1)

## Get the breaks
bs <- ggplot_build(p1)[[2]]$ranges[[1]]$x.major_source

## add new labels
p1 + scale_x_continuous(breaks=bs, labels=round(2**bs, 2)) 

enter image description here

Upvotes: 1

Related Questions