- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 ggplot2 在一张图表中绘制多个箱线图。我有 1 个连续变量和几个因素。我想要一个 Y 轴,每对箱线图都有自己的 x 轴和自己的因子水平。到目前为止,我尝试使用 cowplot::plot_grid
将我单独制作的图表与我用于 Y 轴的空图表连接在一起。我试图通过隐藏边距和调整图表大小来使图表很好地融合,但我仍然无法获得合理的结果,而且这种方法涉及太多的手动调整。
这就是我想要的,也是我目前想到的:
charts
这是我的脚本:
library(ggplot2)
library(cowplot)
library(dplyr)
# make a dataset:
DF <- mtcars
DF$cyl <- as.factor(DF$cyl)
DF$vs <- as.factor(DF$vs)
DF$am <- as.factor(DF$am)
DF$gear <- as.factor(DF$gear)
DF$carb <- as.factor(DF$carb)
#str(DF)
# fisrt boxplot
p1 <- DF %>% ggplot() + theme_grey() + aes(x=cyl, y=mpg, fill=cyl) +
geom_boxplot() +
theme(legend.position = "none",
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
theme(plot.margin = margin(t=0.1, r=0, b=0, l=0, unit="cm"))
# second boxplot
p2 <- DF %>% ggplot() + theme_grey() + aes(x=vs, y=mpg, fill=vs) +
geom_boxplot() +
theme(legend.position = "none",
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
theme(plot.margin = margin(t=0.1, r=0, b=0, l=0, unit="cm"))
# empty boxplot used only for the y axis
y_axis <- DF %>% ggplot() + theme_grey() + aes(x=mpg, y=mpg) +
geom_point() +
theme(axis.title.y = element_text(),
axis.text.y = element_text(),
axis.title.x = element_text(),
axis.text.x = element_text()) +
theme(plot.margin = margin(t=0.1, r=0, b=0, l=0, unit="cm"))+
scale_x_continuous(limits = c(0, 0), breaks=c(0), labels = c(""), name="")
# join all charts toghether
p_all <- plot_grid(y_axis, p1, p2,
align="v", axis="l",
nrow=1, rel_widths = c(0.2, 1, 1))
ggdraw(p_all)
最佳答案
这是使用多个变量、一些颜色和使用 tidyr
时的样子.您可以使用 panel.border
在图之间添加边界并且应该在 facet_wrap
中指定行数为 1 :
library(ggplot2)
library(dplyr)
library(tidyr)
# Only select variables meaningful as factor
DF <- select(mtcars, mpg, cyl, vs, am, gear, carb)
DF %>%
gather(variable, value, -mpg) %>%
ggplot(aes(factor(value), mpg, fill = factor(value))) +
geom_boxplot() +
facet_wrap(~variable, scales = "free_x", nrow = 1, strip.position = "bottom") +
theme(panel.spacing = unit(0, "lines"),
panel.border = element_rect(fill = NA),
strip.background = element_blank(),
axis.title.x = element_blank(),
legend.position = "none",
strip.placement = "outside")
关于r - ggplot : Boxplot by several categorical variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53224089/
我想通过 Seaborn 中生成的箱线图获取特定值 (即,媒体,四分位数)。例如,在下面的箱线图中(来源:link) 有没有办法获得媒体和四分位数而不是手动估计? import numpy as np
我正在尝试创建箱线图,但遇到了这个错误 - “系列”对象没有属性“箱线图” 这是我当前的代码: fig = plt.figure(figsize=(8,6)) ax = fig.gca() frame
我无法在箱形图中使用名称。它不起作用是因为我只有一个向量吗?这是我尝试过的。 tmp = c(1,1,1,1,2,2,2,2,5,5,5,5,5,6,5,4,7) boxplot(tmp) boxpl
我正在使用 seaborn 创建箱线图。当我指定一列对框进行分组/着色时,框的宽度变得非常窄,以至于很难看到。我所做的唯一更改是为hue 指定一个参数,它指向传递的数据帧中的一列。我曾尝试使用 'wi
delme <- exp(rnorm(1000,1.5,0.3)) boxplot(delme,log="y") boxplot(log10(delme)) 为什么这两幅图中的 mustache 不同
我使用以下代码对以第 5 个和第 95 个百分位数作为上限和下限的值进行箱线图。令人惊讶的是,我通过在 python 2.7.3 中使用 matplotlib 1.4.0 和在 python 3.6.
我正在尝试将箱线图的异常值颜色与美学(scale_colour_discrete)设置的填充颜色相匹配。 这是一个例子。 m <- ggplot(movies, aes(y = votes, x =
我对 R 和 ggplot 还很陌生。 我正在尝试生成一个按两个变量排序的箱线图。就我而言,物种和实验。 到目前为止我得到了什么 ggplot(DF, aes(Species, Protein, fi
我有一个数据框,其中包含“季度”和“转售价格”列。我使用数据框使用seaborn绘制了箱线图。箱线图显示季度值,例如 (2007-Q2、2007-Q3、2007-Q4、2008-Q2)。但是,我希望它
我正在根据大数据(2150000 个案例)按年份绘制两组体重的简单箱线图。除去年的最后一组外,所有组的中位数都相同,但在箱线图上,它绘制得好像与其他组一样。 #boxplot ggplot(data
我正在将 datafame 的一些列绘制成箱线图。到目前为止,没问题。如下所示,我写了一些东西并且它有效。但是:第二个图也包含第一个图的图。正如你所看到的,我用“= None”或“del value”
下面的代码有助于获得具有独特颜色框的子图。但是所有子图共享一组通用的 x 和 y 轴。我期待每个子图都有独立的轴: import pandas as pd import numpy as np imp
在关于如何制作具有方面和显着性水平的箱线图的许多问题之后,特别是 this和 this ,我还有一个小问题。 我设法生成了如下所示的图,这正是我想要的。 我现在面临的问题是当我很少或没有显着的比较时;
预先感谢您的帮助! 我正在尝试在 matplotlib 中创建箱线图,但在尝试添加标签时出现错误。这是引发错误的代码: df_selected_station_D.boxplot(column='20
我正在使用 Wisconsin dataset 。要显示 Y 轴是数据帧变量(例如:radius_mean)且 X 轴是诊断的箱线图,我执行以下操作: sns.boxplot(x='label', y
我已经为此工作了几个小时,似乎无法做到这一点。箱线图只给我平坦的垂直线,它让我发疯。无论有没有因子函数,我都得到相同的输入 ggplot(df2,aes(x = factor(Location),y=
预先感谢您的帮助! 我正在尝试在 matplotlib 中创建箱线图,但在尝试添加标签时出现错误。这是引发错误的代码: df_selected_station_D.boxplot(column='20
我正在使用 Wisconsin dataset 。要显示 Y 轴是数据帧变量(例如:radius_mean)且 X 轴是诊断的箱线图,我执行以下操作: sns.boxplot(x='label', y
我正在尝试将箱线图中的异常值涂成黑色。我设法将盒子着色为灰色,但我不知道如何为异常值着色。 (图表上的白点) boxplot(y1$Frequency..times., col="grey") 预先感
在 R 中,我试图在 ggplot 中制作一个箱线图,其中翻转坐标(水平框)使用 facets 分组。当我在不翻转坐标的情况下构建它时,ggplot 将在 scales="free"的方面内删除未使用
我是一名优秀的程序员,十分优秀!