按ggplot(小平面) [英] Multiple plots by factor in ggplot (facets)

查看:116
本文介绍了按ggplot(小平面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中包含两个定性变量(Q1,Q2),这两个变量都是在LOW,MEDIUM,HIGH和连续变量CV的范围内进行测量的,范围为0-100。

  s = 5 
trial < - data.frame(id = c(1:s),
Q1 = ordered c(LOW,MED,HIGH),size = s,replace = T)),
Q2 =有序(sample(c(LOW,MED,HIGH ),
CV = runif(s,0,100))

我需要使用ggplot为每个级别(x3)的每个定性变量(x2)显示连续变量的分面图(最好是水平boxplot / jitter)。这将导致3 x 2布局。



由于我对ggplot非常陌生,我不确定应如何实现。我已经玩过qplot,并且无法弄清楚如何控制facet在同一个图表上显示Q1和Q2箱型图。

我需要运行多个qplots到同一个窗口(在基础中,我将使用par来控制布局),还是可以通过单个命令实现。或者我应该尝试两次融合数据?

  trial = rbind(data.frame(Q =Q1,Level = trial [,2],CV = trial [ ,
data.frame(Q =Q2,Level = trial [,3],CV = trial [,4]))

我会继续尝试,并希望有人能够在此期间提供一些提示。 解决方案

  ggplot(trial, aes(Level,CV))+ 
geom_boxplot()+
geom_jitter()+
facet_wrap(〜Q)+
coord_flip()


I have a data frame with two qualitative variables (Q1, Q2) which are both measured on a scale of LOW, MEDIUM, HIGH and a continuous variable CV on a scale 0-100.

s = 5
trial <- data.frame(id = c(1:s), 
                Q1 = ordered(sample(c("LOW","MED","HIGH"),size=s,replace=T)), 
                Q2 = ordered(sample(c("LOW","MED","HIGH"),size=s,replace=T)), 
                CV = runif(s,0,100))

I need to use ggplot to show a faceted plot (preferably a horizontal boxplot/jitter) of the continous variable for each qualitative variable (x2) for each level (x3). This would result in a 3 x 2 layout.

As I'm very new to ggplot I'm unsure how this should be achieved. I've played with qplot and and can't work out how to control the facets to display both Q1 and Q2 boxplots on the same chart!!

Do I need to run multiple qplots to the same window (in base I would use par to control layout) or can it be achieved from a single command. Or should I try to melt the data twice?

trial = rbind(data.frame(Q = "Q1",Level = trial[,2], CV = trial[,4]),
          data.frame(Q = "Q2",Level = trial[,3], CV = trial[,4]))

I'll keep trying and hope somebody can provide some hints in the meantime.

解决方案

I'm not entirely clear on what you want, but maybe this helps:

ggplot(trial, aes(Level, CV)) + 
   geom_boxplot() + 
   geom_jitter() + 
   facet_wrap(~Q) + 
   coord_flip() 

这篇关于按ggplot(小平面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆