【问题标题】:Why X-axis values of a histogram disappear when specifying the breaks?为什么在指定中断时直方图的 X 轴值会消失?
【发布时间】:2013-02-05 15:36:32
【问题描述】:

尝试使用 ggplot2 绘制光栅文件,但 x 轴值消失了。我很感激任何帮助

conne <- file("C:complete.bin","rb")
sd <- readBin(conne, numeric(), size=4,  n=1440*720, signed=TRUE)
y <-t(matrix((data=sd), ncol=1440, nrow=720))
f <- hist(y, breaks=30,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("Bi")+ 
    ylab("Frequency")+
    scale_fill_gradientn(colours = rev(rainbow(20, s = 1, v = 1, start = 0, end = 1)[1:12]))+
    ggtitle("2010")+
    theme(axis.title.x = element_text(size = 20))+
    theme(axis.title.y =  element_text(size = 20))+
    theme(plot.title = element_text(size = rel(2.5)))+
    scale_x_continuous(breaks = seq(seq(-0.5,0.5,0.1)),labels = seq(seq(-0.5,0.5,0.1)))

【问题讨论】:

  • 也许可以尝试删除代码的最后一行。
  • 你也可以让ggplot为你制作直方图:ggplot(sd, aes(x = y)) + geom_histogram(aes(fill = ..count..))
  • 运行 seq(seq(-0.5,0.5,0.1)) 并验证这是否是真的你想要做的。我猜不是。
  • 您正在尝试混合离散和连续。直方图在其“x 轴”上必然是离散的。您还使用与中断不一致的值进行标记。它不应该“工作”。在澄清了为什么这是有意义的......选择一个更合适的注释方法。

标签: r ggplot2 histogram raster


【解决方案1】:

正如@joran 所暗示的,将最后一行更改为

scale_x_continuous(breaks = seq(-0.5,0.5,0.1),labels = seq(-0.5,0.5,0.1))

(我认为 labels 参数在这种情况下可能是多余的。)当您运行 seq(seq(...)) 时,您得到的是在向量上运行 seq 的结果,即从 1 到索引的向量向量的长度 (11)。该向量根本不与数据的 x 范围重叠,因此中断消失了...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 2021-10-24
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多