2015-03-02 26 views
0

,我有以下數據:刪除和編輯雙圖例文本GGPLOT2

metric_list <- c(0.6, 0.2, 0.1, 0.05) 
terms_used = c("a", "b", "c", "d") 

,我使用下面的代碼來獲得一個情節:

df <- data.frame(x = 1:length(metric_list), 
         terms_used = terms_used, 
         metric_list = 100*metric_list, 
         cumulative = cumsum(100*metric_list)) 
gplot <- ggplot(data = df, aes(x = x)) + 
      geom_point(aes(y = metric_list, 
          color = "ERR"), 
         size = 3) + 
      geom_text(aes(y = metric_list, 
          label = terms_used), 
         size = 6, 
         hjust = -1, 
         vjust = 0) + 
      geom_line(aes(y = cumulative, 
          color = "ESR", 
          linetype = "ESR"), 
         size = 1) + 
      geom_point(aes(y = cumulative, 
          color = "ESR", 
          shape = "ESR"), 
         size = 3) + 
      scale_colour_manual(name = "Legend", 
           values = c("ERR" = "blue", "ESR" = "red")) + 
      scale_linetype_manual(name = "Legend", 
            values = c("ERR" = 0, "ESR" = "dashed")) + 
      scale_shape_manual(name = "Legend", 
           values = c("ERR" = 1, "ESR" = 0)) 

我得到的是以下幾點: ggplot2 plot

我想知道如何修改的傳說,使得只有一個藍點(爲ERR),並用空心方形紅色線(用於ESR)出現。任何幫助是極大的讚賞。

回答

4

ggplot2在您的數據長而不是寬,格式時效果最佳。 (見整潔數據presentationpaper由哈德利韋翰,GGPLOT2的創造者。)因此,而不是:

> df 
    x terms_used metric_list cumulative 
1 1   a   60   60 
2 2   b   20   80 
3 3   c   10   90 
4 4   d   5   95 

你應該使用

> DF 
    x terms_used var_name val 
1 1   a  ERR 60 
2 2   b  ERR 20 
3 3   c  ERR 10 
4 4   d  ERR 5 
5 1     ESR 60 
6 2     ESR 80 
7 3     ESR 90 
8 4     ESR 95 

tidyr包可以用來重塑數據:

library(tidyr) # for gather 
library(plyr) # for revalue 
# Reshape from wide to long 
DF <- gather(df, var_name, val, c(metric_list, cumulative)) 
# change levels of factor (e.g. metric_list to ERR) 
DF$var_name <- revalue(DF$var_name, 
         c("metric_list"="ERR", "cumulative"="ESR")) 

然後,您可以創建這樣的情節:

# Remove letters beside red points on plot 
DF$terms_used <- as.character(DF$terms_used) 
DF$terms_used[DF$var_name=="ESR"] <- "" 

gplot <- ggplot(data = DF, 
       aes(x=x, y=val, group=var_name, colour=var_name, 
        linetype=var_name, shape=var_name)) + 
    geom_point() + 
    geom_text(aes(label=terms_used), 
      size = 6, 
      hjust = -1, 
      vjust = 0, 
      show_guide = FALSE) + 
    geom_line(size = 1) + 
    geom_point(size = 3) + 
    scale_colour_manual(values = c("blue", "red")) + 
    scale_linetype_manual(values = c("blank", "dashed")) + 
    scale_shape_manual(values = c(1,0)) 
gplot 

ggplot2 plot

+0

非常感謝您的時間和幫助!真的很好的解釋。使用ggplot時,我忘記了這個「良好實踐」。此外,感謝您分享的鏈接。 – jroberayalas 2015-03-02 19:16:43