在行尾绘制标签

我有以下数据(temp.dat有关完整数据,请参见尾注)


   Year State     Capex

1  2003   VIC  5.356415

2  2004   VIC  5.765232

3  2005   VIC  5.247276

4  2006   VIC  5.579882

5  2007   VIC  5.142464

...

我可以生成以下图表:


ggplot(temp.dat) + 

  geom_line(aes(x = Year, y = Capex, group = State, colour = State))


我希望标签不是传说,而是


颜色与系列相同

每个系列的最后一个数据点的右侧

我在以下链接的答案中注意到baptiste的评论,但是当我尝试修改他的代码(geom_text(aes(label = State, colour = State, x = Inf, y = Capex), hjust = -1))时,文本不会出现。


ggplot2-在情节之外进行注释


temp.dat <- structure(list(Year = c("2003", "2004", "2005", "2006", "2007", 

"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2003", 

"2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", 

"2012", "2013", "2014", "2003", "2004", "2005", "2006", "2007", 

"2008", "2009", "2010", "2011", "2012", "2013", "2014", "2003", 

"2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", 

"2012", "2013", "2014"), State = structure(c(1L, 1L, 1L, 1L, 

1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 

2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 

4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("VIC", 

"NSW", "QLD", "WA"), class = "factor"), Capex = c(5.35641472365348, 

5.76523240652641, 5.24727577535625, 5.57988239709746, 5.14246402568366, 

4.96786288162828, 5.493190785287, 6.08500616799372, 6.5092228474591, 

7.03813541623157, 8.34736513875897, 9.04992300432169, 7.15830329914056, 

7.21247045701994, 7.81373928617117, 7.76610217197542, 7.9744994967006, 

7.93734452080786, 8.29289899132255, 7.85222269563982, 8.12683746325074, 

8.61903784301649, 9.7904327253813, 9.75021175267288, 8.2950673974226, 

6.6272705639724, 6.50170524635367, 6.15609626379471, 6.43799637295979, 

6.9869551384028, 8.36305663640294, 8.31382617231745, 8.65409824343971,

撒科打诨
浏览 668回答 3
3回答

德玛西亚99

要使用Baptiste的想法,您需要关闭剪辑。但是,当您这样做时,就会得到垃圾。此外,您需要geom_text隐藏图例,并为(选择)2014年的资本支出(Capex),并增加边距为标签留出空间。(或者,您可以调整hjust参数以在打印面板内移动标签。)类似以下内容:library(ggplot2)library(grid)p = ggplot(temp.dat) +&nbsp;&nbsp; geom_line(aes(x = Year, y = Capex, group = State, colour = State)) +&nbsp;&nbsp; geom_text(data = subset(temp.dat, Year == "2014"), aes(label = State, colour = State, x = Inf, y = Capex), hjust = -.1) +&nbsp; scale_colour_discrete(guide = 'none')&nbsp; +&nbsp; &nbsp;&nbsp;&nbsp; theme(plot.margin = unit(c(1,3,1,1), "lines"))&nbsp;# Code to turn off clippinggt <- ggplotGrob(p)gt$layout$clip[gt$layout$name == "panel"] <- "off"grid.draw(gt)在此处输入图片说明但是,这是最适合的情节directlabels。library(ggplot2)library(directlabels)ggplot(temp.dat, aes(x = Year, y = Capex, group = State, colour = State)) +&nbsp;&nbsp; geom_line() +&nbsp; scale_colour_discrete(guide = 'none') +&nbsp; scale_x_discrete(expand=c(0, 1)) +&nbsp; geom_dl(aes(label = State), method = list(dl.combine("first.points", "last.points"), cex = 0.8))&nbsp;在此处输入图片说明编辑 要增加端点和标签之间的间距,请执行以下操作:ggplot(temp.dat, aes(x = Year, y = Capex, group = State, colour = State)) +&nbsp;&nbsp; geom_line() +&nbsp; scale_colour_discrete(guide = 'none') +&nbsp; scale_x_discrete(expand=c(0, 1)) +&nbsp; geom_dl(aes(label = State), method = list(dl.trans(x = x + 0.2), "last.points", cex = 0.8)) +&nbsp; geom_dl(aes(label = State), method = list(dl.trans(x = x - 0.2), "first.points", cex = 0.8))&nbsp;

qq_花开花谢_0

这个问题虽然古老,但却是黄金,我为疲倦的ggplot人士提供了另一个答案。该解决方案的原理可以相当普遍地应用。Plot_df <-&nbsp;&nbsp; temp.dat %>% mutate_if(is.factor, as.character) %>%&nbsp; # Who has time for factors..&nbsp; mutate(Year = as.numeric(Year))现在,我们可以子集数据了ggplot() +&nbsp;geom_line(data = Plot_df, aes(Year, Capex, color = State)) +geom_text(data = Plot_df %>% filter(Year == last(Year)), aes(label = State,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x = Year + 0.5,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y = Capex,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color = State)) +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; guides(color = FALSE) + theme_bw() +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scale_x_continuous(breaks = scales::pretty_breaks(10))最后的pretty_breaks部分只是用于固定下面的轴。
打开App,查看更多内容
随时随地看视频慕课网APP