r - Adding a scatterplot to a coefficient plot in ggplot -


i trying create coefficient plot scatterplot of data points laid overtop of it. portion of data listed below:

print(dat)                     lower    upper      mode    condition   obsmean p100 p102 p103 p104 p105 ob.h.black.650  0.693595 0.950305 0.8849853  h.black.650 0.8122302 0.85 1.00 0.85 0.85 0.85 ob.h.white.650  0.649625 0.930375 0.8306119  h.white.650 0.7852518 0.70 0.95 0.80 0.80 0.90 ob.h.black.800  0.833255 0.993105 0.9701335  h.black.800 0.9035971 0.95 1.00 0.95 0.95 1.00 ob.h.white.800  0.802110 0.983230 0.9469604  h.white.800 0.8762590 0.95 0.95 0.80 1.00 0.85 ob.h.black.950  0.869375 0.995800 0.9804180  h.black.950 0.9165468 0.90 1.00 0.85 1.00 1.00 ob.h.white.950  0.862895 0.991750 0.9694811  h.white.950 0.9046763 1.00 1.00 0.95 1.00 0.90 ob.h.black.1100 0.879340 0.996550 0.9885382 h.black.1100 0.9323741 1.00 1.00 0.90 0.75 0.85 ob.h.white.1100 0.853110 0.994405 0.9773240 h.white.1100 0.9140288 0.95 0.95 1.00 0.90 0.90 

my code produces output shown below:

ggplot(dat, aes(x = reorder(condition, 1:8), y = mode)) + #x cond, y mode   geom_point(size = 4) + #add in black dots modes   geom_pointrange(aes(ymin = lower, ymax = upper)) + #add in 95% hdi bars   geom_point(aes(x = reorder(condition, 1:8), y = obsmean), size = 4, colour = "red") +   geom_point(aes(x = reorder(condition, 1:8), y = p100), #x cond, y ps mean     position = position_jitter(width = .2, height = 0), #jitter data point     size = 2, colour = "blue") +  #make small , blue   geom_point(aes(x = reorder(condition, 1:8), y = p102), #x cond, y ps mean     position = position_jitter(width = .2, height = 0), #jitter data point     size = 2, colour = "blue") +  #make small , blue   geom_point(aes(x = reorder(condition, 1:8), y = p103), #x condition, y ps mean     position = position_jitter(width = .2, height = 0), #jitter data point     size = 2, colour = "blue") +  #make small , blue   geom_point(aes(x = reorder(condition, 1:8), y = p104), #x condition, y ps mean     position = position_jitter(width = .2, height = 0), #jitter data point     size = 2, colour = "blue") +  #make small , blue   geom_point(aes(x = reorder(condition, 1:8), y = p105), #x condition, y ps mean     position = position_jitter(width = .2, height = 0), #jitter data point     size = 2, colour = "blue") +  #make small , blue   coord_cartesian(ylim = c(.6, 1)) + #scale graph .6 1   ylab("hit rate") + #add y axis label   theme_bw() +  #use black , white theme   theme(axis.title.x = element_blank(), #get rid of x-axis label     panel.background = element_blank(), #get rid of background     panel.grid = element_blank(), #get rid of gridlines     panel.border = element_rect(colour = "black", fill = na, size = 1))  #add border 

which produces plot.

enter image description here

as can see, code inefficient. each set of points want overlay on condition mean , confidence interval, have enter line of code. 139 repetitions in full dataset, that's particularly cumbersome. wondering if there more efficient way of overlaying individual repetitions on mean , cis.

as mentioned in comments, should reshape data long form. can done either reshape2, data.table or tidyr:

library(reshape2) dat2 <- melt(dat, measure.vars = c("p100","p102","p103","p104","p105"))  library(data.table) # method 1 (equivalent reshape2 method) dat2 <- melt(setdt(dat), measure.vars = c("p100","p102","p103","p104","p105")) # method 2 dat2 <- melt(setdt(dat), measure.vars = patterns("^p"))  library(tidyr) dat2 <- dat %>% gather(variable, value, c(p100,p102,p103,p104,p105)) 

next, set factorlevels of condition in correct order:

dat2$condition <- factor(dat2$condition, levels = c("h.black.650","h.white.650","h.black.800","h.white.800",                                                     "h.black.950","h.white.950","h.black.1100","h.white.1100")) 

finally, can simplify plotting code follows:

ggplot(dat2, aes(x = condition, y = mode)) +    geom_point(size = 4) +    geom_pointrange(aes(ymin = lower, ymax = upper)) +    geom_point(aes(x = condition, y = obsmean), size = 4, colour = "red") +   geom_point(aes(x = condition, y = value),               position = position_jitter(width = .2, height = 0),               size = 2, colour = "blue") +    coord_cartesian(ylim = c(.6, 1.01)) +   ylab("hit rate") +   theme_bw() +   theme(axis.title.x = element_blank(),          panel.background = element_blank(),          panel.grid = element_blank(),         panel.border = element_rect(colour = "black", fill = na, size = 1)) 

which results in following plot:

enter image description here

furthermore, might consider coloring blue points p-variables:

ggplot(dat2, aes(x = condition, y = mode)) +    geom_point(size = 4) +    geom_pointrange(aes(ymin = lower, ymax = upper)) +    geom_point(aes(x = condition, y = obsmean), size = 4, colour = "red") +   geom_point(aes(x = condition, y = value, color = variable),               position = position_jitter(width = .2, height = 0), size = 2) +    coord_cartesian(ylim = c(.6, 1.01)) +   ylab("hit rate") +   theme_bw() +   theme(axis.title.x = element_blank(),          panel.background = element_blank(),          panel.grid = element_blank(),         panel.border = element_rect(colour = "black", fill = na, size = 1)) 

this results in:

enter image description here


Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -