更改ggplot2中因子的图例文本

问题描述 投票:0回答:1

对于示例数据框:

df <- structure(list(antibiotic = c(0.828080341411847, 1.52002304506738, 
1.31925434545302, 1.66681722567074, 1.17791610945551, 0.950096368502059, 
1.10507733691997, 1.0568193215304, 1.03853131016669, 1.02313195567946, 
0.868629787234043, 0.902126485349154, 1.12005679002801, 1.88261441540084, 
0.137845900627507, 1.07040656448604, 1.41496470588235, 1.30978543173373, 
1.16931780610558, 1.05894439450366, 1.24805122785724, 1.21318238007025, 
0.497310305098053, 0.872362356327429, 0.902584749481137, 0.999731895498823, 
0.907560340983954, 1.05930840957587, 1.40457554864091, 1.09747179272879, 
0.944219456216072, 1.10363111431903, 0.974649273935516, 0.989983064420841, 
1.14784471036171, 1.17232858907798, 1.44675812720393, 0.727078405331282, 
1.36341361598635, 1.06120293299474, 1.06920290856811, 0.711007267992205, 
1.39034247642439, 0.710873996527168, 1.30529753573398, 0.781191310196629, 
0.921788181250106, 0.932214675722466, 0.752289683770589, 0.942392026874501
), year = c(3, 1, 4, 1, 2, 4, 1, 3, 4, 3, 4, 1, 2, 3, 4, 1, 1, 
4, 1, 1, 1, 1, 4, 1, 3, 3, 1, 4, 1, 4, 2, 1, 1, 1, 3, 4, 3, 2, 
2, 2, 3, 3, 1, 2, 3, 2, 3, 4, 4, 1), imd.decile = c(8, 2, 5, 
5, 4, 3, 2, 8, 6, 4, 3, 6, 9, 2, 5, 3, 5, 6, 4, 2, 9, 11, 2, 
8, 3, 5, 7, 8, 7, 4, 9, 7, 6, 4, 8, 10, 5, 6, 6, 11, 6, 4, 2, 
4, 10, 8, 2, 8, 4, 3)), .Names = c("antibiotic", "year", "imd.decile"
), row.names = c(17510L, 6566L, 24396L, 2732L, 13684L, 28136L, 
1113L, 15308L, 28909L, 21845L, 23440L, 1940L, 8475L, 22406L, 
27617L, 4432L, 3411L, 27125L, 6891L, 6564L, 1950L, 5683L, 25240L, 
5251L, 20058L, 18068L, 5117L, 29066L, 2807L, 24159L, 12309L, 
6044L, 7629L, 2336L, 16583L, 23921L, 17465L, 14911L, 8879L, 13929L, 
17409L, 19421L, 7239L, 11570L, 15283L, 8283L, 16246L, 27950L, 
23723L, 4411L), class = "data.frame")

我正在绘制针对imd.decile的抗生素使用情况(按年份)

p <- ggplot(df, aes(x = imd.decile, y = antibiotic, col = factor(year))) + 
  stat_summary(geom = "line", fun.y = mean, size=1) +
  ylab("My title y")  +
  xlab("My title x") +
  scale_y_continuous(breaks=seq(0, 2, 0.25))  +
  scale_x_continuous(breaks=seq(0, 10, 1)) +
  scale_fill_discrete(name="Year",
                      breaks=c("1", "2", "3", "4"),
                      labels=c("2013 - 2014", "2014 - 2015", "2015 - 2016", "2016 - 2017"))
p  

正如您所看到的,我正在尝试更改我的图例文本以及数字所说的内容。

有人可以解释为什么它不起作用?我的imd.decile变量在一个因素中可能会引起一些混乱......

另外我想让轴标题远离刻度线 - 我之前使用过“\” - 但这不起作用

r ggplot2
1个回答
1
投票

添加评论,你正在使用scale_fill_discrete,你应该使用scale_color_discrete。我记得这两者之间的区别是通过考虑fill占据区域的图形对象(即,它们是二维 - 想想条形,多边形等)和color用于不占用区域的图形对象(它们是一维的 - 想想线条,点等)。既然你是geom是线条,那么他们就没有fill可以离散地扩展。如果你在条形图上使用像多边形这样的geom,你可以使用scale_color_discrete,但它只会影响对象的边框。

试试这个:

p <- ggplot(df, aes(x = imd.decile, y = antibiotic, col = factor(year))) + 
  stat_summary(geom = "line", fun.y = mean, size=1) +
  ylab("My title y")  +
  xlab("My title x") +
  scale_y_continuous(breaks=seq(0, 2, 0.25))  +
  scale_x_continuous(breaks=seq(0, 10, 1)) +
  scale_color_discrete(name="Year",   # THIS IS THE LINE YOU HAVE TO CHANGE
                  breaks=c("1", "2", "3", "4"),
                  labels=c("2013 - 2014", "2014 - 2015", "2015 - 2016", "2016 - 2017"))
p  
© www.soinside.com 2019 - 2024. All rights reserved.