2017-09-27 34 views
1

我想在下圖中p得分大於0.1時將點顯示爲空心,但我無法弄清楚如何進行格式化一點上的p值有條件的:ggplot:基於未顯示的變量的條件格式點

structure(list(metric = structure(1:6, .Label = c("Viewers", 
"Views", "Chatters", "Chats", "Diamond Senders", "Diamonds"), class = "factor"), 
    group = c("Views", "Views", "Chats", "Chats", "Diamonds", 
    "Diamonds"), avg = c(-0.0264387458383021, -0.0515928340053015, 
    0.0097420053047867, 0.144967615821856, 0.347281593023006, 
    -0.25567948486292), err_low = c(-0.0664593483464582, -0.139178443719337, 
    -0.0595290104397667, -0.0490217193008488, -0.146892412146765, 
    -1.24468943017821), err_high = c(0.013581856669854, 0.0359927757087344, 
    0.0790130210493401, 0.33895695094456, 0.841455598192777, 
    0.733330460452365), p = c(0.277195141257042, 0.332587114951675, 
    0.817060323071668, 0.218997456845286, 0.247710490053607, 
    0.670666890004374), ord = c(1, 2, 3, 4, 5, 6)), .Names = c("metric", 
"group", "avg", "err_low", "err_high", "p", "ord"), row.names = c(NA, 
-6L), class = "data.frame") 

圖表可以使用GGPLOT2再現:

enter image description here

數據幀可以使用該代碼被再現

ggplot(datac, aes(x=metric, y=avg, color=group)) +geom_hline(yintercept = 0)+ 
     geom_label(aes(label=paste0(round(avg*100,2),"%")),nudge_x=-0.35, size=2.5) +geom_point(size=6)+ 
    geom_errorbar(position=position_dodge(.9), width=.25, aes(ymin=err_low, ymax=err_high)) + 
    theme_minimal()+ scale_color_manual(values=c("Views"="blue", "Chats"="orange","Diamonds"="dark green")) + 
    scale_y_continuous(labels = scales::percent,breaks=c(seq(-2,+2,by=0.2))) + labs(title="Change in Test vs. Control", 
     x="Experiment Metrics ", y = "% change") 

謝謝你的解決方案!

+1

一個快速和骯髒方式可以是添加一個額外的(白色,稍小)geom_point()層僅包括滿足您的標準的子集,例如add + geom_point(data = subset(dat,p> 1),color =「white」,size = 5) – caw5cv

回答

3

最簡單的方法是用所需的映射創建另一個因子列並將其應用於圖上。我用P> 0.5在實施例

library(dplyr) 
datac %>% 
    mutate(p1= factor(ifelse(p > 0.5, 1, 0)))%>% 
    ggplot(aes(x=metric, y=avg, color=group))+ 
    geom_hline(yintercept = 0)+ 
    geom_label(aes(label=paste0(round(avg*100,2),"%")),nudge_x=-0.35, size=2.5)+ 
    geom_point(size=6, aes(shape = p1))+ 
    geom_errorbar(position=position_dodge(.9), width=.25, aes(ymin=err_low, ymax=err_high))+ 
    theme_minimal()+ 
    scale_color_manual(values=c("Views"="blue", "Chats"="orange","Diamonds"="dark green")) + 
    scale_y_continuous(labels = scales::percent,breaks=c(seq(-2,+2,by=0.2)))+ 
    labs(title="Change in Test vs. Control", x="Experiment Metrics ", y = "% change")+ 
    scale_shape_manual("p > 0.5", values=c("0" = 1, "1" = 16)) 

enter image description here

+0

傑出 - 謝謝。 – metalaureate