Histogram on Lattice

那年仲夏 提交于 2019-12-12 08:07:45

问题


Since hist() of the base R does not report percentages (and the freq=FALSE) does not help either, I turned to lattice.

histogram(rnorm(10000))

Please help me with the following:

  1. How can I get rid of the box arround the plot?
  2. How can I seperately define the cex of the x/y labels and x/y axis?
  3. How can I provide custom names to x and y axis?

回答1:


This should get you started:

library(lattice)
histogram(rnorm(10000),     
    main=list(
        label="Main plot title",
        cex=1.5),
    xlab=list(
        label="Custom x-axis label",
        cex=0.75),
    ylab=list(
        label="My very own y-axis label",
        cex=1.2),
    scales=list(cex=0.5),
    par.settings = list(axis.line = list(col = 0))
)




回答2:


Or, if you want to stick with hist(), you can modify it slightly, as shown below.

This function calls hist() once to get its return value, which is an object containing all sorts of useful information about the structure of the histogram. It then uses (a) the width of the bins and (b) the density for each bar to calculate (c) the percentage of the observations falling in each bar.

histPercent <- function(x, ...) {
   H <- hist(x, plot = FALSE)
   H$density <- with(H, 100 * density* diff(breaks)[1])
   plot(H, freq = FALSE, ...)
}

histPercent(rnorm(10000), col="dodgerblue", las=1,
            xlab="Echs-axis", ylab="Why-axis")



来源:https://stackoverflow.com/questions/9080917/histogram-on-lattice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!