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列
这是一个很好的解决方案!感谢您的宝贵意见。