R直方图添加带有百分比而不是粗略值的标签
R histogram add label with percentage instead of rough values
我想用hist()
用百分比制作直方图。
但是,我还想在每个直方图条的顶部放置一个标签,显示实际百分比。
这是我的代码和输出的 link:
DataToPlot <- sapply(dataframe, as.numeric)
hist(DataToPlot, xlab="dataframe",ylab="Calls", main="Weekly data", col="lightgreen",labels = TRUE, ylim=c(0, 12000))
使用freq = TRUE
选项:
hist(DataToPlot, xlab="dataframe",ylab="Calls", main="Weekly data", col="lightgreen",labels = TRUE, ylim=c(0, 12000), freq = TRUE)
添加此行(value
是您希望作为标签的列):
labs <- paste(round(DataToPlot$value), "%", sep="")
然后这样调用情节:
hist(DataToPlot, xlab="dataframe",ylab="Calls", main="Weekly data", col="lightgreen",labels = labs, ylim=c(0, 12000))
你想要的是freq = FALSE
选项。 @jkd 搞错了。
请注意文档说
freq
: logical; if TRUE, the histogram graphic is a representation of frequencies, the counts component of the result; if FALSE, probability densities, component density, are plotted (so that the histogram has a total area of one). Defaults to TRUE if and only if breaks are equidistant (and probability is not specified).
如果您只想使用基本图形,这与仅使用 hist()
函数的效果一样好。您可以像这样手动添加百分比值
data(iris)
# store the histogram in 'h'
h <- hist(iris$Sepal.Length, xlab="dataframe", ylab="Calls", freq = FALSE,
main="Weekly data", col="lightgreen", labels = FALSE, ylim = c(0,0.5))
text(
x = (h$breaks[-1] + h$breaks[-length(h$breaks)])/2, # compute break center
y = h$density, # use density as y value
labels = paste(round((h$density/sum(h$density))*100, 2), "%"), # labels are (density/sum(density)) *100 for %
pos = 3) # pos = 3 means above the specified coordinates
这产生
请注意,我使用的数据来自 iris
数据集,因为您没有提供任何
我想用hist()
用百分比制作直方图。
但是,我还想在每个直方图条的顶部放置一个标签,显示实际百分比。
这是我的代码和输出的 link:
DataToPlot <- sapply(dataframe, as.numeric)
hist(DataToPlot, xlab="dataframe",ylab="Calls", main="Weekly data", col="lightgreen",labels = TRUE, ylim=c(0, 12000))
使用freq = TRUE
选项:
hist(DataToPlot, xlab="dataframe",ylab="Calls", main="Weekly data", col="lightgreen",labels = TRUE, ylim=c(0, 12000), freq = TRUE)
添加此行(value
是您希望作为标签的列):
labs <- paste(round(DataToPlot$value), "%", sep="")
然后这样调用情节:
hist(DataToPlot, xlab="dataframe",ylab="Calls", main="Weekly data", col="lightgreen",labels = labs, ylim=c(0, 12000))
你想要的是freq = FALSE
选项。 @jkd 搞错了。
请注意文档说
freq
: logical; if TRUE, the histogram graphic is a representation of frequencies, the counts component of the result; if FALSE, probability densities, component density, are plotted (so that the histogram has a total area of one). Defaults to TRUE if and only if breaks are equidistant (and probability is not specified).
如果您只想使用基本图形,这与仅使用 hist()
函数的效果一样好。您可以像这样手动添加百分比值
data(iris)
# store the histogram in 'h'
h <- hist(iris$Sepal.Length, xlab="dataframe", ylab="Calls", freq = FALSE,
main="Weekly data", col="lightgreen", labels = FALSE, ylim = c(0,0.5))
text(
x = (h$breaks[-1] + h$breaks[-length(h$breaks)])/2, # compute break center
y = h$density, # use density as y value
labels = paste(round((h$density/sum(h$density))*100, 2), "%"), # labels are (density/sum(density)) *100 for %
pos = 3) # pos = 3 means above the specified coordinates
这产生
请注意,我使用的数据来自 iris
数据集,因为您没有提供任何