R 一个图形上有多行:在每行末尾(ggplot2)添加一个文本首字母缩略词(“图例”)

R 一个图形上有多行:在每行末尾(ggplot2)添加一个文本首字母缩略词(“图例”),r,graph,ggplot2,R,Graph,Ggplot2,我已生成以下图表: 我有大量的长数据(可在此处下载:),数据如下: head(data) -10:130 variable value 1 -10 Utilities 0.001680609 2 -9 Utilities 0.004652453 3 -8 Utilities -0.002441692 4 -7 Utilities -0.018639594 5 -6 Utilities -0.007587632 6

我已生成以下图表:

我有大量的长数据(可在此处下载:),数据如下:

head(data)
  -10:130  variable        value

1     -10 Utilities  0.001680609
2      -9 Utilities  0.004652453
3      -8 Utilities -0.002441692
4      -7 Utilities -0.018639594
5      -6 Utilities -0.007587632
6      -5 Utilities  0.004526066
我用于生成此图形的代码:

ggplot(data=data,
       aes(x=-10:130, y=value, colour=variable)) +
    geom_line()
我想要的东西看起来像下图:

图例消失后,该类别的首字母缩略词以与该行相同的颜色显示在每行末尾。这是必要的,因为有太多的线条和颜色让读者无法理解。一旦你们这些天才帮我解决了这个问题,我将制作一个4面板图(使用facet_网格),每个图有10条线


谢谢:)

删除您可以使用的图例

+ opts(legend.position = 'none')
+ annotate("text",x=XPOSITION,y=YPOSITION,label="TEXTFORPLOT",size=3.5)
要将文本添加到绘图,可以使用

+ opts(legend.position = 'none')
+ annotate("text",x=XPOSITION,y=YPOSITION,label="TEXTFORPLOT",size=3.5)
解决你的问题的一次快速肮脏的尝试

library(ggplot2)
## Read in the data from your link. You will have to change this.
dat <- read.csv("~/Downloads/demo.csv")
head(dat)
## Get the y values - turns out they are all 130
label_summary <- aggregate(dat[,2], list(dat$variable), max)
## A quick method to reduce the names, by grabbing the first 3 characters
label_names <- substr(label_summary[,1],1,3)
## get the x values of each variable
label_x <- label_summary[,2]
# A method to get the last y value of each variable
label_y <- sapply(1:length(label_x), function(i) dat[with(dat, dat[, 2]==label_x[i]&dat[, 3]==label_summary[i,1]),"value"])
# Make the plot without legend and text
p <- ggplot(data=dat,aes(x=-10:130, y=value, colour=variable)) + geom_line() + opts(legend.position = 'none')
p 
# Use an sapply function to add the labels one by one to the. This is needed because the label option within the annotate function only allow one label.
p + sapply(1:length(label_x), function(i) annotate("text",x=label_x[i]+10,y=label_y[i],label=label_names[i],size=3.5))
库(ggplot2)
##从链接中读取数据。你必须改变这个。

dat删除可以使用的图例

+ opts(legend.position = 'none')
+ annotate("text",x=XPOSITION,y=YPOSITION,label="TEXTFORPLOT",size=3.5)
要将文本添加到绘图,可以使用

+ opts(legend.position = 'none')
+ annotate("text",x=XPOSITION,y=YPOSITION,label="TEXTFORPLOT",size=3.5)
解决你的问题的一次快速肮脏的尝试

library(ggplot2)
## Read in the data from your link. You will have to change this.
dat <- read.csv("~/Downloads/demo.csv")
head(dat)
## Get the y values - turns out they are all 130
label_summary <- aggregate(dat[,2], list(dat$variable), max)
## A quick method to reduce the names, by grabbing the first 3 characters
label_names <- substr(label_summary[,1],1,3)
## get the x values of each variable
label_x <- label_summary[,2]
# A method to get the last y value of each variable
label_y <- sapply(1:length(label_x), function(i) dat[with(dat, dat[, 2]==label_x[i]&dat[, 3]==label_summary[i,1]),"value"])
# Make the plot without legend and text
p <- ggplot(data=dat,aes(x=-10:130, y=value, colour=variable)) + geom_line() + opts(legend.position = 'none')
p 
# Use an sapply function to add the labels one by one to the. This is needed because the label option within the annotate function only allow one label.
p + sapply(1:length(label_x), function(i) annotate("text",x=label_x[i]+10,y=label_y[i],label=label_names[i],size=3.5))
库(ggplot2)
##从链接中读取数据。你必须改变这个。

dat您可能想看看directlabels软件包。我想他可能只需要在
geom_text()
中设置
hjust
(水平调整)就可以了。您可能想看看directlabels软件包。我想他可能只需要设置
hjust
(水平调整)就可以了在
geom\u text()中