在ggplot2中更改轴标签名称 [英] Change axis labels name in ggplot2

查看:74
本文介绍了在ggplot2中更改轴标签名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关更改标签的命令,但是似乎无法在图形中正确获取它们.

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

我想将y轴标签从"HeartRate"更改为心率(次/分钟)".

这是我的命令:

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. :(

推荐答案

在代码的最后一行添加:

Add in the code's last line:

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

是这样的:

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

这篇关于在ggplot2中更改轴标签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆