I am doing a funnel plot of proportion in metafor
in R
with color coding based on three different Year categories ( blue, red, and orange).
I want to plot untransformed proportion in the x-axis of the funnel plot with 3 year
subgroups legend and colors.
The prior post helped me in creating this question but did not solve all points yet.
In the following code, I can see only 2 colors (categories) and the same case happened in my actual dataset.
#Here is my code and data sample:
d<-read.table(text="ID study year d.asp n.asp d.plac n.plac
1 MRC-1 1974 49 615 67 624
2 CDP 1976 44 758 64 771
3 MRC-2 1979 102 832 126 850
4 GASP 1979 32 317 38 309
5 PARIS 1980 85 810 52 406
6 AMIS 1980 246 2267 219 2257
7 ISIS-2 1988 1570 8587 1720 8600
8 IMRC-2 1973 12 888 176 8950
9 IGASP 1972 12 399 48 209
10 IPARIS 1970 65 899 12 706
11 IIMRC-1 1973 19 315 27 124", sep="", header=T)
d$year_3gps<-"Before 1973"
d$year_3gps[ d$year >=1973 & d$year <1980 ]<-"Year 1973 - 1979"
d$year_3gps[ d$year >=1980 ]<-"Year 1980 or after"
addmargins(table(d$year_3gps))
m1 <- rma(xi=d.plac, ni=n.plac, data=d, measure="PLO",
#slab=paste(author, year, sep=", "),
method="FE")
# Create a vector of colors
my_colors <- c("blue", "red","orange4")[(d$year_3gps == "Before 1973") + 1]
# Create funnel plot, catching output
funnelplotdata <- funnel(m1, atransf = exp, ylab="Standered error", xlab="Proportion")
legend(-4.1, 0, c("Before 1973","Year 1973 - 1979", "Year 1980 or after" ),
fill = c("blue", "red","orange4"))
# Plot over points
with(funnelplotdata, points(x, y, col = my_colors, pch = 19))
Here is the resulting plot that is missing the orange dots although they are not under the legend:
Your color vector is incorrect, try this:
# Create a vector of colors
my_colors <- c("blue", "red", "orange4")[as.factor(d$year_3gps)]