2017-08-07 74 views
2

說我有此數據幀:ggplo2在R:geom_segment顯示不同的線比geom_line

treatment <- c(rep("A",6),rep("B",6),rep("C",6),rep("D",6),rep("E",6),rep("F",6)) 
year <- as.numeric(c(1999:2004,1999:2004,2005:2010,2005:2010,2005:2010,2005:2010)) 
variable <- c(runif(6,4,5),runif(6,5,6),runif(6,3,4),runif(6,4,5),runif(6,5,6),runif(6,6,7)) 
se <- c(runif(6,0.2,0.5),runif(6,0.2,0.5),runif(6,0.2,0.5),runif(6,0.2,0.5),runif(6,0.2,0.5),runif(6,0.2,0.5)) 
id <- 1:36 
df1 <- as.data.table(cbind(id,treatment,year,variable,se)) 

df1$year <- as.numeric(df1$year) 
df1$variable <- as.numeric(df1$variable) 
df1$se <- as.numeric(df1$se) 

正如我在以前的問題(draw two lines with the same origin using ggplot2 in R)提到的,我想用GGPLOT2在一個特定的顯示我的數據辦法。

我設法做到使用下面的腳本:

y1 <- df1[df1$treatment=='A'&df1$year==2004,]$variable 
y2 <- df1[df1$treatment=='B'&df1$year==2004,]$variable 
y3 <- df1[df1$treatment=='C'&df1$year==2005,]$variable 
y4 <- df1[df1$treatment=='D'&df1$year==2005,]$variable 
y5 <- df1[df1$treatment=='E'&df1$year==2005,]$variable 
y5 <- df1[df1$treatment=='E'&df1$year==2005,]$variable 
y6 <- df1[df1$treatment=='F'&df1$year==2005,]$variable 

p <- ggplot(df1,aes(x=year,y=variable,group=treatment,color=treatment))+ 
geom_line(aes(y = variable, group = treatment, linetype = treatment, color = treatment),size=1.5,lineend = "round") + 
scale_linetype_manual(values=c('solid','solid','solid','dashed','solid','dashed')) + 
geom_point(aes(colour=factor(treatment)),size=4)+ 
geom_errorbar(aes(ymin=variable-se,ymax=variable+se),width=0.2,size=1.5)+ 
guides(colour = guide_legend(override.aes = list(shape=NA,linetype = c("solid", "solid",'solid','dashed','solid','dashed')))) 

p+labs(title="Title", x="years", y = "Variable 1")+ 
    theme_classic() + 
scale_x_continuous(breaks=c(1998:2010), labels=c(1998:2010),limits=c(1998.5,2010.5))+ 
    geom_segment(aes(x=2004, y=y1, xend=2005, yend=y3),colour='blue1',size=1.5,linetype='solid')+ 
    geom_segment(aes(x=2004, y=y1, xend=2005, yend=y4),colour='blue1',size=1.5,linetype='dashed')+ 
    geom_segment(aes(x=2004, y=y2, xend=2005, yend=y5),colour='red3',size=1.5,linetype='solid')+ 
    geom_segment(aes(x=2004, y=y2, xend=2005, yend=y6),colour='red3',size=1.5,linetype='dashed')+ 
    scale_color_manual(values=c('blue1','red3','blue1','blue1','red3','red3'))+ 
    theme(text = element_text(size=12)) 

正如你可以看到我同時使用geom_line和geom_segment以顯示我的圖線。

figure

這幾乎是完美的,但如果你仔細觀察,要繪製的(2004年和2005年之間)的部分不顯示在同一行的大小,儘管我用的腳本相同的參數值(即size=1.5linetype='solid'dashed)。

當然,我可以手動更改段的大小以獲得相似的線條,但是當我這樣做時,線段不如使用geom_line的線條那樣流暢。 另外,通過在aes()參數中包含sizelinetype參數,我會遇到同樣的問題(不同的線條形狀)。

你有什麼想法是什麼導致了這種差異,我怎麼能得到完全相同的形狀爲我的段和線?

+0

我覺得這些線條有相同的大小。這似乎是光柵化算法的一個問題。由於隨機抽樣得到的數據不同,這個問題似乎發生在2006年至2007年,其中只使用了'geom_line'。 – Marcelo

+0

事實上,當我刪除'geom_segment'部分中的'顏色'參數時,顏色發生了變化...... 但是我不明白你對2006年到2007年的評論。數據框的構建方式,應該應用geom_line範圍從1999年到2004年和/或從2005年到2010年,對嗎? –

+0

也許一個解決方案會添加一個帶有'override.aes'類似參數的線段,就像圖例一樣?如果這樣做有道理...... –

回答

1

它似乎是一個geom_segment反鋸齒問題,但這似乎是一個有點麻煩的方法開始。我想我已經通過在原始數據框中複製AB治療來解決您的問題。

# First we are going to duplicate and rename the 'shared' treatments 
library(dplyr) 
library(ggplot2) 

df1 %>% 
    filter(treatment %in% c("A", "B")) %>% 
    mutate(treatment = ifelse(treatment == "A", 
          "AA", "BB")) %>% 
    bind_rows(df1) %>% # This rejoins with the original data 
    # Now we create `treatment_group` and `line_type` variables 
    mutate(treatment_group = ifelse(treatment %in% c("A", "C", "D", "AA"), 
            "treatment1", 
            "treatment2"), # This variable will denote color 
     line_type = ifelse(treatment %in% c("AA", "BB", "D", "F"), 
          "type1", 
          "type2")) %>% # And this variable denotes the line type 

# Now pipe into ggplot 
    ggplot(aes(x = year, y = variable, 
      group = interaction(treatment_group, line_type), # grouping by both linetype and color 
      color = treatment_group)) + 
    geom_line(aes(x = year, y = variable, linetype = line_type), 
      size = 1.5, lineend = "round") + 
    geom_point(size=4) + 
    # The rest here is more or less the same as what you had 
    geom_errorbar(aes(ymin = variable-se, ymax = variable+se), 
       width = 0.2, size = 1.5) + 
    scale_color_manual(values=c('blue1','red3')) + 
    scale_linetype_manual(values = c('dashed', 'solid')) + 
    labs(title = "Title", x = "Years", y = "Variable 1") + 
    scale_x_continuous(breaks = c(1998:2010), 
        limits = c(1998.5, 2010.5))+ 
    theme_classic() + 
    theme(text = element_text(size=12)) 

它會給你以下enter image description here

我的號碼是不同的,因爲他們是隨機生成的。

然後,您可以根據自己的喜好修改圖例,但我的建議是使用類似geom_label的東西,然後一定要設置check_overlap = TRUE

希望這會有所幫助!

+0

輸出正是我所期待的,非常感謝!另外我在這裏學到了一些東西: 我不知道你可以使用'df1%>%'然後寫下ggplot的腳本來直接運行它。 這對於僅僅爲了這個目的(或任何其他目的)改變我的數據框的部分也是非常有用的,因爲當我使用'View(df1)'之後,治療仍然是'A'和'B',而不是' AA'和'BB'。再次感謝。 –

+0

快速問題,但我不明白你爲什麼用'AA'替換'A'和'B'。我理解腳本的方式,'treatment1'等於'A' +'C' +'D' +'AA',但爲什麼這比僅僅'A' +'C' +'D'更方便? –

+0

當然可以。最終,我並沒有用'AA'和'BB'替換'A'和'B',而是將它們複製,然後將它們與未改變的原始數據(這就是'bind_rows'參數所做的)組合起來。我隨意使用了'AA'和'BB',這樣我就可以輕鬆地命名線型分組和顏色分組。目前我的'ggplot'的功能是從1999年到2010年繪製完整的實線,然後是從1999年到2010年的完整虛線。 –

相關問題