如何在两点之间使用geom_hline

问题描述:

我正在制作Ramachandran情节,我想在两点之间划一条线。我使用此代码:如何在两点之间使用geom_hline

ggplot(result) + 
    scale_x_continuous(limits = c(-180,180), breaks = seq(-180,180,40), expand=c(0,0)) + 
    scale_y_continuous(limits = c(-180,180), breaks = seq(-180,180,40), expand=c(0,0)) + 
    geom_hex(aes(x, y), bins = 500) + 
    geom_vline(xintercept = 0, colour="red", linetype = "longdash") + 
    scale_fill_gradientn("", colours = rev(rainbow(10, end = 4/6))) + ylab(expression(paste(psi))) + xlab(expression(paste(phi))) 

而且我有这样的:

enter image description here

但我想提出另两条水平线这样的数字:

enter image description here

我用hline但不知道如何在两点之间定义它。

+0

在碱或晶格图形“线”两者被称为“段”。似乎确实有geom_segment gglot2函数。在[[r] ggplot2片段上搜索162个命中] –

+0

@ 42-是!.谢谢,我发现它是段的答案。 – Enrique

使用点geom_segment

df <- data.frame(x1 = 0, x2 = -180, y1 = 0, y2 = 0) #Data frame with the points 

    ggplot(result) + 
    scale_x_continuous(limits = c(-180,180), breaks = seq(-180,180,40), expand=c(0,0)) + 
    scale_y_continuous(limits = c(-180,180), breaks = seq(-180,180,40), expand=c(0,0)) + 
    geom_hex(aes(x, y), bins = 500) + 
    geom_vline(xintercept = 0, colour="red", linetype = "longdash") + 
    geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, colour = "segment"), data = df)+ 
    scale_fill_gradientn("", colours = rev(rainbow(10, end = 4/6))) + ylab(expression(paste(psi))) + xlab(expression(paste(phi)))