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