Search code examples
rplotscale

R: Adding a scale to graph


I have created the graph I wanted in R, but it isn't scaled the way I expected it to. The graph I created is below. The x-axis is age and currently it's listed as "10,11,12,13...91 and above." I want it to be something more like "10,20,30...91 and above." so the x-axis is readable. How would I go about doing that in R? The code for the graph is also below.

enter image description here

 ggplot(data, aes(Age,Shoes))+
    geom_density(stat="identity")+
    facet_wrap(~Gender)

The data is my data set which includes the three columns Age, Shoes, and Gender. Any help is greatly appreciated! Thanks everyone! Lizzie


Solution

  • You want to use scale_x_continuous() in your case, as alistaire suggested. You will specify where you want breaks in the function. In the example below, I specified 5 and 10.

    # Create a sample data
    mydf <- data.frame(age = rep(c(1, 2, 4, 10, 6, 7, 5, 5, 8, 10), times = 10),
                      shoes = runif(n = 100, min = 20, max = 75))
    
    
    ### X is specified
    
    ggplot(data = mydf, aes(x = age, y = shoes)) + 
    geom_bar(stat = "identity") +
    scale_x_continuous(breaks = c(5, 10))
    

    enter image description here

    ### Default
    ggplot(data = mydf, aes(x = age, y = shoes)) + 
    geom_bar(stat = "identity") 
    

    enter image description here