2017-08-31 29 views
1

點我在R代碼裏面的曲線圖的編碼方式類似於這樣:R代碼裏面線圖表示最高的y值

Graph<-ggplot(data=df, aes(x=Frame,y=Score)) + 
    geom_line() 
    geom_abline() 

請問有什麼可以添加到這個代碼,將創建一個標記,指示圖表上的點是數據的最高y值?數據如下所示:

Score Frame 
0.2  1 
3.6  2 
4.56  3 
0.3  4 
2.8  5 
1.7  6 
3.5  7 

感謝您的幫助。

+0

是的,肯定是有辦法。請分享可再現的數據樣本,以幫助我們回答。如果您已有數據集,可以將「dput(nameOfYourData)」的輸出複製/粘貼到上面的問題中。 – www

+0

由於有很多種方法可以做到這一點,它也將有助於在將來包含您想要輸出的樣子的描述或圖像。 – www

回答

1

下面的幾個的多種可能的方式:

require(ggplot2) 

df <- data.frame(
    Score=c(0.2,3.6,4.56,0.3,2.8,1.7,3.5), 
    Frame=c(1:7) 
) 
df$Max <- ifelse(df$Score==max(df$Score),"Yes","No") 

ggplot(data=df, aes(x=Frame,y=Score)) + 
    geom_point(aes(col=Max),size=2) + 
    geom_line() 

選項1個輸出:

enter image description here

或者你也可以使用類似:

ggplot(data=df, aes(x=Frame,y=Score)) + 
    geom_line() + 
    geom_hline(yintercept = max(df$Score, na.rm=TRUE), 
      color="red") 

選項2輸出:

enter image description here

0

一種可能性是使用尺寸審美如下:

Graph<-ggplot(data=df, aes(x=Frame,y=Score)) + 
    geom_point(aes(size=Score)) + 
    geom_line() 

enter image description here