Bar*_*rry 3 r raster histogram ggplot2
我有一个光栅文件,我想绘制为直方图,我使用hist()进行绘制,如下所示.但我想使用ggplot2进行绘图,以更好的方式绘制它.
conne <- file("C:\\fined.bin","rb")
r = raster(y)
hist(r, breaks=30, main="SMD_2010",
xlab="Pearson correlation", ylab="Frequency", xlim=c(-1,1))
Run Code Online (Sandbox Code Playgroud)
我试过这个:
qplot(rating, data=r, geom="histogram")
Run Code Online (Sandbox Code Playgroud)
错误:
ggplot2 doesn't know how to deal with data of class RasterLayer
Run Code Online (Sandbox Code Playgroud)
我需要绘制类似的东西:
作为快速解决方案,您可以使用结果 hist
f <- hist(r, breaks=30)
dat <- data.frame(counts= f$counts,breaks = f$mids)
ggplot(dat, aes(x = breaks, y = counts)) +
geom_bar(stat = "identity",fill='blue',alpha = 0.8)+
xlab("Pearson correlation")+ ylab("Frequency")+
scale_x_continuous(breaks = seq(-1,1,0.25), ## without this you will get the same scale
labels = seq(-1,1,0.25)) ## as hist (question picture)
Run Code Online (Sandbox Code Playgroud)
PS:也许你需要scale_x_discrete用来获得更好的轴外观
编辑添加渐变填充
ggplot(dat, aes(x = breaks, y = counts, fill =counts)) + ## Note the new aes fill here
geom_bar(stat = "identity",alpha = 0.8)+
xlab("Pearson correlation")+ ylab("Frequency")+
scale_x_continuous(breaks = seq(-1,1,0.25),
labels = seq(-1,1,0.25))+
scale_fill_gradient(low="blue", high="red") ## to play with colors limits
Run Code Online (Sandbox Code Playgroud)
