R:在系数路径图中的行尾绘制值 [英] R: plotting values at the end of the lines in a coefficient path plot
本文介绍了R:在系数路径图中的行尾绘制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个非常简单的代码,可以生成系数路径图:
库(ggplot2)图书馆(dplyr)图书馆(tidyr)value2 = matrix(c(0,0,2,1,2,0,0,0,0,0,0,0,0,0,0,1,0,2,3,4,0,0,0,0,0),nrow = 5)#阴谋L1 <-函数(x)和(abs(x));bind_cols(as.data.frame(value2)%&%;%summarise_all(funs(L1(.)))%>%t()%&%;%as.data.frame()%&%;%重命名(x = V1),t(value2)%>%as.data.frame()%&%;%named_all(funs(gsub("V",",.))))%&%;%collect(row,y,2:2:(nrow(value2)+1))%>%ggplot(aes(x,y,color = row))+ geom_line()+geom_vline(xintercept = 4.5,col =黑色")
这是它生成的图:我想做的只是在行的末尾添加标签,对于黑线,我也想添加其值.像这样:
我可以使用任何 ggplot
函数吗?
解决方案
(v0.2.0)创建于2018-04-01.
I have this very simple code that generates a coefficient path plot:
library(ggplot2)
library(dplyr)
library(tidyr)
value2=matrix(c(0,0,2,1,2,0,0,0,0,0,0,0,0,0,0,1,0,2,3,4,0,0,0,0,0), nrow =5 )
#Plot
L1 <- function(x) sum(abs(x));
bind_cols(
as.data.frame(value2) %>%
summarise_all(funs(L1(.))) %>%
t() %>%
as.data.frame() %>%
rename(x = V1),
t(value2) %>%
as.data.frame() %>%
rename_all(funs(gsub("V", "", .)))) %>%
gather(row, y, 2:(nrow(value2)+1)) %>%
ggplot(aes(x, y, colour = row)) + geom_line() +
geom_vline(xintercept = 4.5 , col = "black")
And here is the plot it generates: What I want to do is just add the labels at the end of the line, and for the black line I want to add its value too. Like this:
Is there any ggplot
function that I could use for this?
解决方案
ggrepel
can do this kind of job
library(ggplot2)
library(dplyr)
library(tidyr)
library(ggrepel)
value2 = matrix(c(0,0,2,1,2,0,0,0,0,0,0,0,0,0,0,1,0,2,3,4,0,0,0,0,0), nrow=5)
#Plot
L1 <- function(x) sum(abs(x));
bind_cols(
as.data.frame(value2) %>%
summarise_all(funs(L1(.))) %>%
t() %>%
as.data.frame() %>%
rename(x = V1),
t(value2) %>%
as.data.frame() %>%
rename_all(funs(gsub("V", "", .)))) %>%
gather(row, y, 2:(nrow(value2)+1)) -> df
ggplot(df, aes(x, y, colour = row)) + geom_line() +
geom_vline(xintercept = 4.5 , col = "black") +
geom_text_repel(
data = subset(df, x == max(x)),
aes(label = row),
size = 6,
nudge_x = 1,
segment.color = NA
) +
theme_classic() +
theme(legend.position = "none")
Created on 2018-04-01 by the reprex package (v0.2.0).
这篇关于R:在系数路径图中的行尾绘制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文