Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot中回避嵌套数据点_R_Ggplot2 - Fatal编程技术网

R 在ggplot中回避嵌套数据点

R 在ggplot中回避嵌套数据点,r,ggplot2,R,Ggplot2,我正在绘制12个数据点,它们嵌套在两个分组级别上,分别称为type和treatment。组类型包括选项多和少,而组治疗包括选项低和高。因此,四种组合中的每一种都会发生三次 该图的代码如下(需要包ggplot2): 您可以通过使用交互功能来实现这一点。与: ggplot(data = test2) + geom_point(aes(class, value, shape = type, group = interaction(treatment,type), colour=treatment)

我正在绘制12个数据点,它们嵌套在两个分组级别上,分别称为
type
treatment
。组
类型
包括选项
,而组
治疗
包括选项
。因此,四种组合中的每一种都会发生三次

该图的代码如下(需要包ggplot2):


您可以通过使用
交互
功能来实现这一点。与:

ggplot(data = test2) +
  geom_point(aes(class, value, shape = type, group = interaction(treatment,type), colour=treatment),
             stat = 'identity', position = position_dodge(width=0.5), size = 2.5) +
  geom_errorbar(aes(class, ymin=value-se, ymax=value+se, group = interaction(treatment,type), colour = treatment),
                stat = 'identity', position=position_dodge(width=0.5), width = 0.2) +
  labs(x="Class", y="Value") +
  scale_colour_manual(values=c("blue","red")) +
  scale_shape_manual(values=c(16,17)) +
  scale_x_discrete(limits=c("one", "five", "ten"), labels=c("One", "Five", "Ten")) +
  scale_y_continuous(expand=c(0.0,0.0), limits=c(8.25, 10.25), breaks=c(8.5,9,9.5,10), labels=c("8.5","9","9.5","10")) +
  theme_bw() +
  theme(legend.title=element_blank(),
        axis.title.x = element_text(vjust=0.1,face="bold", size=16),
        axis.text.x = element_text(vjust=0.1, size=14, angle=0),
        axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18),
        axis.text.y = element_text(size=14),
        panel.grid.minor=element_blank(), 
        panel.grid.major=element_blank(),
        panel.border = element_rect(size=2, colour = "black", fill=NA, linetype=1),
        plot.margin = unit(c(0.3,0.4,0.28,0.0),"lines"))
你会得到:


另一个选项是预先创建交互变量:

test2$treattype <- factor(interaction(test2$treatment,test2$type),
                          labels = c("few high","few low","many high","many low"))
您将得到一个只有一个图例的绘图:


以下选项是@jlhoward提供的解决方案的扩展,还处理了类“十”的错误条几乎不可读的事实,并集成了第二个选项中的交互变量:

test2$class <- with(test2, factor(class, levels=unique(class)))

ggplot(test2, aes(x=type, y=value))+
  geom_point(aes(shape=treattype, color=treattype), position=position_dodge(width=0.5), size=3)+
  geom_errorbar(aes(ymin=value-se, ymax=value+se, shape=treattype, color=treattype),
                width=0.2, position=position_dodge(width=0.5))+
  scale_colour_manual(values=c("blue","red","darkgreen","brown")) +
  scale_shape_manual(values=c(16,17,16,17)) +
  facet_wrap(~class, scales="free_y")+
  theme_bw() +
  theme(legend.key = element_rect(colour=NA)) +
  guides(colour = guide_legend(title = "treatment x type",
                               override.aes = list(colour = c("blue","red","darkgreen","brown"),
                                                   shape = c(16,17,16,17))),
         shape = FALSE)

<代码> Test2 $类> P>不完全是你所要求的,我知道,但是我强烈建议你考虑这个方面。这就不那么令人困惑了(你可以避开这两个传说)

库(ggplot2)

test2$class非常感谢您的快速回复和大量提示。小调整,我希望有几个高,几个低,许多高和许多低的顺序,理想情况下,两个“少数”点和两个“许多”点之间的差距稍大一些。我可以同样地使用交互函数来生成点的顺序吗?许多的thanks@tabtimm是的,那是可能的。您只需更改
交互
函数中两个变量的顺序即可。查看更新的答案。@tabtimm要获得更大的间距,可以增加减淡宽度(例如,增加到
0.8
),但这也会增加高点和低点之间的宽度。后者是使用
交互
功能的结果。@tabtimm我在我的答案中添加了另外两个选项。感谢你们两位提供的非常有用的建议。我喜欢关于面和单个传奇的想法。谢谢
test2$treattype <- factor(interaction(test2$treatment,test2$type),
                          labels = c("few high","few low","many high","many low"))
ggplot(data = test2) +
  geom_point(aes(class, value, shape = treattype, colour = treattype),
             stat = 'identity', position = position_dodge(width=0.5), size = 3) +
  geom_errorbar(aes(class, ymin=value-se, ymax=value+se, colour = treattype),
                stat = 'identity', position=position_dodge(width=0.5), width = 0.2) +
  labs(x="Class", y="Value") +
  scale_x_discrete(limits=c("one", "five", "ten"), labels=c("One", "Five", "Ten")) +
  scale_y_continuous(expand=c(0.0,0.0), limits=c(8.25, 10.25), breaks=c(8.5,9,9.5,10), labels=c("8.5","9","9.5","10")) +
  scale_colour_manual(values=c("blue","red","blue","red")) +
  scale_shape_manual(values=c(16,17,16,17)) +
  theme_bw() +
  theme(legend.title=element_blank(),
        axis.title.x = element_text(vjust=0.1,face="bold", size=16),
        axis.text.x = element_text(vjust=0.1, size=14, angle=0),
        axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18),
        axis.text.y = element_text(size=14),
        panel.grid.minor=element_blank(), 
        panel.grid.major=element_blank(),
        panel.border = element_rect(size=2, colour = "black", fill=NA, linetype=1),
        plot.margin = unit(c(0.3,0.4,0.28,0.0),"lines"))
test2$class <- with(test2, factor(class, levels=unique(class)))

ggplot(test2, aes(x=type, y=value))+
  geom_point(aes(shape=treattype, color=treattype), position=position_dodge(width=0.5), size=3)+
  geom_errorbar(aes(ymin=value-se, ymax=value+se, shape=treattype, color=treattype),
                width=0.2, position=position_dodge(width=0.5))+
  scale_colour_manual(values=c("blue","red","darkgreen","brown")) +
  scale_shape_manual(values=c(16,17,16,17)) +
  facet_wrap(~class, scales="free_y")+
  theme_bw() +
  theme(legend.key = element_rect(colour=NA)) +
  guides(colour = guide_legend(title = "treatment x type",
                               override.aes = list(colour = c("blue","red","darkgreen","brown"),
                                                   shape = c(16,17,16,17))),
         shape = FALSE)
library(ggplot2)
test2$class <- with(test2,factor(class, levels=unique(class)))
ggplot(test2, aes(x=type, y=value, color=treatment))+
  geom_point(position=position_dodge(width=0.5))+
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=0.1, 
                position=position_dodge(width=0.5))+
  facet_wrap(~class)+
  theme_bw()