vanao veneri
vanao veneri

Reputation: 1054

How can I draw a line with specific coordinates in ggplot?

This is a tricky one because I am bending the rules of ggplot a bit.

Please have look at the following example. I would like to connect each pair of black and gray rects with a line (center to center).

I tried with geom_path(). See the commented-out line, which would produce an error message: "Error: Aesthetics must be either length 1 or the same as the data (6): x, y, colour, group"

I think geom_path() and geom_line() are probably not the right approach. I think I need something that draws me a line with specified coordinates, and then I just try to figure out a way to provide the coordinates (similar to the geom_rect). Is there a way?

#create dataframe 
set.seed(1)
N = 20
rownameorder <- c("C", "B", "A", "D", "E", "F")
datforgraph <- data.frame( 
    ID = rep(c(1001:1020), each=6), 
    CLASS = rep(20, N*6),
    TEST = rep(rownameorder, N), 
    T1 = round(rnorm(N*6, 50, 10)), 
    T2 = round(rnorm(N*6, 50, 10))
)
datforgraph$T1UPPER <- datforgraph$T1 + 5
datforgraph$T1LOWER <- datforgraph$T1 - 5
datforgraph$T2UPPER <- datforgraph$T2 + 5
datforgraph$T2LOWER <- datforgraph$T2 - 5
studentdata <- subset(datforgraph, ID==1002)

#create graphics
COLORS <- c("#FF9999", "#FFFF99", "#99FF99", "#99C199", "#9999FF")
RECTS <- data.frame(ystart = c(20,30,40,60,70), yend = c(30,40,60,70,80))
ggp <- ggplot() + 
    coord_cartesian(ylim=c(10,90), expand=FALSE) + 
    scale_x_discrete() + 
    scale_y_continuous() + 
    geom_blank(data=studentdata, aes(x=factor(TEST, rownameorder))) + 
    geom_rect(data=RECTS, aes(ymin=ystart, ymax=yend, xmin=0, xmax=7), fill=COLORS) + 
    geom_rect(data=studentdata, fill="black", alpha=0.99, color="black", size=0.1, aes(xmin=as.numeric(factor(TEST, rownameorder)) - 0.3, xmax=as.numeric(factor(TEST, rownameorder)) - 0.1, ymin=T1LOWER , ymax=T1UPPER)) + 
    geom_rect(data=studentdata, fill="darkgrey", alpha=0.99, color="black", size=0.1, aes(xmin=as.numeric(factor(TEST, rownameorder)) + 0.1, xmax=as.numeric(factor(TEST, rownameorder)) + 0.3, ymin=T2LOWER , ymax=T2UPPER)) + 
    #geom_path(data=studentdata, mapping=aes(x=c(as.numeric(factor(TEST, rownameorder)) - 0.3, as.numeric(factor(TEST, rownameorder)) + 0.3), y=c(T1, T2), color=factor(TEST, rownameorder), group=1)) + 
    theme_classic() + 
    theme(axis.line.y = element_blank(), axis.title.y = element_blank(), axis.ticks.y = element_blank(), axis.text.y = element_blank()) + 
    xlab("") + 
    ylab("")
ggp

Upvotes: 0

Views: 2457

Answers (1)

Lennyy
Lennyy

Reputation: 6132

ggp + 
geom_segment(data = studentdata,aes(x = c(0.9:5.9), y = T1, xend = c(1.1:6.1), yend = T2))

From your lines xmax=as.numeric(factor(TEST, rownameorder)) - 0.1 and aes(xmin=as.numeric(factor(TEST, rownameorder)) + 0.1 I extracted the appropriate xcoordinates of the segments, which should be 0.1 below and 0.1 above the integers.

enter image description here

Upvotes: 2

Related Questions