2017-08-09 336 views
0

我想用ggplot()绘制一个betadispers对象mod1,以便我可以更好地控制颜色。ggplot geom_point和geom_seg单独的颜色

我从mod1提取的重心,我使用geom_point()密谋每年重复每个沙丘,geom_seg()绘制每个沙丘星线,第二geom_point()声明绘制的重心。 当我绘制这个使用 scale_colour_manual(values=cols, guide= FALSE) 它只改变了第一个geom_points和geom_seg的颜色,但不是质心。

如何分别控制每个部件的颜色,使沙丘点着色为cols,部分着色为cols1,质心使用cols2? 我还想将每个质心的黑色轮廓的颜色更改为cols1

library(vegan) 
library(ggplot2) 

cols = c("blue","red") 
cols1 = c("green","dark orange") 
cols2 = c("purple","yellow") 

data(dune) 
sites = data.frame(year = rep(c(1:5), times= 4), dune = rep(c(1:4),each=5), dune.type = rep(c("A","B"),each=10)) 
distances <- vegdist(dune, method = "bray") 

#create Betadispersion model on betad (effectively a PCoA) 
mod1 <- with(sites, betadisper(distances, dune, type = "centroid")) 
s = scores(mod1) 

# Get points 
pnt_sites = as.data.frame(s$sites) 
pnt_sites = cbind(pnt_sites, sites) 

# Get centroids 
pnt_centroids = as.data.frame(s$centroids) 
pnt_centroids$dune = rownames(pnt_centroids) 
pnt_centroids$dune.type = rep(c("A","B"),each=2) 

# Calculate segments 
seg = pnt_sites[, c("PCoA1", "PCoA2", "dune")] 
tmp = rename(pnt_centroids, c("PCoA1" = "PCoA1_ctr", "PCoA2" = "PCoA2_ctr")) 
seg = join(seg, tmp, "dune") 

# Plot 
ggplot() + 
    geom_point(
    data = pnt_sites, 
    aes(x = PCoA1, y = PCoA2, colour = dune.type, shape = dune.type), 
    size = 2 
) + 
    geom_segment(
    data = seg, 
    aes(x = PCoA1, y = PCoA2, xend = PCoA1_ctr, yend = PCoA2_ctr, colour = 
     dune.type) 
) + 
    geom_point(
    data = pnt_centroids, 
    aes(x = PCoA1, y = PCoA2, fill = dune.type), 
    size = 3, shape = 21 
) + 
    scale_colour_manual(values=cols, guide= FALSE) + 
    coord_equal() + 
    theme_bw() 

回答

1

只能指定scale_colour_manual()每块一次,而不是每个geom_point呼叫多次,所以你需要你的重心和站点合并成一个数据帧(增加了对心/现场变量,质心A /心乙/站点A /位点B),然后绘制为单geom_point()

#combine centroids and points into one dataframe 
pnt_centroids$year = NA 
pnt_centroids$data.type = "centroid" 
pnt_sites$data.type = "site" 
sites_centroids <- rbind(pnt_centroids, pnt_sites) 
sites_centroids$type <- paste(sites_centroids$data.type, sites_centroids$dune.type) 

当定义的颜色矢量,以用于scale_fill_manualscale_colour_manual,每次都会有6级,以适应所具有的变量的数量( 4点类型加2段类型)。您的网站点和线段没有填充属性,因此在绘制这些点和线段时将忽略填充,但仍需要在scale_fill_manual中定义6种颜色,以便您的填充点可以正确绘制。

#change the cols vector definitions at the beginning of code to this 
cols.fill <- c("purple", "yellow", "purple", "yellow", "purple", "yellow") 
cols.colour <- c("green", "dark orange", "green", "dark orange", "blue", "red") 

指定新的颜色,填充和形状尺度的情节像这样的代码:

# Plot 
ggplot() + 
    geom_segment(#segment must go before point so points are in front of lines 
    data = seg, 
    aes(x = PCoA1, y = PCoA2, xend = PCoA1_ctr, yend = PCoA2_ctr, colour = dune.type)) + 
    geom_point(
    data = sites_centroids, 
    aes(x = PCoA1, y = PCoA2, colour = type, fill = type, shape = type), size = 2) + 
    scale_colour_manual(values = cols.colour) + 
    scale_fill_manual(values = cols.fill, guide = FALSE) + 
    scale_shape_manual(values = c(21, 21, 16, 17)) + 
    coord_equal() + 
    theme_bw() 

下面是结果。图例有点繁忙,删除它可能会更好,并使用文本注释来标记沙丘类型。

enter image description here