Reputation: 11
I am using a segmented line plot and want to group variables on the y-axis based on a factor (in this case Patient ID). How can I change the width of the ticks on the y so that a patient is grouped by his/her ID, and only one label is given for each unique ID?
An example of my data and plot is below.
ggplot(data) +
geom_segment(aes(x=age1, xend=age2,
y=PatientID, yend=PatientID, colour=mortality)) +
scale_colour_manual(values=c("green", "red", "black"))
Data:
PatientID age1 age2 mortality
11313 0 30 low
11313 31 50 low
11313 51 65 med
11313 0 10 med
11313 0 50 hi
131NY 0 30 med
143CA 24 27 hi
165099 23 45 med
165099 46 55 hi
165099 40 55 med
Upvotes: 1
Views: 1847
Reputation: 7997
I have used the sample data provided but the output seems to be similar to the desired output as described in the question. Patients are grouped by ID and have only one label each (see 11313
below). Am I missing something?
library(ggplot2)
mytext <- "PatientID,age1,age2,mortality
11313,0,30,low
11313,31,50,low
11313,51,65,med
131NY,0,30,med
143CA,24,27,hi
165099,23,45,med
165099,46,55,hi"
dat <- read.table(textConnection(mytext), sep = ",",
check.names = FALSE,
strip.white = TRUE,
header = TRUE)
ggplot(dat) +
geom_segment(aes(x = age1, xend = age2,
y = PatientID, yend = PatientID, colour = mortality)) +
scale_colour_manual(values = c("green", "red", "black"))
Upvotes: 1