visualize associations between two groups of data

前端 未结 4 980
清酒与你
清酒与你 2021-01-06 20:11

Where each datapoint has a pairing of A and B and there multiple entries in A and multiple entires in B. IE multiple syndromes and multiple diagnoses, although for each data

4条回答
  •  误落风尘
    2021-01-06 20:39

    This is what I do. A darker colour indicates a more important combination of A and B.

    dataset <- data.frame(A = sample(LETTERS[1:5], 200, prob = runif(5), replace = TRUE), B = sample(LETTERS[1:5], 200, prob = runif(5), replace = TRUE))
    Counts <- as.data.frame(with(dataset, table(A, B)))
    library(ggplot2)
    ggplot(Counts, aes(x = A, y = B, fill = Freq)) + geom_tile() + scale_fill_gradient(low = "white", high = "black")
    

    Or if you prefer lines

    library(ggplot2)
    dataset <- data.frame(A = sample(letters[1:5], 200, prob = runif(5), replace = TRUE), B = sample(letters[1:5], 200, prob = runif(5), replace = TRUE))
    Counts <- as.data.frame(with(dataset, table(A, B)))
    Counts$X <- 0
    Counts$Xend <- 1
    Counts$Y <- as.numeric(Counts$A)
    Counts$Yend <- as.numeric(Counts$B)
    ggplot(Counts, aes(x = X, xend = Xend, y = Y, yend = Yend, size = Freq)) +
    geom_segment() + scale_x_continuous(breaks = 0:1, labels = c("A", "B")) + 
    scale_y_continuous(breaks = 1:5, labels = letters[1:5])
    

    This third options add labels to the data points using geom_text().

    library(ggplot2)
    dataset <- data.frame(
        A = sample(letters[1:5], 200, prob = runif(5), replace = TRUE), 
        B = sample(LETTERS[20:26], 200, prob = runif(7), replace = TRUE)
    )
    Counts <- as.data.frame(with(dataset, table(A, B)))
    Counts$X <- 0
    Counts$Xend <- 1
    Counts$Y <- as.numeric(Counts$A)
    Counts$Yend <- as.numeric(Counts$B)
    ggplot(Counts, aes(x = X, xend = Xend, y = Y, yend = Yend)) + 
    geom_segment(aes(size = Freq)) + 
    scale_x_continuous(breaks = 0:1, labels = c("A", "B")) + 
    scale_y_continuous(breaks = -1) + 
    geom_text(aes(x = X, y = Y, label = A), colour = "red", size = 7, hjust = 1, vjust = 1) + 
    geom_text(aes(x = Xend, y = Yend, label = B), colour = "red", size = 7, hjust = 0, vjust = 0)
    

提交回复
热议问题