2017-03-24 46 views
0

基於以下數據框和圖,我想在did.it=="y"條件下將數據點的顏色更改爲黑色。然而,點的形狀和線的顏色應保持不變。我怎樣才能做到這一點?帶連接點的散點圖中的數據點的條件格式化

set.seed(4887) 
Strain <- rep(c(rep("A", times = 2), rep("B", times = 4)), times = 2) 
Sex_ID <- rep(c("M_1", "F_2", "M_3", "F_4", "M_5", "F_6"), times = 2) 
State <- rep(c("virgin", "mated", "expecting", "parent"), each = 6) 
Huddling <- runif(8, 1.5, 3.8) 
did.it<-rep(c("y","n","n"), times=8) 

d <- data.frame(Strain, Sex_ID, State, Huddling, did.it) 

library(tidyr) 
d <- d %>% 
    separate(Sex_ID, c('Sex', 'ID'), sep = '_') 

ggplot(d, aes(x = factor(State), y = Huddling, color = Sex, group = ID, shape = ID))+ 
    facet_grid(Strain ~ ., scales = 'free_y') + 
    geom_point(size = 3, position = position_dodge(width=0.3), show.legend = F) + 
    geom_line(size = 0.7, position = position_dodge(width=0.3)) + 
    scale_color_manual(values = c('red4', 'midnightblue')) + 
    scale_fill_manual(values = "white") + 
    scale_x_discrete(limits = c("virgin", "mated", "expecting", "parent"), 
        labels = c("Virgin", "Mated", "Expecting", "Parent")) + 
    labs(y = "Time huddling (s)", x = "Reproductive stage") + 
    theme_classic() + 
    theme(axis.line.x = element_line(color = "black", size = 1), 
     axis.line.y = element_line(color = "black", size = 1), 
     axis.text = element_text(size = 17), 
     axis.title = element_text(size = 19,face = "bold"), 
     legend.title = element_text(size = 17), 
     legend.text = element_text(size = 15), 
     plot.title = element_text(lineheight = .8, face = "bold",size = 22)) 

Here is the plot at the moment

回答

0

你得到一部分的方式有通過只是在做:

geom_point(size = 3, aes(color = did.it) ...) + 
... 
    scale_color_manual(values = c('red4', 'midnightblue', 'orange', 'black')) ... 

但這不留點顏色不變時did.itFALSE。所以:(加上你的額外主題發言)

d$point_col <- ifelse(d$did.it=='y', 'y', d$Sex) 

ggplot(d, aes(x = factor(State), y = Huddling, color = Sex, group = ID, shape = ID))+ 
    facet_grid(Strain ~ ., scales = 'free_y') + 
    geom_point(size = 3, aes(color = point_col), position = position_dodge(width=0.3), 
     show.legend = F) + 
    geom_line(size = 0.7, position = position_dodge(width=0.3)) + 
    scale_color_manual(values = c('red4', 'midnightblue', 'black')) + 
    scale_fill_manual(values = "white") + 
    scale_x_discrete(limits = c("virgin", "mated", "expecting", "parent"), 
    labels = c("Virgin", "Mated", "Expecting", "Parent")) + 
    labs(y = "Time huddling (s)", x = "Reproductive stage") 

+0

這樣的作品,非常感謝! –