我想对 geom_boxplot 做一些修改:
- 颠倒 Y 轴的顺序(A1 在顶部,A4 在底部)。
- 是否可以在图表上而不是在图表旁边绘制我的图例。
这是一个例子:
aa <- c(rep("A1",5), rep("A2",3), rep("A3",4), rep("A4",9))
aa <- as.factor(aa)
per <- runif(length(aa), min=0, max=100)
per <- trunc(per)
z <- data.frame(x=aa,y=per)
z$ch <- NA
z[z$x %in% c("A1","A2"), "ch"] <- "string1"
z[z$x %in% c("A3"), "ch"] <- "string2"
z[z$x %in% c("A4"), "ch"] <- "string3"
z$ch <- as.factor(z$ch)
p <- ggplot(z, aes(x, y, fill = ch)) +
geom_boxplot(size = 0.2, position = "dodge", outlier.colour = "red", outlier.shape = 16, outlier.size = 2) +
geom_jitter(size=1) + opts(legend.position = "right") +
scale_colour_hue("variable") +
coord_flip()
print(p)