Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在ggplot2中将列名设置为直方图标题_R_Ggplot2 - Fatal编程技术网

R 如何在ggplot2中将列名设置为直方图标题

R 如何在ggplot2中将列名设置为直方图标题,r,ggplot2,R,Ggplot2,我想使用ggplot2从iris数据集中绘制多个直方图。唯一缺少的是一种优雅的方法,将数据集中的列名设置为labs(title=)中每个绘图的标题。我以前尝试过以多种方式使用colnames和paste,但是没有返回所需的输出。你们中有人知道如何完成最后一步,使每个柱状图显示相应的列名作为标题吗 这是我的reprex: library (ggplot2) # Reorder iris columns for convenience df <- iris[,c(5, 1:4)] # H

我想使用
ggplot2
从iris数据集中绘制多个直方图。唯一缺少的是一种优雅的方法,将数据集中的列名设置为
labs(title=)
中每个绘图的标题。我以前尝试过以多种方式使用
colnames
paste
,但是没有返回所需的输出。你们中有人知道如何完成最后一步,使每个柱状图显示相应的列名作为标题吗

这是我的reprex:

library (ggplot2)

# Reorder iris columns for convenience
df <- iris[,c(5, 1:4)]
 
# Histograms - z represents the columns of the df containing data for histograms
 histograms <- apply (df[,2:ncol(df)], 2, function (z){
     ggplot(df, aes(x = z)) +
       geom_histogram(aes(y = ..density..)) + 
       stat_function(fun = dnorm, args = list(mean = mean(z, na.rm =TRUE), sd = sd(z, na.rm = TRUE)), colour = "blue") +
       facet_wrap(~ Species) +
       labs (title = "Histogram for column z", x = "values")
  })

histograms
库(ggplot2)
#为方便起见,请重新排列iris列

df试试这个。不要迭代列,而是迭代名称:

库(ggplot2)
#为方便起见,请重新排列iris列

这是一个很好的解决方案!感谢您的宝贵意见。