2016-08-14 100 views
0

我正在製作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

回答

1

使用點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))) 
相關問題