ggplot2:有没有办法将均值与箱形图对齐并对齐图例文本?

时间:2019-02-11 21:15:27

标签: r ggplot2

我是R的初学者,这是我第一次尝试使用ggplot。

我一直遇到问题:1.)如果箱形图的宽度保持恒定,则我的均值未与数据集对齐。 2)我想将图例文本向左对齐。我似乎找不到适合他们的功能。

Cellsize <- c(5.099, 6, 8, 9.01, 5, 4.323, 8.736, 6.84, 8.374, 9.21, 7.85, 4.323, 8.736, 7.84, 9.374)

Experiment <- c('Control', 'Control', 'Control', 'Genotype 1', 'Genotype 1', 'Genotype 1', 'Genotype 2', 'Genotype 2', 'Genotype 2', 'Genotype 1', 'Genotype 1', 'Genotype 1', 'Genotype 2', 'Genotype 2', 'Genotype 2')

DevAge <- c('30h', '30h', '30h', '30h', '30h', '30h', '30h', '30h', '30h', '48h', '48h', '48h', '48h', '48h', '48h')

df <- data.frame(Experiment, DevAge, Cellsize )
head(df, 15)

dfp<-ggplot(df, aes(x=DevAge, y=Cellsize, color=Experiment)) +
  geom_boxplot (outlier.fill = "red",outlier.color = "black",outlier.shape = 21, position = position_dodge2(preserve =c ("single"), padding= 0.2)) + 
  stat_summary(fun.y=mean, geom="point", shape=5, size=2, position=position_dodge(0.75)) + 
  scale_y_continuous(breaks = seq(0, 13, 2), limits = c (0, 13)) +
  theme_classic() + 
  labs(color= element_blank(), x="Developmental Age", y="Average cell size (\u03BCm)") + 
  scale_color_manual(values=c("black", "blue", "orange"),
                     labels =c ("Control", expression(italic("Genotype 1")), expression (italic ("Genotype 2")))
  ) 

dfp + theme(
  axis.title.x = element_text(size=16, family ="Arial", face="bold"), 
  axis.title.y = element_text(size=16, family ="Arial", face="bold"),
  axis.text.x = element_text(size=12, family ="Arial", face="bold", angle = 45, vjust = 0.5),
  axis.text.y = element_text(size=12, family ="Arial", face="bold"),
  legend.text = element_text(size=10, family ="Arial")
)

dfp

enter image description here

0 个答案:

没有答案