Search code examples
rchartshistogramfrequencyaxis-labels

How can I build histogram with proportion values in y-axis?


I am experimenting with ggplot package in R language. My task is to plot the chart shown in the picture from this excellent paper: http://cmup.fc.up.pt/cmup/engmat/2012/seminario/artigos2012/Luis_Ferreira/Using%20logistic%20regression%20to%20estimate%20the%20influence%20of%20accident.pdf

Can you please give me some hint on how to plot this? Thank you in advance

Example of the plot I need to build in R language


Solution

  • I think this is what you are looking for:

    # Category names
    my.names <- c("test1","test2","test3")
    
    # Example data
    data <- runif(length(my.names))
    
    # Normalize the example data as a percentage of the total
    data.norm <- data / sum(data)
    
    # Use barplot to plot the results, plot without an x axis
    x <- barplot(data.norm,names.arg=my.names,xaxt="n")
    
    # Apply new x labels rotated by 45 degrees
    text(cex=1, x=x-0.1, y=-0.05, my.names, xpd=TRUE, srt=45)