R图,使用线宽传达信息的相对重要性

时间:2018-08-16 15:57:01

标签: r plot

我有5条线代表不同的轨迹,这些轨迹的可能性不同。我想更改线的相对粗细以传达有关其可能性的信息。可能性加起来达到100%,我希望线条的粗细反映出来,这是一些虚拟数据

attempt<-matrix(0,ncol=6,nrow=5, dimnames=list(c("Intermediate A", 
    "Worst Case", "Intermediate B", "Best Case","Intermediate C"),
    c("likelihood",c(1:5))))

attempt[,1]<-c(0.2,0.10,0.30,0.25,0.15)
attempt[,2]<-c(rep(0,nrow(attempt)))
attempt[,3]<-c(0.1,0.2,0.15,0.06,0.15)
attempt[,4]<-c(0.2,0.4,0.2,0.10,0.3)
attempt[,5]<-c(0.3,0.6,0.25,0.18,0.4)
attempt[,6]<-c(0.4,0.8,0.3,0.21,0.5)

#want to plot these as lines that have varying thickness according to the
#likelihood in column 1

plot(0,0, xlim=c(1,5), ylim=c(0,1), xlab="years", ylab="probability of 
extinction")
  lines(attempt[1,2:6], col="blue")
  lines(attempt[2,2:6], col="red")
  lines(attempt[3,2:6], col="brown")
  lines(attempt[4,2:6], col="green")
  lines(attempt[5,2:6], col="purple")

我发现的大多数帖子都指向重叠的线(频率)或在数据的不同部分上改变单条线的粗细,我希望读者能够判断哪条线最有可能(遵循可能性1)中的值通过线条的相对粗细。

2 个答案:

答案 0 :(得分:1)

plot(0,0, xlim=c(1,5), ylim=c(0,1), xlab="years", ylab="probability of 
extinction")

lwd_mult <- 100

lines(attempt[1,2:6], col="blue", lwd=attempt[1, 1] * lwd_mult)
lines(attempt[2,2:6], col="red", lwd=attempt[2, 1] * lwd_mult)
lines(attempt[3,2:6], col="brown", lwd=attempt[3, 1] * lwd_mult)
lines(attempt[4,2:6], col="green", lwd=attempt[4, 1] * lwd_mult)
lines(attempt[5,2:6], col="purple", lwd=attempt[5, 1] * lwd_mult)

lwd参数设置线的相对粗细,lwd_mult参数用于可视地缩放粗细。

答案 1 :(得分:0)

另一个使用tidyverse

的选项
library(tidyverse)

as.data.frame(attempt) %>%
  rownames_to_column()%>%
  gather(num, val, -likelihood, -rowname) %>% 
  ggplot(aes(x = num, y = val, group = rowname, color = rowname, size = likelihood))+
  geom_line()

reprex package(v0.2.0)于2018-08-16创建。

首先,我们转换为数据框,然后将行名称更改为列,然后将数据收集为长格式,最后绘制数据。在aes中,我们可以告诉ggplot,我们希望颜色基于行名,而行的大小则基于似然性。