在线的末尾绘制标签 [英] Plot labels at ends of lines
问题描述
我有以下数据( 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,color = State))
取而代之的是,我希望标签是
- 着色与系列相同
- 位于每个系列的最后一个数据点的右侧
我在下面的链接中注意到了baptiste的回答,但是当我尝试修改他的代码时( geom_text(aes(label = State,color = State ,x = Inf,y = Cap ex),hjust = -1)
)文本不会出现。 $ b ggplot2 - 在图的外部注释
temp .dat < - 结构(列表(年= c(2003,2004,2005,2006,2007,
2008,2009,2010 2011,2012,2013,2014,2003,
2004,2005,2006,2007,2008,2009 ,2011,
2012,2013,2014,2003,2004,2005,2006,2007,
2008 2009年2010年2011年2012年2013年2014年2003年b $ b2004年2005年2006年2007年2008年 (1L,1L,1L,1L,
1L,1,2009,2010 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) VIC,
NSW,QLD,WA),class =factor),Capex = c(5.3564147236 5348,
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,
9.70529678167458,11.3102788081848,11.8696420977237,6.77937303542605,
5.51242844820827,5.35789621712839,4.38699327451101,4.4925792218211,
4.29934654081527,4.54639175257732,4.70040615159951,5.04056109514957,
5.49921208937735,5.96590909090909,6.18700407463007)),类=数据.frame,row.names = c(NA,
-48L),.Names = c(Year,State,C apex))
要使用Baptiste的想法,您需要关闭剪辑。但是当你这样做时,你会得到垃圾。此外,您需要取消图例,并为 geom_text
选择2014年的资本支出,并增加保证金以为标签留出空间。 (或者,您可以调整 hjust
参数来移动绘图面板中的标签。)像这样:
<$ p
$ g
$ b $ ge $ =数据=子集(temp.dat,Year ==2014),aes(label = State,color = State,x = Inf,y =
scale_colour_discrete(guide ='none')+
theme(plot.margin = unit(c(1,3,1,1),lines)= ))
#关闭剪辑的代码
gt< - ggplotGrob(p)
gt $ layout $ clip [gt $ layout $ name ==panel]< ; - off
grid.draw(gt)
但是,这是完美的情节为直接标签
。
library(ggplot2)
library直接标签)
ggplot(temp.dat,aes(x =年份, y =焦平面,组=状态,颜色=状态))+
geom_line()+
scale_colour_discrete(guide ='none')+
scale_x_discrete(expand = c(0,1)) +
geom_dl(aes(label = State),method = list(dl.combine(first.points,last.points),cex = 0.8))
编辑增加终点和标签之间的空间:
<$ p $ ($ x $ year,y = Capex,group = State,color = State))+
geom_line()+
scale_colour_discrete(guide = 'none')+
scale_x_discrete(expand = c(0,1))+
geom_dl(aes(label = State),method = list(dl.trans(x = x + 0.2), (标记=状态),方法=列表(dl.trans(x = x-0.2),first.points,cex = 0.8))
I have the following data (temp.dat
see end note for full data)
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
...
and I can produce the following chart:
ggplot(temp.dat) +
geom_line(aes(x = Year, y = Capex, group = State, colour = State))
Instead of the legend, I'd like the labels to be
- coloured the same as the series
- Be to the right of the last data point for each series
I've noticed baptiste's comments in the answer in the following link, but when I try to adapt his code (geom_text(aes(label = State, colour = State, x = Inf, y = Capex), hjust = -1)
) the text does not appear.
ggplot2 - annotate outside of plot
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,
9.70529678167458, 11.3102788081848, 11.8696420977237, 6.77937303542605,
5.51242844820827, 5.35789621712839, 4.38699327451101, 4.4925792218211,
4.29934654081527, 4.54639175257732, 4.70040615159951, 5.04056109514957,
5.49921208937735, 5.96590909090909, 6.18700407463007)), class = "data.frame", row.names = c(NA,
-48L), .Names = c("Year", "State", "Capex"))
To use Baptiste's idea, you need to turn off clipping. But when you do, you get garbage. In addition, you need to suppress the legend, and, for geom_text
, select Capex for 2014, and increase the margin to give room for the labels. (Or you can adjust the hjust
parameter to move the labels inside the plot panel.) Something like this:
library(ggplot2)
library(grid)
p = ggplot(temp.dat) +
geom_line(aes(x = Year, y = Capex, group = State, colour = State)) +
geom_text(data = subset(temp.dat, Year == "2014"), aes(label = State, colour = State, x = Inf, y = Capex), hjust = -.1) +
scale_colour_discrete(guide = 'none') +
theme(plot.margin = unit(c(1,3,1,1), "lines"))
# Code to turn off clipping
gt <- ggplotGrob(p)
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
But, this is the sort of plot that is perfect for directlabels
.
library(ggplot2)
library(directlabels)
ggplot(temp.dat, aes(x = Year, y = Capex, group = State, colour = State)) +
geom_line() +
scale_colour_discrete(guide = 'none') +
scale_x_discrete(expand=c(0, 1)) +
geom_dl(aes(label = State), method = list(dl.combine("first.points", "last.points"), cex = 0.8))
Edit To increase the space between the end point and the labels:
ggplot(temp.dat, aes(x = Year, y = Capex, group = State, colour = State)) +
geom_line() +
scale_colour_discrete(guide = 'none') +
scale_x_discrete(expand=c(0, 1)) +
geom_dl(aes(label = State), method = list(dl.trans(x = x + 0.2), "last.points", cex = 0.8)) +
geom_dl(aes(label = State), method = list(dl.trans(x = x - 0.2), "first.points", cex = 0.8))
这篇关于在线的末尾绘制标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!