简体   繁体   中英

R - How do I plot a histogram with a specific y-axis (rather than just frequency)?

I'm looking to create a histogram which plots a ranking (1-400ish values) along the x-axis and a frequency per 1000 people on the y-axis. Is there a way to do this with the hist() function? Currently I am using plot.default() like so:

plot.default(frequencyData$Deprivation.rank, frequencyData$Prescription.per.1000.people, xlab = 
"Deprivation Rank", ylab = "per 1000", main=graph_title, xlim=NULL, ylim=NULL, type="h")

Once I have this done I would like to calculate the mean and standard deviation and plot them as a line on the graph I am getting which currently (using plot.deafult) looks like this

Anyone able to provide any help?

You didn't provide any data to test with, but I suppose you can do it without any particular problem. Just use the hist() function.

Here's the documentation which you can reach typing ?hist in the R console:

Histograms Description

The generic function hist computes a histogram of the given data values. If plot = TRUE , the resulting object of class "histogram" is plotted by plot.histogram , before it is returned.

 Usage hist(x, ...)

Default S3 method:

 hist(x, breaks = "Sturges", freq = NULL, probability =,freq. include,lowest = TRUE, right = TRUE, density = NULL, angle = 45, col = NULL, border = NULL, main = paste("Histogram of", xname), xlim = range(breaks), ylim = NULL, xlab = xname, ylab, axes = TRUE, plot = TRUE, labels = FALSE, nclass = NULL. warn,unused = TRUE. ...)

So, you can try changing the plot function as this:

hist(
  frequencyData$Deprivation.rank,
  frequencyData$Prescription.per.1000.people,
  xlab = "Deprivation Rank",
  ylab = "per 1000",
  main = graph_title,
  xlim = NULL,
  ylim = NULL
)

ggplot alternative

You can obtain a better-looking graph by using the ggplot library, like this example:

library(ggplot2)
ggplot(frequencyData, aes(Deprivation.rank, Prescription.per.1000.people)) +
  geom_histogram() +
  xlab("Deprivation Rank") +
  ylab("per 1000") +
  ggtitle(graph_title)

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM