[英]Arrange marginal rug and density so they don't overlap with legend at bottom
Let's have a following dataframe:让我们有以下 dataframe:
test <- structure(list(category = c("C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",
"C", "C", "C", "C"), position = c(43.87, 175.63, 119.58, 63.82,
70.7, 87, 103.23, 128.84, 31.52, 99.21, 40.65, 27.44, 139.39,
95.38, 87.56, 16.94, 45.18, 32.93, 62.68, 225.38, 48.85, 87.97,
18.28, 82.39, 76.08, 23.91, 57.61, 76.3, 47.78, 108.4, 43.73,
228.35, 71.39, 40.92, 59.93, 113.23, 29.51, 9.97, 134.46, 79.26,
188.23, 24.96, 131.29, 52.58, 114.98, 100.4, 75.89, 54.44, 61.1,
27.36, 26.95, 22.25, 62.63, 36.87, 15.19, 88.69, 28.92, 235.41,
35.49, 46.76, 97.62, 47.67, 24.91), length = c(110.83, 205.63,
135.97, 139.4, 137.48, 120.56, 131, 147.17, 90.37, 108.25, 141.27,
154.22, 157.4, 143.66, 151.57, 185.03, 313.36, 60.45, 81.37,
256.44, 140.56, 188.46, 100.16, 203.62, 172.79, 139.8, 174.39,
132.27, 126.19, 129.12, 152.96, 294.5, 190.25, 112.24, 224.06,
172.53, 308.87, 118.08, 168.83, 115.55, 238.9, 215.96, 159.52,
88.47, 178.92, 178.92, 178.38, 81.43, 71.2, 125.03, 139.9, 93.25,
144.58, 164.3, 126.32, 130.55, 102.31, 246.91, 136.16, 138.35,
228.73, 124.76, 88.11)), row.names = c(NA, -63L), class = c("tbl_df",
"tbl", "data.frame"))
I would like to plot the variables as scatterplot, add rug on the y-axis and marginal distributions on both axes.我想将 plot 变量作为散点图,在 y 轴上添加地毯,在两个轴上添加边际分布。 I would also like to make the chart aesthetically pleasing.
我还想让图表美观。
This is my most satisfying result so far:这是我迄今为止最满意的结果:
library(ggplot2)
library(ggExtra)
plottest <- ggplot2::ggplot(test, aes(x=length, y=position, colour = category)) +
ggplot2::geom_point()+ ggplot2::geom_rug(sides = "r",
col="steelblue",
alpha=0.2,
size=1.5,
outside = TRUE) +
ggplot2::theme_minimal() +
ggplot2::theme(legend.position="bottom",
axis.line.x = element_line(size = 0.45, color="#3a424f"),
axis.line.y = element_line(size = 0.45, color="#3a424f")) +
ggplot2::coord_cartesian(clip = "off") +
ggplot2::scale_color_manual(values=c("#174e73"))
ggExtra::ggMarginal(plottest, groupColour = TRUE)
And here is the result:结果如下:
However I would prefer to have filled distributions and theme_bw() instead of theme_minimal().但是我更喜欢填充分布和 theme_bw() 而不是 theme_minimal()。 However, in this case, the rug overlaps with distribution.
然而,在这种情况下,地毯与分布重叠。 This is not a big deal in case of this minimal example, however in my real-life data this can be more annoying.
对于这个最小的例子,这不是什么大问题,但在我的真实数据中,这可能更烦人。
I am wondering how to plot rug on the outer side of the plot and move distribution a little bit away from the rug and plotting area, so the elements would not be cluttered one atop of another.我想知道如何将 plot 地毯放在 plot 的外侧,并将分布从地毯和绘图区域移开一点,这样元素就不会一个接一个地杂乱无章。
This is another version of my code, with filled densities and preferred theme:这是我的代码的另一个版本,具有填充密度和首选主题:
plottest <- ggplot2::ggplot(test, aes(x=length, y=position, colour = category)) +
ggplot2::geom_point()+ ggplot2::geom_rug(sides = "r",
col="steelblue",
alpha=0.2,
size=1.5,
outside = TRUE) +
ggplot2::theme_bw() +
ggplot2::theme(legend.position="bottom") +
ggplot2::coord_cartesian(clip = "off") +
ggplot2::scale_color_manual(values=c("#174e73"))
ggExtra::ggMarginal(plottest, groupColour = TRUE,groupFill = TRUE)
This is what I produce with this code:这是我用这段代码产生的:
And this is what I would like to have (this graph is prepared in gimp):这就是我想要的(这张图是用 gimp 准备的):
This is not trivial, as the position of your marginal plot is sort of "relatively hard coded" with ggExtra:::addRightMargPlot
in the sense that it is adding an extra gtable column directly adjacent to the panel, but here three (relatively simple) workarounds.这不是微不足道的,因为边际 plot 的 position 在某种意义上是用
ggExtra:::addRightMargPlot
进行了“相对硬编码”,因为它添加了一个直接与面板相邻的额外 gtable 列,但这里是三个(相对简单)解决方法。
Figure 1 .图 1 。 My favourite: Add an "invisible" dupe y axis and make cheeky use of the common newbie "mistake" to remove titles with
""
(this is still drawing something, and it is in general better to use NULL
, but not here.), You could then even add a margin to this invisible title.我最喜欢的:添加一个“不可见的”欺骗 y 轴并厚颜无耻地使用常见的新手“错误”来删除带有
""
的标题(这仍然是在画东西,通常最好使用NULL
,但不是在这里。) , 然后您甚至可以为这个不可见的标题添加边距。 but this is not even necessary here.但这在这里甚至没有必要。
Figure 2 : You can play around with the size argument.图 2 :您可以使用 size 参数。 This will change the plot appearance drastically and might not be what you want.
这将彻底改变 plot 的外观,可能不是您想要的。
Figure 3 (by far the easiest): You can simply accept to plot the rug "inside" which is not necessarily such a huge compromise in your visual.图 3 (迄今为止最简单的):您可以简单地接受 plot 地毯“内部”,这在您的视觉上不一定是一个巨大的妥协。
library(ggExtra)
library(ggplot2)
p <-
ggplot(test, aes(x = length, y = position, colour = category)) +
geom_point() +
geom_rug(
sides = "r",
col = "steelblue",
alpha = 0.2,
size = 1.5,
outside = TRUE
) +
coord_cartesian(clip = "off") +
scale_color_manual(values = c("#174e73")) +
## add a second axis
## use of the common title = "" newbie mistake to your advantage
scale_y_continuous(sec.axis = dup_axis(name = "")) +
theme_minimal() +
theme(legend.position = "bottom",
axis.line.x = element_line(size = 0.45, color = "#3a424f"),
axis.line.y = element_line(size = 0.45, color = "#3a424f"),
## remove all stuff from your axis
axis.text.y.right = element_blank(),
axis.ticks.y.right = element_blank(),
axis.line.y.right = element_blank())
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
ggMarginal(p, groupFill = TRUE)
plottest <-
ggplot(test, aes(x = length, y = position, colour = category)) +
geom_point() +
## I've removed geom_rug for the below
theme_minimal() +
theme(
legend.position = "bottom",
axis.line.x = element_line(size = 0.45, color = "#3a424f"),
axis.line.y = element_line(size = 0.45, color = "#3a424f")
) +
coord_cartesian(clip = "off") +
scale_color_manual(values = c("#174e73"))
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
## Option 2 - playing around with sizes
## p_outside is your original plot
p_outside <- plottest +
geom_rug(
sides = "r",
col = "steelblue",
alpha = 0.2,
size = 1.5,
outside = TRUE
)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
ggMarginal(p_outside, size = 1, groupFill = TRUE)
## Option 3 - don't plot the rug outside
## p_inside is your plot with outside argument removed
p_inside <- plottest +
geom_rug(
sides = "r",
col = "steelblue",
alpha = 0.2,
size = 1.5,
)
ggMarginal(p_inside, groupFill = TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.