简体   繁体   中英

Colour text in facet by legend color in R ggplot2

I have a sample dataframe:

dput(data)

structure(list(DAF = c(0.00704225, 0.00352113, 0.00352113, 0.028169, 
0.00352113, 0.00704225, 0.0105634, 0.00352113, 0.0105634, 0.00352113, 
0.00352113, 0.00352113, 0.0176056, 0.0140845, 0.00352113, 0.0140845, 
0.00352113, 0.0105634, 0.00352113, 0.00352113, 0.0140845, 0.00352113, 
0.084507, 0.00352113, 0.0669014, 0.00704225, 0.00352113, 0.00352113, 
0.00704225, 0.00352113, 0.00704225, 0.00352113, 0.00352113, 0.028169, 
0.00352113, 0.00704225, 0.0105634, 0.00352113, 0.0105634, 0.00352113, 
0.00352113, 0.00352113, 0.0176056, 0.0140845, 0.00352113, 0.0140845, 
0.00352113, 0.0105634, 0.00352113, 0.00352113, 0.0140845, 0.00352113, 
0.084507, 0.00352113, 0.0669014, 0.00704225, 0.00352113, 0.00352113, 
0.00704225, 0.00352113), TYPE = structure(c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("All SVs", "bDEL"), class = "factor"), 
Function = structure(c(2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 1L), .Label = c("Genic", "Intergenic"
), class = "factor")), .Names = c("DAF", "TYPE", "Function"
), class = "data.frame", row.names = c(NA, -60L))

I have plotted using"

p1<-ggplot(sv,aes(x=DAF,y=..density..,fill=Function))+geom_histogram(position="dodge",binwidth=0.02)+facet_wrap( ~ TYPE, scales = "free",ncol=2)

The annotated text from the dataframe:

> dput(dat)
structure(list(x = c(0.05, 0.05, 0.05, 0.05, 0.05, 0.05), y = c(20L, 
17L, 14L, 35L, 30L, 25L), labs = structure(c(3L, 4L, 6L, 1L, 
2L, 5L), .Label = c("mean=0.0173", "mean=0.0190", "mean=0.0415", 
"mean=0.0440", "p=0.0393", "p=1.47e-08"), class = "factor"), 
    TYPE = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("All SVs", 
    "bDEL"), class = "factor")), .Names = c("x", "y", "labs", 
"TYPE"), class = "data.frame", row.names = c(NA, -6L))


p2<-p1+geom_text(aes(x, y, label=labs),size=1,data=dat,inherit.aes = F)

which gives the following plot:

在此处输入图片说明

However, i would need to color the text "mean=..." inside each facet by the corresponding colour of the legends. ie in facet All SVs, 'mean=0.0415' should be in "Genic" color and "mean=0.0440' in 'Intergenic' color.

You'll need to add a column to "dat" to represent what group each row/label should be in. I used NA for the p-values.

dat$Function = c("Genic", "Intergenic", NA, "Genic", "Intergenic", NA)

Then you can map this variable to color in geom_text . You can avoid adding the NA to the legend by using show.legend = FALSE .

p1 + geom_text(aes(x, y, label = labs, color = Function), 
               size = 1, data = dat, inherit.aes = FALSE, show.legend = FALSE)

The NA color defaults to grey50 , which you could change to something else if needed in scale_color_discrete .

p1 + geom_text(aes(x, y, label = labs, color = Function), 
               size = 1, data = dat, inherit.aes = FALSE, show.legend = FALSE) +
    scale_color_discrete(na.value = "black")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM