ggplot2 - 使用拆分框图对齐geom_point

Kez*_*Kez 4 r ggplot2 boxplot

我是R的新手并且一直在使用谷歌(主要是指导我到这个网站)来摸索我为项目制作可通过的图表.我无法找出要搜索的内容,找到与我有同样问题的其他人,所以我决定提出要求.

我有一个看起来像这样的数据集:

    ATM TEMP PARENT variable value
1     1    5      1     DEAD     2
2     1    5      2     DEAD     0
3     1    5      3     DEAD     1
4     1   20      1     DEAD     1
55    1    5      1     LIVE    47
56    1    5      2     LIVE    42
57    1    5      3     LIVE    45
58    1   20      1     LIVE    45
109   1    5      1 SWIMMING     1
110   1    5      2 SWIMMING     8
111   1    5      3 SWIMMING     4
112   1   20      1 SWIMMING     4
Run Code Online (Sandbox Code Playgroud)

ATM代表压力实验,温度,温度,PARENT,幼虫来自3个成年人中的一个,变量代表在给定压力/温度下幼虫的状况,数值是多少(最初不同,但我使用reshape2合并它们.

我已经能够创建这个图:

在此输入图像描述

使用此代码:

qplot(factor(ATM), value, data = CONDITION, geom = "boxplot", fill = factor(TEMP)) +
geom_point(aes(colour=factor(TEMP)) +
facet_wrap(~ variable, ncol = 1) +
scale_fill_manual(values = c("lightblue","#FF6666")) +
scale_colour_manual(values = c("lightblue","#FF6666")) +
labs(title = "Effect of Pressure on Condition of C.fornicata Larvae") +
xlab("Pressure \n (atm)") +
ylab("Number of Larvae") +
guides(fill=guide_legend(title="Incubation Temp (°C)"),colour=guide_legend(title="Incubation Temp (°C)"))
Run Code Online (Sandbox Code Playgroud)

我的问题是fill=factor(TEMP)将箱形图分成两个(我想要的),但是来自geom_point的点不与现在偏移的箱形图对齐.我试过搞乱geom_point中的位置参数,但没有运气.

提前致谢!

kda*_*ria 5

看看?position_dodge.

# Making the example data set bigger    
library(ggplot2)
x = read.table(text='ATM TEMP PARENT variable value
1     1    5      1     DEAD     2
2     1    5      2     DEAD     0
3     1    5      3     DEAD     1
4     1   20      1     DEAD     1
55    1    5      1     LIVE    47
56    1    5      2     LIVE    42
57    1    5      3     LIVE    45
58    1   20      1     LIVE    45
109   1    5      1 SWIMMING     1
110   1    5      2 SWIMMING     8
111   1    5      3 SWIMMING     4
112   1   20      1 SWIMMING     4')
x = rbind(x,x)
x$ATM[13:24] = 50

# Example code that moves the points to the middle of the boxplots
ggplot( aes(x=factor(ATM),y=value), data=x ) +
  geom_boxplot( aes(fill=factor(TEMP))) +
  geom_point( aes(color=factor(TEMP)), 
              position=position_dodge(width=0.75) )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述