首页 > 解决方案 > 使用 position_dodge() 的问题

问题描述

在使用颜色和形状时,我遇到了 position_dodge 的问题。

我想绘制一个实验的结果,在该实验中,在许多站点复制了两种治疗方法,并且我想以图形方式强调某些数据点。

由于 x 轴是一个因素,我使用 position_dodge 来分隔处理。到目前为止一切顺利,请参见下图 1。

但是,如果我想通过更改形状来强调特定数据点,请参见图 2。数据点现在已分为三列,而不是两列。

关于如何制作图表的任何建议,如下面的第三个面板所示。

site <- rep(c("site1"),times=6) 
treatment <- rep(c("one","two"),times=2,each=3) 
set.seed(21)
response <- c(rnorm(3,mean=4),
              rnorm(3,mean=5))

special <- as.factor(c(0,1,0,0,0,0))

mydata <- data.frame(site,treatment,response,special)

#graph 1
ggplot()+
  geom_point(data=mydata,
             aes(x = site,
                y = response,
                colour=treatment),
             size=4,
             position=position_dodge(1))

#graph 2
ggplot()+
  geom_point(data=mydata,
             aes(x = site,
                 y = response,
                 colour=treatment,
                 shape=special),
             size=4,
             position=position_dodge(0.5))

![示例](https://drive.google.com/open?id=1vpH4uYlqTp7J9Wr8Ma1fwuYrNFtegDdG)

标签: rggplot2

解决方案


推荐阅读