我正在研究自定义 ggplot2 主题,并认为根据绘图对象的某些特征自动修改主题元素可能很不错。例如,有没有办法指定如果绘图包含面,则为每个面板添加边框?
我想问题是真的,我可以从自定义 theme() 调用中访问当前的 gg 对象,然后有条件地应用某些主题元素吗?在我的脑海中,我会将我的主题功能定义为这样的:
theme_custom <- function() {
if (plot$facet$params > 0) {
theme_minimal() +
theme(panel.border = element_rect(color = "gray 50", fill = NA))
}
else {
theme_minimal()
}
}
Run Code Online (Sandbox Code Playgroud)
如果这是可能的,它在使用中看起来像这样:
library(ggplot2)
# plot with facets automatically adds panel borders
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
facet_wrap(vars(cyl)) +
theme_custom()
Run Code Online (Sandbox Code Playgroud)

# plot without facets no panel border
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
theme_custom()
Run Code Online (Sandbox Code Playgroud)

注意:这最初发布在RStudio 社区上,但没有收到答复。
我正在开发一个独立的 flexdashboard 项目,我想知道当用户单击一个选项卡集中的新选项卡时是否有可能,它也会更改为第二个选项卡集中的新选项卡。
例如,当您单击下面的“图表 B1”时,我还想将第二列中的视图更改为“图表 B2”。单击“图表 A2”将变回“图表 A1”,依此类推。
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
```
Column {.tabset}
-----------------------------------------------------------------------
### Chart A1
### Chart B1
Column {.tabset}
-----------------------------------------------------------------------
### Chart A2
### Chart B2
Run Code Online (Sandbox Code Playgroud)
请注意,这首先发布在 RStudio Community 上,但没有收到任何回复。
总体思路是创建一个可以在显示计数和百分比之间切换的绘图图表。我可以使用更新菜单更改显示的迹线,并且轴刻度可以动态更改,但是当我在下面的示例中切换到“%”时,图例消失。
我根本不是一个情节专业人士,这就是我使用的原因ggplotly()(我的真实示例更复杂并且ggplolty()效果很好!)并且我想知道是否必须手动将图例添加到 % 迹线 (3, 4) 中当第一个痕迹不可见时是否显示图例?
library(ggplot2)
library(plotly)
df <- structure(list(outcome = c("a", "b", "a", "b", "a", "b", "a",
"b", "a", "b"), n = c(59, 191, 28, 67, 29, 56, 33, 45, 32, 40
), pct = c(0.208480565371025, 0.674911660777385, 0.288659793814433,
0.690721649484536, 0.337209302325581, 0.651162790697674, 0.4125,
0.5625, 0.444444444444444, 0.555555555555556), day = c(1L, 1L,
2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L)), class = "data.frame",
row.names = c(NA, -10L))
p <- ggplot(df, aes(day, n, color = outcome)) + …Run Code Online (Sandbox Code Playgroud)