4

ggplot2我的数据通过带有几个(~10)方面的条形图在包中可视化。我想首先将这些方面分成几行。我可以使用功能facet_grid()facet_wrap()为此。在此处的最小示例数据中,我在两行 (4x2) 中构建了 8 个方面。但是我需要为不同的方面调整比例,即:第一行包含小规模的数据,第二行的值更大。所以我需要对第一行中的所有数据使用相同的比例以沿行比较它们,并为第二行使用另一个比例。

这是最小的示例和可能的解决方案。

#loading necessary libraries and example data
library(dplyr)
library(tidyr)
library(ggplot2)

trial.facets<-read.csv(text="period,xx,yy
A,2,3
B,1.5,2.5
C,3.2,0.5
D,2.5,1.5
E,11,13
F,16,14
G,8,5
H,5,4")

#arranging data to long format with omission of the "period" variable
trial.facets.tidied<-trial.facets %>% gather(key=newvar,value=newvalue,-period)

现在绘制自己:

#First variant
ggplot(trial.facets.tidied,aes(x=newvar,y=newvalue,position="dodge"))+geom_bar(stat ="identity") +facet_grid(.~period)

#Second variant:
ggplot(trial.facets.tidied,aes(x=newvar,y=newvalue,position="dodge"))+geom_bar(stat ="identity") +facet_wrap(~period,nrow=2,scales="free")

第一个和第二个变体的结果如下:

在此处输入图像描述

在这两个示例中,我们要么为所有图设置了自由比例,要么为所有图设置了固定比例。同时,第一行(前 4 个方面)需要稍微缩放到 5,第二行 - 到 15。

作为使用facet_grid()函数的解决方案,我可以添加一个假变量“row”,它指定对应的字母应该属于哪一行。新数据集 trial.facets.row(仅显示三行)如下所示:

period,xx,yy,row
C,3.2,0.5,1
D,2.5,1.5,1
E,11,13,2

然后我可以将相同的重新排列为长格式,省略变量“句​​点”和“行”:

trial.facets.tidied.2<-trial.facets.row %>% gather(key=newvar,value=newvalue,-period,-row)

然后我沿着变量“row”和“period”排列构面,希望使用该选项scales="free_y"来调整跨行的比例:

ggplot(trial.facets.tidied.2,aes(x=newvar,y=newvalue,position="dodge"))+geom_bar(stat ="identity") +facet_grid(row~period,scales="free_y")

并且 - 惊喜:比例的问题得到了解决,但是,我得到了两组空条,整个数据再次延伸到一条长条上:

在此处输入图像描述

所有发现的手册页和手册(通常使用 mpg 和 mtcars 数据集)都没有考虑这种不需要或虚拟数据的情况

4

3 回答 3

5

我结合了您的第一种方法(facet_wrap)和第二种方法(利用不同行的虚拟变量):

# create fake variable "row"
trial.facets.row <- trial.facets %>% mutate(row = ifelse(period %in% c("A", "B", "C", "D"), 1, 2))
# rearrange to long format
trial.facets.tidied.2<-trial.facets.row %>% gather(key=newvar,value=newvalue,-period,-row)
# specify the maximum height for each row
trial.facets.tidied.3<-trial.facets.tidied.2 %>%
  group_by(row) %>%
  mutate(max.height = max(newvalue)) %>%
  ungroup()

ggplot(trial.facets.tidied.3,
       aes(x=newvar, y=newvalue,position="dodge"))+
  geom_bar(stat = "identity") +
  geom_blank(aes(y=max.height)) + # add blank geom to force facets on the same row to the same height
  facet_wrap(~period,nrow=2,scales="free")

结果图

注意:基于这个可重现的示例,我假设您的所有图已经在 0 处共享一个共同的 ymin。如果不是这种情况,只需为 min.height 创建另一个虚拟变量并将另一个添加geom_blank到您的 ggplot 中。

于 2017-08-15T00:58:27.593 回答
3

回顾一下,我遇到了一个可能有点棘手的解决方案 -从这里

这个想法是创建第二个假数据集,该数据集将在每个方面绘制一个点。在每种情况下,该点将绘制在对应于 y 比例的最高期望值的位置。因此,可以为每个方面手动调整刻度的高度。这是相关数据集的解决方案。我们希望第一行的 y 比例(最大 y 值)为 5,第二行为 17。所以创建

df3=data.frame(newvar = rep("xx",8),    
               period = c("A","B","C","D","E","F","G","H"),
               newvalue = c(5,5,5,5,17,17,17,17))

现在使用 geom_point() 将新数据叠加到我们的图表上。

ggplot(trial.facets.tidied,aes(x=newvar,y=newvalue,position="dodge"))+
   geom_bar(stat ="identity") +
   facet_wrap(~period,nrow=2,scales="free_y")+
   geom_point(data=df3,aes(x=newvar,y=newvalue),alpha=1)

在这里我们得到:

带有假点的条形图,固定比例

在这里,我特意画了这个额外的点来说明问题。接下来我们需要让它不可见,这可以通过alpha=0在最后一个命令中设置而不是 1 来实现。

于 2017-08-15T01:19:26.610 回答
2

这种方法在每行的最大值处绘制一条不可见的线

#loading necessary libraries and example data
library(dplyr)
library(tidyr)
library(ggplot2)

trial.facets<-read.csv(text="period,xx,yy
                       A,2,3
                       B,1.5,2.5
                       C,3.2,0.5
                       D,2.5,1.5
                       E,11,13
                       F,16,14
                       G,8,5
                       H,5,4")

# define desired number of columns
n_col <- 4

#assign a row number - mmnsodulo number of colu
trial.facets$row <- seq(0, nrow(trial.facets)-1)  %/% n_col

# determine the max by row, and round up to nearest multiple of 5
# join back to original
trial.facets.max <- trial.facets %>% 
  group_by(row) %>% 
  summarize(maxvalue = (1 + max(xx, yy) %/% 5) * 5 )
trial.facets <- trial.facets %>% inner_join(trial.facets.max)

# make long format carrying period, row and maxvalue
trial.facets.tidied<-trial.facets %>% gather(key=newvar,value=newvalue,-period,-row,-maxvalue)

# plot an invisible line at the max
ggplot(trial.facets.tidied,aes(x=newvar,y=newvalue,position="dodge"))+
  geom_bar(stat ="identity") +
  geom_hline(aes(yintercept=maxvalue), alpha = 0) +
  facet_wrap(~period,ncol=n_col,scales="free")

在此处输入图像描述

于 2017-08-15T01:53:18.747 回答