调整ggplot2的代码

时间:2017-08-22 17:34:37

标签: r ggplot2

我正在使用以下代码绘制一系列效果大小估算值 -

Fig1 <- ggplot(dat1, aes(x=x, y= value, colour = time, group = variable))+
  geom_point(aes(shape = variable),size = 1.75, position = position_quasirandom()) +
  geom_line(data=dat1[!is.na(dat1$value),], aes(colour = NULL, group = NULL)) +
  scale_shape_manual(values = 0:20) +
  scale_y_continuous(limits=c(-0.50,.00), breaks=seq(-0.50,.00, by = .02)) +
  geom_hline(yintercept = 0, linetype=2) +
  coord_flip() +
  xlab('Distribution') +
  ylab('Effect size')

这会创建一个如下所示的图:

enter image description here

我想调整代码,以便绘制的值在不同的高度偏移(在线上方),具体取决于它们与其他值的接近程度。例如,对于此图中的第一行,菱形和正方形将保留在它们当前所在的线上,但倒置的三角形,圆形,垂直线和星号将从线上抬起。它看起来大致如下:

enter image description here

有没有人知道这是否可以使用ggplot2或是否有另一个R包允许我们这样做?如果是这样,任何人都可以建议一种方法来调整代码,以便根据它们与其他估算的接近程度来减少估计值吗?

可重复的例子:

### Installing of needed packages
if (!require("pacman")) install.packages("pacman") 
library(pacman)
pacman::p_load(ggplot2, reshape2, dplyr, magrittr, ggbeeswarm, beepr, readxl)

#data
data<-data.frame(x = "Activity", 
                 mean.b = .12, 
                 osrmin.b = .11, 
                 osrmax.b = .13, 
                 osrmed.b = .12, 
                 TFFE.b = .05, 
                 TFRE.b = .1, 
                 smm.b = .0, 
                 sms.b = .07, 
                 CMA.b = .09, 
                 Petpeese.b = .09, 
                 mean.a = .11, 
                 osrmin.a = .09, 
                 osrmax.a = .10, 
                 osrmed.a = .10, 
                 TFFE.a = .11, 
                 TFRE.a = .05, 
                 smm.a = .07, 
                 smr.a = .09, 
                 CMA.a = .10, 
                 Petpeese.a = .11)


# normalize the data (i.e., convert into 'long' form)
dat1 = melt(data, id.vars = "x")

# Set x factor order in order that appears in data
dat1$x = factor(dat1$x, levels = unique(dat1$x))

dat1 %<>% mutate(time = gsub(".*\\.", "", variable),
                 variable = gsub("\\..*", "", variable))


# Begin plotting
Fig1 <- ggplot(dat1, aes(x=x, y= value, colour = time, group = variable))+
  geom_point(aes(shape = variable),size = 1.75) +
  geom_line(data=dat1[!is.na(dat1$value),], aes(colour = NULL, group = NULL)) +
  scale_shape_manual(values = 0:20) +
  scale_y_continuous(limits=c(0,.2), breaks=seq(0,.2, by = .02)) +
  geom_hline(yintercept = 0, linetype=2) +
  coord_flip() +
  xlab('Distribution') +
  ylab('Effect size')

Fig1 <- Fig1 + 
  theme_bw() + 
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(), 
        axis.line.x = element_line(colour = "black"),
        axis.line.y = element_line(colour = "black"),
        axis.text.x = element_text(size = 6),
        axis.text.y = element_text(size = 6),
        axis.title = element_text(size=6),
        legend.position = ("none"),
        legend.title=element_blank(),
        legend.text=element_text(size=6),
        legend.key = element_rect(colour = NA))

Fig1 + guides(colour = guide_legend(nrow = 3, byrow = TRUE)) +
  guides(shape=guide_legend(ncol=3,byrow=TRUE))

Fig1

0 个答案:

没有答案