Change axis labels name in ggplot2

I have read so much about changing labels command, but I do not seem to get them correctly in my graph.

I want to change the y-axis label from "HeartRate" to "Heart Rate (beats/min)".

This is my command:

df <- data.frame(
    Phase = factor(c(0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9)),
    HeartRate = c(67, 70, 70, 73, 82, 83, 104, 106, 73, 86, 74, 65, 86, 82, 94, 108, 73, 81, 67, 68),
    group = factor(c("female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male")),
    se = c(4, 7, 4, 9, 5, 9, 8, 14, 6, 15, 3, 6, 6, 9, 4, 14, 3, 8, 4, 5)
)


limits <- aes(ymax = HeartRate + se, ymin=HeartRate - se)    
p <- ggplot(df, aes(fill=group, y=HeartRate, x=Phase))
p + geom_bar(position="dodge", stat="identity")    
limits <- aes(ymax = HeartRate + se, ymin=HeartRate - se)    
p <- ggplot(df, aes(fill=group, y=HeartRate, x=Phase, title="Gender differences in Heart Rate"))
p + geom_bar(position="dodge", stat="identity")    
dodge <- position_dodge(width=0.9)    
p + geom_bar(position=dodge) + geom_errorbar(limits, position=dodge, width=0.25)

I tried everything but I just don't seem to get it right. :(

Answer #1:

Add in the code's last line:

scale_y_continuous("Heart Rate (beats/min)")

It would be like this:

p + geom_bar(position=dodge) + geom_errorbar(limits, position=dodge, width=0.25) + scale_y_continuous("Heart Rate (beats/min)")

enter image description here

Answered By: Andre Silva
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles