将 class 间隔放在直方图中的图表中,例如作为图例

Put class intervals within the graph in a histogram, for example as a legend

我的代码的实际版本是:

hist_Price <- hist(diamonds$price)
hist(x = diamonds$price,
     main = "HIstograma del Precio de los Diamantes (Dataset : diamonds)",
     col="blue",
     ylim = c(0,max(hist_Price$counts)+2000),
     xaxt = 'n' # necesario para el uso de la funcion text()
)
axis(side = 1,at = seq(0,max(diamonds$price),3000))
text(hist_Price$mids,hist_Price$counts,labels=hist_Price$counts, adj=c(0.5, -0.5))
box()

我正在寻找一种方法将 class 间隔放入图表中,例如作为图例。可能如图所示

显然,使用 R 的图表应该看起来更专业

您的示例代码需要 19 种不同的颜色。很难区分这么多颜色,所以我把它减少到 10 种。否则就使用图例功能。

library(ggplot2)    ## for diamonds data
hist_Price = hist(x = diamonds$price,
    breaks = seq(0,20000, 2000),
     main = "Histograma del Precio de los Diamantes (Dataset : diamonds)",
     col=rainbow(10, end=0.85),
     ylim = c(0,max(hist_Price$counts)+2000),
     xaxt = 'n' # necesario para el uso de la funcion text()
)
axis(side = 1,at = seq(0,max(diamonds$price),2000))
text(hist_Price$mids,hist_Price$counts,labels=hist_Price$counts, adj=c(0.5, -0.5))
box()

LABELS = paste(seq(0,18000, 2000), seq(2000,20000, 2000), sep="-")
legend("topright", legend=LABELS, fill=rainbow(10, end=0.85))

请注意,我在显示后调整了图片大小。