ggplot:线性刻度的x轴

时间:2015-08-06 03:51:49

标签: r plot ggplot2

我通过记录这样的值来生成图表:

## 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)

但是需要在x轴上显示线性值而不是log2。你能建议怎么做吗?

1 个答案:

答案 0 :(得分:1)

您可以使用scale_x_continuous更改标签。在这里,从图中提取断点并重新标记。

## 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