显示上限和下限y轴

时间:2016-02-23 07:53:34

标签: r ggplot2

我无法得到y的下限和上限。

library(ggplot2)
ggplot(data,aes(x=n,y=value,color=variable)) + geom_line()+
  labs(color="Legend")+
  scale_x_continuous("x",expand=c(0,0),
                     breaks=c(1,2,5,10,30,60))+
  scale_y_continuous("y",expand=c(0,0),
                      breaks=round(seq(0,max(data[,3]),by=0.1),1))

数据(稍后会有更多变量):

n variable     value
1        1 0.2339010
2        1 0.2625115
5        1 0.2781600
10        1 0.2776770
30        1 0.3344481
60        1 0.4810225
  1. 我希望y的上限是y的最高值(0.4810225)四舍五入到最高的第二个小数(0.5)。如何在图上显示“0.5”作为最大y值? (很像60是最大x值。)

  2. 如何在轴的开头显示y = 0.0?

  3. enter image description here

3 个答案:

答案 0 :(得分:0)

last_plot() + 
 scale_y_continuous(lim = c(0, 0.5), expand=c(0,0)) 

如果您想要固定值,或

last_plot() + 
 scale_y_continuous(breaks =  function(x, ...) 
       sort(unique(c(range(x), pretty(x, ...)))), 
     expand=c(0,0)) 

如果您希望ggplot自动查找数据的最小值和最大值。

答案 1 :(得分:0)

设置限制并舍入最大值有望产生预期的情节:

ggplot(data,aes(x=n,y=value,color=variable)) + geom_line()+
  labs(color="Legend")+
  scale_x_continuous("x", expand=c(0,0),
                     breaks=c(1,2,5,10,30,60)) +
  scale_y_continuous("y",expand=c(0,0),
                     limits=c(0, round(max(data[,3]),1)),
                     breaks=seq(0,round(max(data[,3]),1),by=0.1))

enter image description here

答案 2 :(得分:0)

我已经提出以下问题来解决这个问题。

lim <- ceiling(max(data[,3])*10)/10

ylim <- c(0,lim)