标签: facet-wrap

将单独的线添加到 facet_wrapped 图

我正在尝试使用 facet_wrap 将一堆数据集绘制为散点,每个散点中都有一条单独的线,指示特定事件发生的时间。但是,我无法让线条在每个情节中单独显示,而是它们都出现在所有情节中。熔化后,数据如下所示:

names(data) = c("Date","ID", "event_date", "variable", "value")
Run Code Online (Sandbox Code Playgroud)

我希望每个图都是值 ~ 日期的散点图,每个图按 ID 划分,在每个“event_date”处出现一条垂直线,显示每个 ID。我的最大努力使我:

p <- qplot(Date, value, data=dat, colour=variable)
p <- p + geom_vline(xintercept=as.numeric(dat$event_date))
p + facet_wrap(~ID)
Run Code Online (Sandbox Code Playgroud)

除了出现在每个子图中的所有垂直线外,它完美地工作。有什么建议?通读文档还没有让我到任何地方。

plot r ggplot2 facet-wrap

6
推荐指数
1
解决办法
3709
查看次数

ggplot:显示每个构面图的x轴线

我使用ggplot创建了一个facet plot,它有4行1列,代码如下:

ggplot(data=c, aes(x=Time, y=X.mean, fill = Site, width=.1)) +  
  geom_bar(stat="identity", position=position_dodge(),  width=0.5,colour="black",        show_guide=FALSE) +
  ylab(NULL) + xlab(NULL) +
   geom_errorbar(aes(ymax= X.Passes.sd, ymin= 0),
            size = 0.7, width = 0.3,position = position_dodge(0.9))+
  scale_fill_manual(values=c("cadetblue2", "royalblue1", "mediumseagreen", "green4")) +
  scale_y_continuous(breaks=number_ticks(12), expand = c(0, 0), limits=c(0,20)) +
  scale_x_discrete(expand = c(0, 0), limits=c("17:00","18:00","19:00","20:00","21:00","22:00","23:00","00:00","01:00","02:00","03:00","04:00", "05:00", "06:00")) +
  facet_wrap(~ Site, ncol=1,nrow=4)+
  theme_bw() + 
  theme(strip.text.x = element_text(size=18))+
  theme(axis.title.y=element_text(size = 18)) +
  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank())+
  theme(axis.text.x=element_text(angle = 45, hjust=1, vjust=1, size = 18))+
  theme(axis.text.y=element_text(size = 18))+
  theme(panel.border = element_blank())+
  theme(axis.line …
Run Code Online (Sandbox Code Playgroud)

axis r ggplot2 facet-wrap geom-bar

6
推荐指数
2
解决办法
4791
查看次数

R ggplot - 将所有数据点添加到所有方面

到目前为止我找不到合适的答案,这是关于R中ggplot2的简短问题:

data(mtcars)
ggplot(data=mtcars, aes(x=mpg, y=wt, fill=factor(cyl))) +
scale_fill_manual(values=c("red","orange","blue"))+
geom_point(size=2, pch=21)+
facet_grid(.~cyl)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这一切都很好,现在我想要每个方面的所有数据点(无论数字是多少)(例如,在点下方有一个平滑的灰色)?

谢谢,迈克尔

r facet ggplot2 facet-wrap

6
推荐指数
2
解决办法
1840
查看次数

使用多个变量的ggplot2 facet_wrap的独立条带主题

是否可以为ggplot2中使用的每个变量设置独立的条带主题facet_wrap

以这段代码为例:

p1 <- ggplot(mpg, aes(displ, hwy)) +
   geom_point() +
   facet_wrap(c("cyl", "drv"), labeller = "label_both")
plot(p1)
Run Code Online (Sandbox Code Playgroud)

绘图输出

我希望上面的条带('cyl')具有不同的主题 - 比方说,用粗体.另外,我可能想制作'drv'斜体并使用不同的字体类型和大小.我怎么能这样做?

我正在考虑以下几点:

 p1 <- p1 + theme(strip.text.variable1 = element_text(face = 'bold'),
                  strip.text.variable2 = element_text(face = 'italic', size = 8)
                  )
Run Code Online (Sandbox Code Playgroud)

不幸的是,我在文档或之前的问题中找不到这样的内容.

干杯

编辑:我提出了一个更普遍的问题,以进一步帮助社区.

r ggplot2 facet-wrap

6
推荐指数
1
解决办法
607
查看次数

GGPLOT箱线图:独立指定小平面尺度

我有两个实验室测试的大量测量值(N ~ 500.000),我想使用geom_boxplot. 这些测量结果是使用三种不同的分析仪创建的。然而,这些测量结果中有许多都是极端异常值,这会导致范围极宽,从而再次导致框太小而无法真正读取或比较。

我创建了一个虚拟数据集,仅对其中 250 个测量值进行采样,以展示该问题。数据如下:

dummy <- structure(list(result = c(3.93, 2.708, 2.08, 1.8422, 0.897, 1.68, 
                    3.56, 2.8954, 0.972, 2.99, 0.567, 2.01, 2.5629, 0.7958, 4.81, 
                    3.539, 2.24, 4.79, 2.07, 5.56, 3.4761, 1.74, 1.5691, 0.964, 1.8171, 
                    0.005, 0.663, 2.16, 2.37, 0.0164, 1.25, 3.086, 0.769, 1.9573, 
                    1.05, 2.17, 1.6331, 8.2358, 0.58, 1.43, 0.9328, 1.94, 2.59, 5.06, 
                    0.0574, 1.61, 1.01, 8.21, 2.77, 0.9938, 20.38, 3.71, 1.6731, 
                    0.0259, 0.9701, 0.0114, 0.499, 38.8, 4.8689, 3.02, 1.176, 2.86, 
                    1.96, 5.03, 0.7564, 0.903, 2.0017, …
Run Code Online (Sandbox Code Playgroud)

r scale ggplot2 facet-wrap boxplot

6
推荐指数
0
解决办法
1616
查看次数

R 中的 Facet_Wrap 标签

我正在尝试使用 facet 更改 ggplot 图的标签,并且正在使用数据帧中未分面的变量。代码如下-

iris %>%
  group_by(Species) %>%
  summarise(lbl = mean(Petal.Length)) %>%
  select(lbl) %>%
  unlist() -> lbls
lbls <- map2(unique(iris$Species), lbls, paste, sep = "\n")
iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, group = 1)) +
  geom_point() +
  facet_wrap(~ Species, labeller = lbls)
Run Code Online (Sandbox Code Playgroud)

但是,这给了我以下错误-

Error in cbind(labels = list(), list(`{`, if (!is.null(.rows) || 
!is.null(.cols)) { : 
 number of rows of matrices must match (see arg 2)
Run Code Online (Sandbox Code Playgroud)

我查看了 labeller 函数和不同的选项,但其中大多数似乎是针对包含在 facet 中的变量。有没有办法做到这一点?任何线索表示赞赏,谢谢!

r ggplot2 facet-wrap

6
推荐指数
2
解决办法
4708
查看次数

使用facet_wrap的百分比直方图

我正在尝试将百分比直方图与facet_wrap,但百分比不是基于组计算的,而是所有数据.我希望每个直方图显示一组中的分布,而不是相对于所有人口.我知道可以做几个情节并将它们结合起来multiplot.

library(ggplot2)
library(scales)
library(dplyr)

set.seed(1)
df <- data.frame(age = runif(900, min = 10, max = 100),
                 group = rep(c("a", "b", "c", "d", "e", "f", "g", "h", "i"), 100))

tmp <- df %>%
  mutate(group = "ALL")

df <- rbind(df, tmp)

ggplot(df, aes(age)) + 
  geom_histogram(aes(y = (..count..)/sum(..count..)), binwidth = 5) + 
  scale_y_continuous(labels = percent ) + 
  facet_wrap(~ group, ncol = 5) 
Run Code Online (Sandbox Code Playgroud)

输出: 输出图

r histogram ggplot2 facet-wrap

6
推荐指数
1
解决办法
825
查看次数

编辑刻面/条带与绘图之间的距离

例如,

library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(cols = vars(drv))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 如何更改条带和主图之间的距离?(例如,在条带和主图之间创建一个间隙。)
但我不需要更改条带大小(与此编辑条带大小 ggplot2 不同)。

r ggplot2 facet-wrap facet-grid

6
推荐指数
1
解决办法
703
查看次数

如何控制“facet_wrap()”中的轴刻度数?

我有一个用facet_wrap 创建的图形,可视化许多组的估计密度。有些组的方差比其他组小得多。这会导致某些面板的 x 轴无法读取。最小可重现示例:

library(tidyverse)
x1 <- rnorm(1e4)
x2 <- rnorm(1e4,mean=2,sd=0.00001)

data.frame(x=c(x1,x2),group=c(rep("1",length(x1)),rep("2",length(x2)))) %>%
  ggplot(.) + geom_density(aes(x=x)) + facet_wrap(~group,scales="free")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

解决这个问题的明显方法是增加图形大小,以便一切都变得可读。然而,面板太多,无法使其成为有用的解决方案。我最喜欢的解决方案是控制轴刻度数,例如在所有 x 轴上仅允许两个刻度。有办法做到这一点吗?


根据建议进行编辑:

添加+ scale_x_continuous(n.breaks = 2)看起来应该完全符合我的要求,但实际上并没有:

在此输入图像描述

按照建议问题Change number of Breaks using facet_grid in ggplot2中的答案,我最终得到两个轴刻度,但不希望有很多小数点:

equal_breaks <- function(n = 3, s = 0.5, ...){
  function(x){
    # rescaling
    d <- s * diff(range(x)) / (1+2*s)
    seq(min(x)+d, max(x)-d, length=n)
  }
}

data.frame(x=c(x1,x2),group=c(rep("1",length(x1)),rep("2",length(x2)))) %>%
  ggplot(.) + geom_density(aes(x=x)) + facet_wrap(~group,scales="free")  + scale_x_continuous(breaks=equal_breaks(n=3, s=0.05), expand = c(0.05, 0))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r facet ggplot2 facet-wrap

6
推荐指数
1
解决办法
2476
查看次数

ggplot2 中的中心面

我正在geom_smooth按以下分组的方面绘制 s size

library(ggplot2)
ggplot(df,
       aes(x = pos, y = mean_ratio_f ))+
  geom_smooth(aes(group = factor(size)), method = "lm", se = FALSE, linewidth = 0.5) +
  # facets:
  facet_wrap(. ~ size, scales = 'free_x')+
  labs(x ="X", 
       y = "Y")
Run Code Online (Sandbox Code Playgroud)

不幸的是,最后三个面(对于size第 23、24 和 25 组)与左边距对齐,因此它们的右侧有一个间隙(这也造成了整个图向右倾斜的印象!):

在此输入图像描述

在我看来,这个问题可以通过集中讨论的三个方面来解决(但也许还有其他解决方案)。如何重新排列事实以使最后三个方面居中

数据:

df <- structure(list(size = c(3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 
                              8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 12L, 12L, 13L, 13L, 14L, 
                              14L, …
Run Code Online (Sandbox Code Playgroud)

r facet ggplot2 facet-wrap

6
推荐指数
1
解决办法
252
查看次数

标签 统计

facet-wrap ×10

ggplot2 ×10

r ×10

facet ×3

axis ×1

boxplot ×1

facet-grid ×1

geom-bar ×1

histogram ×1

plot ×1

scale ×1