为什么在指定断点时直方图的X轴值会消失?

时间:2021-11-26 14:56:35

trying to plot a raster file using ggplot2 but the x axis values disappeared. I am grateful to any help

尝试使用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)))

为什么在指定断点时直方图的X轴值会消失?

1 个解决方案

#1


3  

As @joran hinted, change the last line to

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

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

(I think the labels argument is probably redundant in this case.) When you run seq(seq(...)) what are you getting is the result of running seq on a vector, i.e. a vector of indices from 1 to the length of your vector (11). This vector doesn't overlap the x range of your data at all, so the breaks disappear ...

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

#1


3  

As @joran hinted, change the last line to

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

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

(I think the labels argument is probably redundant in this case.) When you run seq(seq(...)) what are you getting is the result of running seq on a vector, i.e. a vector of indices from 1 to the length of your vector (11). This vector doesn't overlap the x range of your data at all, so the breaks disappear ...

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