【问题标题】:Visualizing transitions in panel data with ggplot2使用 ggplot2 可视化面板数据中的转换
【发布时间】:2013-11-14 07:54:58
【问题描述】:

首先是一些玩具数据:

df = read.table(text = 
              "id      year    transition1 transition2 
1           2000    0   1
1           2001    1   0
1           2002    0   0
1           2003    0   0
2           2000    0   0
2           2002    0   0
2           2003    1   1
3           2000    0   0
3           2001    0   0
3           2002    1   0
3           2003    0   1 ", sep = "", header = TRUE)

我尝试在一个图中为每个 id 可视化,

  1. 如果一年中有一次观察(黑点)

  2. 如果发生transition1(红点)

  3. 如果发生转换 2(黄色点)

我大概知道如何可视化第一步

p <- ggplot(df, aes(y=id))
p <- p + geom_point(aes(x=year), colour="black")
p

但是如何为每个过渡添加点?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    一个选项,使用嵌套的ifelse 创建列向量:

    library(ggplot2)
    p <- ggplot(df, aes(y=id))
    p <- p + geom_point(aes(x=year), size=10,
                      colour=ifelse(df$transition1==1,"red",
                                    ifelse(df$transition2==1,"yellow","black")))
    p
    

    【讨论】:

    • 太好了,谢谢。这几乎就是我想要的。我怎样才能另外避免一点覆盖另一点?例如,在 2003 年,id 2 的所有点都应该可见。(如果有更好的解决方案,我不关心颜色)
    • 如何去掉y轴上的1.5和2.5?
    【解决方案2】:

    要添加到@agstudy 的答案,请尝试 geom_jitter()。

    library(ggplot2)
    p <- ggplot(df, aes(y=id)) +
      geom_point(aes(x=year), size=10,
                 colour=ifelse(df$transition1==1,"red",
                               ifelse(df$transition2==1,"yellow","black"))) +
      geom_jitter()
    

    【讨论】:

    • @agstudy 非常感谢。嵌套 ifelse 的解决方案是否也适用于两个以上的转换?最后,是否可以使点透明而不使用抖动?
    【解决方案3】:

    下面是一种略有不同的处理方式,您可能会也可能不会觉得有帮助。本质上,它为两个观察结果生成了一个组合变量。目前这仅适用于 2 个级别,但如果您需要它在更多级别上工作,请告诉我,我会考虑使其适用于 3 个或更多级别。

    library(ggplot2)
    
    df = read.table(text = 
                      "id      year    transition1 transition2 
    1           2000    0   1
    1           2001    1   0
    1           2002    0   0
    1           2003    0   0
    2           2000    0   0
    2           2002    0   0
    2           2003    1   1
    3           2000    0   0
    3           2001    0   0
    3           2002    1   0
    3           2003    0   1 ", sep = "", header = TRUE)
    
    # get transition names
    trans.names <- names(df[3:4])
    
    # add up the numbers in the data columns
    df$total <- apply(df[,c(3:4)], 1, sum)
    
    # for the columns where there is only on transition figure out which type it is
    df$total2[which(df$total==1)] <- apply(df[which(df$total==1),c(3:4)], 1, which.max)
    
    #set up the type variable to recieve data
    df$type <- ""
    
    # put in the names of the variable where it is one or the other
    df$type <- trans.names[df$total2]
    
    # overwrite that data with observation for those ones with no data
    df$type[which(df$total==0)] <- "Observation"
    
    # for the ones with both, mark as such
    df$type[which(df$total==2)] <- "transition 1 and 2"
    
    # cleanup, remove calculation columns
    df <- df[,c("id","year","type")]
    
    # reorder the factors in the 'type' data
    df$type <- factor(df$type,levels=c("Observation","transition1", "transition2","transition 1 and 2"))
    
    # make other variables into factors
    df$id <- as.factor(df$id)
    df$year <- as.factor(df$year)
    
    #plot data
    p <- ggplot(df, aes(y=id,x=year,color=type)) +
      geom_point(size=10) +
      scale_color_manual(name="Legend Name Goes Here",values=c("black", "red", "yellow","purple"))+
      theme(legend.position="bottom")+
      ggtitle("Title Goes Here")+
      ylab("y label")+
      xlab("x label")
    
    #show plot
    p
    

    希望对您有所帮助, 乔什

    【讨论】:

      猜你喜欢
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2019-04-13
      • 2016-05-02
      • 2022-01-21
      • 2016-02-12
      相关资源
      最近更新 更多