按特定值,按颜色分类和按组组织

时间:2019-08-01 10:19:32

标签: r ggplot2 facet

我要添加以下代码: 每个strip.background将由“组”列着色,并按组组织构面。 此外,将每个图分成一个在另一个上的两个图。脂质“ GD1a”的上图,y轴上具有千位数。其余脂质的底图,y轴上有数百个刻度。

谢谢

dat <- gather(dat, key="lipid", value="measurement", Total.GD1a:Total.GM3)
pdf("Lipidomics_graphs_per_patient.pdf", width=14, height=12)
ggplot(data=dat, aes(x=Brain.region, y=measurement)) +
  geom_point(aes(color=lipid)) +
  facet_wrap(~Sample.Name) +
  theme(axis.text.x = element_text(angle = 45))
dev.off()

我的数据中的一个小例子:

  Sample.Name Group   Brain.region          lipid      measurement
   <chr>       <chr>   <chr>                 <chr>            <dbl>
 1 P54/99      PD-GBA  Cingulate gyrus       Total.GD1a    3570.   
 2 P25/13      PD-GBA  Cingulate gyrus       Total.GM1      138.   
 3 P54/11      IPD     Cingulate gyrus       Total.GM1      188.   
 4 P23/98      PD-GBA  Striatum              Total.GM3        0.990
 5 P54/11      IPD     Occipital cortex      Total.GM3        3.32 
 6 P43/13      control Striatum              Total.GM1       77.9  
 7 P64/11      control Occipital cortex      Total.GD1a    2632.   
 8 P21/13      IPD     Middle temporal gyrus Total.GD2       76.5  
 9 P40/00      PD-GBA  Striatum              Total.GM1      102.   
10 P67/10      control Occipital cortex      Total.GM1      142.   
11 P6/08       IPD     Occipital cortex      Total.GM2        9.56 
12 P36/01      PD-GBA  Occipital cortex      Total.GM1      110.   
13 P25/01      PD-GBA  Cingulate gyrus       Total.GD2      174.   
14 P23/98      PD-GBA  Middle temporal gyrus Total.GM3        1.94 
15 P54/13      PD-GBA  Cingulate gyrus       Total.GD2      112.  

set.seed(1234)
dat_example <- dat %>% sample_n(15, replace = FALSE)
dput(dat_example)

structure(list(Sample.Name = c("C04/97", "P67/12", "P80/07", 
"P50/03", "P79/10", "P61/15", "P64/11", "P68/06", "P73/04", "P29/12", 
"P48/15", "P6/08", "P25/98", "59/94", "C04/97"), Group = c("control", 
"PD-GBA", "IPD", "IPD", "IPD", "IPD", "control", "PD-GBA", "PD-GBA", 
"control", "control", "IPD", "PD-GBA", "PD-GBA", "control"), 
    Brain.region = c("Occipital cortex", "Cingulate gyrus", "Cingulate gyrus", 
    "Occipital cortex", "Middle temporal gyrus", "Striatum", 
    "Striatum", "Striatum", "Occipital cortex", "Middle temporal gyrus", 
    "Striatum", "Occipital cortex", "Middle temporal gyrus", 
    "Striatum", "Middle temporal gyrus"), lipid = c("Total.GM3", 
    "Total.GD2", "Total.GD2", "Total.GD2", "Total.GD2", "Total.GD2", 
    "Total.GD2", "Total.GD1a", "Total.GD1a", "Total.GD2", "Total.GM2", 
    "Total.GM1", "Total.GM3", "Total.GM2", "Total.GD1a"), measurement = c(1.72785, 
    70.073, 97.289, 108.6805, 18.0735, 34.29075, 31.1428, 2589.50185, 
    2669.1055, 80.814, 3.24406869429729, 163.8335, 1.22105, 6.0281, 
    1273.784)), row.names = c(NA, -15L), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("Sample.Name", "Group", "Brain.region", 
"lipid", "measurement")) 

enter image description here

1 个答案:

答案 0 :(得分:0)

要获取基本图解,可以使用以下内容(假设df是示例数据):

g <- ggplot(df, aes(x = Brain.region, y = measurement)) +
  geom_point(aes(colour = lipid)) +
  facet_grid(vars(ifelse(lipid == "Total.GD1a", "GD1a", "Others")), 
             vars(Group),
             scales = "free_y") +
  scale_y_continuous(limits = c(0, NA)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
g

enter image description here

现在我们还没有条纹的颜色,这有点麻烦。以下是gtable方法:

# Convert to gtable
gt <- ggplotGrob(g)

# Get top strips
tstrip <- which(grepl("strip-t", gt$layout$name))
colours <- c("red", "green", "blue")

# Replace strip background fill
# Correct subset found through trial and error
for (i in seq_along(tstrip)) {
  gt$grobs[[tstrip[i]]]$grobs[[1]]$children[[1]]$gp$fill <- colours[i]
}

# Plot
library(grid)
grid.newpage(); grid.draw(gt)

enter image description here