【问题标题】:How to force a y axis to minimum and maximum range in R?如何强制y轴在R中的最小和最大范围?
【发布时间】:2013-02-04 08:34:50
【问题描述】:

如果您查看下图(y 轴),您会注意到比例从00.20。我还有其他直方图,范围从00.4。我想让它们从01 保持一致,并从01 显示y 轴。

conne <- file("C:Aisdefined.bin","rb")
sd    <- readBin(conne, numeric(), size=4,  n=1440*720, signed=TRUE)
y     <- t(matrix((data=sd), ncol=1440, nrow=720))
r     <- raster(y)
f     <- hist(y, breaks=10,main="sm")

f$counts <- f$counts/sum(f$counts)
dat <- data.frame(counts= f$counts,breaks = f$mids)
ggplot(dat, aes(x = breaks, y = counts, fill =counts)) + 
   geom_bar(stat = "identity",alpha = 0.8) +
   xlab("Pearson correlation")+ ylab("Frequency") + 
   scale_x_continuous(breaks = seq(-1,1,0.250), labels = seq(-1,1,0.250)) +
   ggtitle("2011") + theme(axis.title.x = element_text(size = 20)) + 
   theme(axis.title.y = element_text(size = 20)) + 
   theme(text = element_text(size=20), 
   axis.text.x = element_text(angle = 90, vjust=1,colour="black"), 
   axis.text.y = element_text(colour="black")) + 
   theme(plot.title = element_text(size = rel(2.5))) + 
   scale_fill_gradientn(colours = "black")

【问题讨论】:

  • 在0以下没有数据点的情况下,为什么ggplot2在y轴值0以下包含空格?

标签: r ggplot2 histogram


【解决方案1】:

只需添加:

+ coord_cartesian(ylim=c(0,1))

【讨论】:

  • 为什么不简单地ylim()
  • +coord_cartesian(ylim = c(0.005, 0.02)) 中的错误:一元运算符的参数无效
  • @MonaJalal 需要添加到 ggplot 对象中,不能单独使用。它旨在添加到 OP 的原始绘图规范中。
【解决方案2】:

试试这个:

scale_y_continuous( limits = c(0,1), expand = c(0,0) )

【讨论】:

  • 注意:在规模上设置限制可能会排除数据。
  • @James 确实 - 我错过了 OP 只想要 y 轴。我的错。在这种情况下使用 scale_y_continuous 仍然有效,因为它是一个直方图,因此值将介于 0 和 1 之间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多