【问题标题】:R - How to histogram multiple matrixes using qplot/ggplot2R - 如何使用 ggplot/ggplot2 对多个矩阵进行直方图
【发布时间】:2014-10-09 08:26:55
【问题描述】:

我正在使用 R 从 NetCDF 文件 (ncdf4) 读取和绘制数据。我最近才开始使用 R,所以我很困惑,请原谅。

假设我从文件中获得了 N 个二维数值矩阵,每个矩阵都有不同的维度和许多 NA 值。

我必须在同一个图中对这些值进行直方图,在给定的宽度和给定的范围内,每个矩阵都相同。 对于一个矩阵,我可以这样做:

library(ncdf4)
library(ggplot2)

file0 <- nc_open("test.nc")
#Read a variable
prec0 <- ncvar_get(file0,"pr")
#Some settings
min_plot=0
max_plot=30
bin_width=2
xlabel="mm/day"
ylabel="PDF"
title="Precipitation"
#Get maximum of array, exclude NAs
maximum_prec0=max(prec0, na.rm=TRUE)
#Store the histogram
histo_prec0 <- hist(prec0, xlim=c(min_plot,max_plot), right=FALSE, breaks=seq(0,ceiling(maximum_prec0),by=bin_width))
#Plot the histogram densities using points instead of bars, which is what we want
qplot(histo_prec0$mids, histo_prec0$density, xlim=c(min_plot,max_plot), color=I("yellow"), xlab=xlabel, ylab=ylabel, main=title, log="y")
#If necessary, can transform matrix to vector using
#vector_prec0 <- c(prec0)

但是我发现最好使用 DataFrame 来绘制多个矩阵。我不确定这一点,也不知道该怎么做。这也将允许自动图例以及使用带有 ggplot2 的数据框所带来的所有优势。

我想要实现的目标类似于: https://copy.com/thumbs_public/j86WLyOWRs4N1VTi/scatter_histo.jpg?size=1024

在 Y 上我们有 Density,在 X 上是 bin。

提前致谢。

【问题讨论】:

  • 我会从每个栅格中提取值并将它们放入 data.frame 中,每列一个栅格。我会融化这个data.frame并使用color = variable绘制结果。
  • 您的示例图表明您正在寻找散点图而不是直方图。 docs.ggplot2.org/0.9.3.1/geom_point.html 应该可以帮助您到达那里。
  • @RomanLuštrik:是的,这是我想到的第一件事,fileunderwater 在下面的答案中或多或少地解释了。

标签: r ggplot2 dataframe


【解决方案1】:

说实话,不清楚你在追求什么(散点图或以点为值的数据直方图?)。

这里有几个使用 ggplot 的示例,它们可能符合您的目标(基于您的最后一句话:“Y 上我们有密度,X 上的垃圾箱”):

# some data 
nsample<- 200
d1<- rnorm(nsample,1,0.5)
d2<- rnorm(nsample,2,0.6)

#transformed into histogram bins and collected in a data frame
hist.d1<- hist(d1)
hist.d2<- hist(d2)
data.d1<- data.frame(hist.d1$mids, hist.d1$density, rep(1,length(hist.d1$density)))
data.d2<- data.frame(hist.d2$mids, hist.d2$density, rep(2,length(hist.d2$density)))
colnames(data.d1)<- c("bin","den","group")
colnames(data.d2)<- c("bin","den","group")
ddata<- rbind(data.d1,data.d2)
ddata$group<- factor(ddata$group)

# plot
plots<- ggplot(data=ddata, aes(x=bin, y=den, group=group)) +
     geom_point(aes(color=group)) + 
     geom_line(aes(color=group)) #optional
print(plots)

但是,您也可以直接在 ggplot 中生成平滑的密度图(或直方图):

ddata2<- cbind(c(rep(1,nsample),rep(2,nsample)),c(d1,d2))
ddata2<- as.data.frame(ddata2)
colnames(ddata2)<- c("group","value")
ddata2$group<- factor(ddata2$group)

plots2<- ggplot(data=ddata2, aes(x=value, group=group)) +
     geom_density(aes(color=group))   
     # geom_histogram(aes(color=group, fill=group))  # for histogram instead
windows()
print(plots2)

【讨论】:

  • 第一个正是我要找的,我的好先生,谢谢!我唯一不明白的是使用那个因子()。就我而言,它只返回一个错误:$&lt;-.data.frame(*tmp*, "group", value = integer(0)) 中的错误:替换有 0 行,数据有 2150
  • @AdrianoFantini factor 调用只是为了使我的分类变量成为一个因素(否则图中的图例是渐变色条)。从您写的内容中,我无法说出您为什么会收到错误消息。您需要做的事情是有一列密度,一列中点和一列对不同组进行分类(您的问题中的“多个矩阵”)。如果分类是字符向量,则不需要factorcall。
  • @AdrianoFantini 另外,请随时点赞并接受您认为有用的答案。
  • 感谢您的解释。我也不明白这个错误,但它有效,因为我只使用字符向量作为分类,所以现在我不会深入挖掘。我接受了答案,但无法投票,因为这是我第一次参加 SO,而且我没有足够的代表(还没有!)。再次感谢。
猜你喜欢
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 2021-10-10
  • 2017-11-06
相关资源
最近更新 更多