gpt4 book ai didi

r - 一张图上的多条线 : Adding a text acronym ("legend") to the very the end of each line (ggplot2)

转载 作者:行者123 更新时间:2023-12-04 18:17:36 25 4
gpt4 key购买 nike

我生成了以下图表:

http://i47.tinypic.com/s3dd0m.png

我有相当多的长数据(可以在这里下载:http://www.sendspace.com/file/lfd31r),数据看起来像:

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()

我想要看起来像下图的东西:

i46.tinypic.com/2cmvfrq.png

图例消失了,但类别的首字母缩略词显示在每行的末尾,颜色与行本身相同。这将是必要的,因为有太多的线条和颜色让读者无法理解是什么。一旦你们的天才帮助我弄清楚如何解决这个问题,我将制作一个 4 面板图(使用 facet_grid),每个面板有 10 行。

谢谢 :)

最佳答案

要删除图例,您可以使用

+ 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))

关于r - 一张图上的多条线 : Adding a text acronym ("legend") to the very the end of each line (ggplot2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11364729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com