2017-08-09 95 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