【问题标题】:Reproduce line plot in matplotlib or R在 matplotlib 或 R 中重现线图
【发布时间】:2016-11-09 15:03:10
【问题描述】:

我遇到了很棒的figure,它总结了(科学)作者多年来的合作。图贴在下面。

每条垂直线指的是单个作者。每条垂直线的开始对应于相关作者收到她的第一个合作者的年份(即,当她变得活跃并因此成为合作网络的一部分时)。作者根据他们在去年(即 2010 年)的合作者总数进行排名。着色表示每位作者的合作者数量多年来(从开始活跃到 2010 年)的增加情况。

我有一个类似的数据集;我的数据集中有关键字,而不是作者。每个数值表示特定年份的任期频率。数据如下:

Year Term1 Term2 Term3 Term4
1966     0     1     1     4
1967     1     5     0     0
1968     2     1     0     5
1969     5     0     0     2

例如,Term2 第一次出现在 1967 年,频率为 1,而 Term4 第一次出现在 1966 年,频率为 4。完整的数据集可在 here 获得。

【问题讨论】:

  • 这不是很有挑战性。展示你自己的努力并解释你陷入困境的地方。
  • 因为你有自然的垃圾箱(作者 ID 和年份),我会用热图/imshow 来做到这一点。首先用np.nan 填充它,然后用整数填充值(不清楚如何有分数协作者)。然后只需使用 ax.imshow 作为背景 + ax.plot 作为绘制线。

标签: r matplotlib data-visualization reproducible-research


【解决方案1】:

图表看起来很漂亮,所以我尝试重现它。事实证明它比我想象的要复杂一些。

df=read.table("test_data.txt",header=T,sep=",")
#turn O into NA until >0 then keep values
df2=data.frame(Year=df$Year,sapply(df[,!colnames(df)=="Year"],function(x) ifelse(cumsum(x)==0,NA,x)))
#turn dataframe to a long format 
library(reshape)
molten=melt(df2,id.vars = "Year")
#Create a new value to measure the increase over time: I used a log scale to avoid a few classes overshadowing the others.
#The "increase" is measured as the cumsum, ave() is used to get cumsum to work with NA's and tapply to group on "variable"
molten$inc=log(Reduce(c,tapply(molten$value,molten$variable,function(x) ave(x,is.na(x),FUN=cumsum)))+1)
#reordering of variable according to max increase
#this dataframe is sorted in descending order according to the maximum increase"
library(dplyr)
df_order=molten%>%group_by(variable)%>%summarise(max_inc=max(na.omit(inc)))%>%arrange(desc(max_inc))
#this allows to change the levels of variable so that variable is ranked in the plot according to the highest value of "increase"
molten$variable<-factor(molten$variable,levels=df_order$variable)
#plot
ggplot(molten)+
  theme_void()+ #removes axes, background, etc...
  geom_line(aes(x=variable,y=Year,colour=inc),size=2)+
  theme(axis.text.y = element_text())+
  scale_color_gradientn(colours=c("red","green","blue"),na.value = "white")# set the colour gradient

提供:

没有论文中的那么好,但这是一个开始。

【讨论】:

    猜你喜欢
    • 2018-01-07
    • 2013-04-11
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 2018-11-26
    • 2013-11-25
    • 2017-12-12
    相关资源
    最近更新 更多