当同时绘制时,如何在 ggplot2 中显示 `geom_boxplot` 和 `geom_dotplot` ?
How to display `geom_boxplot` and `geom_dotplot` spaced in ggplot2 when plot both at same time?
我正在做一个项目并尝试重现以下情节。
这是我的尝试。
test<-data.frame(
x=factor(rep(1:3, each=20)),
y=runif(60)
)
p<-ggplot(test, aes(x=x, y=y,fill=x)) +
geom_boxplot(width=0.2) +
geom_dotplot(binaxis = "y")
p
但是箱线图和点图在我的尝试中挤在一起。任何建议表示赞赏。
您可以使用 position = position_nudge(...)
微调几何的位置。您可以使用它在相反的方向上偏移箱线图和点图。示例如下:
library(ggplot2)
test<-data.frame(
x=factor(rep(1:3, each=20)),
y=runif(60)
)
ggplot(test, aes(x=x, y=y,fill=x)) +
geom_boxplot(width=0.2, position = position_nudge(x = -0.1)) +
geom_dotplot(binaxis = "y", position = position_nudge(x = 0.1))
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
由 reprex package (v1.0.0)
于 2021-04-05 创建
我会使用 position_nudge(x = 0, y = 0)
函数。这允许您在 x 和 y 中平移绘图的元素。该函数可用作 geom_xxxx()
函数中的位置参数。例如使用 geom_boxplot(..., position = position_nudge(x = -0.1))
将箱线图 0.1 向左平移。
library(tidyverse)
test<-data.frame(
x=factor(rep(1:3, each=20)),
y=runif(60)
)
p<-ggplot(test, aes(x=x, y=y,fill=x)) +
#Translate left
geom_boxplot(width=0.2, position = position_nudge(-0.1)) +
#Translate right
geom_dotplot(binaxis = "y", width = 0.2, position = position_nudge(x = 0.1))
p
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
由 reprex package (v2.0.0)
于 2021-04-04 创建
我正在做一个项目并尝试重现以下情节。
这是我的尝试。
test<-data.frame(
x=factor(rep(1:3, each=20)),
y=runif(60)
)
p<-ggplot(test, aes(x=x, y=y,fill=x)) +
geom_boxplot(width=0.2) +
geom_dotplot(binaxis = "y")
p
但是箱线图和点图在我的尝试中挤在一起。任何建议表示赞赏。
您可以使用 position = position_nudge(...)
微调几何的位置。您可以使用它在相反的方向上偏移箱线图和点图。示例如下:
library(ggplot2)
test<-data.frame(
x=factor(rep(1:3, each=20)),
y=runif(60)
)
ggplot(test, aes(x=x, y=y,fill=x)) +
geom_boxplot(width=0.2, position = position_nudge(x = -0.1)) +
geom_dotplot(binaxis = "y", position = position_nudge(x = 0.1))
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
由 reprex package (v1.0.0)
于 2021-04-05 创建我会使用 position_nudge(x = 0, y = 0)
函数。这允许您在 x 和 y 中平移绘图的元素。该函数可用作 geom_xxxx()
函数中的位置参数。例如使用 geom_boxplot(..., position = position_nudge(x = -0.1))
将箱线图 0.1 向左平移。
library(tidyverse)
test<-data.frame(
x=factor(rep(1:3, each=20)),
y=runif(60)
)
p<-ggplot(test, aes(x=x, y=y,fill=x)) +
#Translate left
geom_boxplot(width=0.2, position = position_nudge(-0.1)) +
#Translate right
geom_dotplot(binaxis = "y", width = 0.2, position = position_nudge(x = 0.1))
p
#> `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.
由 reprex package (v2.0.0)
于 2021-04-04 创建