1

首先是一些玩具数据:

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

但是我怎样才能为每个过渡添加点呢?

4

3 回答 3

0

要添加到@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()
于 2013-11-03T17:09:36.420 回答
0

一种选择,使用嵌套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

在此处输入图像描述

于 2013-11-03T16:41:28.017 回答
0

以下是一种略有不同的处理方式,您可能会也可能不会觉得有帮助。本质上,它为两个观察结果生成了一个组合变量。目前这仅适用于 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-11-04T01:54:26.050 回答