在ggplot2中更改因子的图例文本

时间:2018-06-27 11:07:49

标签: r ggplot2

对于示例数据框:

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变量可能会引起一些混乱...

我还想使坐标轴标题离刻度线更远-我以前使用过“ \”-但这不起作用

1 个答案:

答案 0 :(得分:1)

要添加到注释中,您正在使用scale_fill_discrete,而应该使用scale_color_discrete。我记得这两种方法之间的区别,对于占用面积的图对象(即它们是二维的,例如条形图,多边形等),考虑fill,对于占图面积的图对象则考虑color不要占用区域(它们是一维的-想想线条,点等)。由于您的几何是线,因此它们不是fill可以单独缩放的线。如果您在条形图上使用了多边形之类的几何图形,则可以使用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