R/官员/MsChart导出到Powerpoint:如何设置条形图标签之间的间隔

vfhzx4xs  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(144)

我目前使用officer-和mschart-软件包将一个带R的条形图导出到Powerpoint演示文稿中。我几乎可以实现我想要的一切。但如果我有一个巨大的数据。像这样的框架(minimal.pptx只是一个带一张空幻灯片的pptx):

library(officer)
library(mschart)

test_data <- data.frame(
    stringsAsFactors = FALSE,
    categories = factor(
        rep(
            c(
                "Label 1", "Label 2", "Label 3", "Label 4", "Label 5",
                "Label 6", "Label 7", "Label 8", "Label 9", "Label 10",
                "Label 11", "Label 12", "Label 13", "Label 14", "Label 15",
                "Label 16", "Label 17", "Label 18", "Label 19", "Label 20",
                "Label 21", "Label 22", "Label 23", "Label 24", "Label 25",
                "Label 26", "Label 27", "Label 28", "Label 29", "Label 30",
                "Label 31", "Label 32", "Label 33", "Label 34", "Label 35"
            ),
            3
        ),
        levels = c(
            "Label 1", "Label 2", "Label 3", "Label 4", "Label 5",
            "Label 6", "Label 7", "Label 8", "Label 9", "Label 10",
            "Label 11", "Label 12", "Label 13", "Label 14", "Label 15",
            "Label 16", "Label 17", "Label 18", "Label 19", "Label 20",
            "Label 21", "Label 22", "Label 23", "Label 24", "Label 25",
            "Label 26", "Label 27", "Label 28", "Label 29", "Label 30",
            "Label 31", "Label 32", "Label 33", "Label 34", "Label 35"
        ),
    ),
    values = c(
        1, 2, 3, 4, 5,
        6, 7, 8, 9, 10,
        11, 12, 13, 14, 15,
        16, 17, 18, 19, 20,
        21, 22, 23, 24, 25,
        26, 27, 28, 29, 30,
        31, 32, 33, 34, 35
    ),
    groups = c(
        "A", "A", "A", "A", "A",
        "A", "A", "A", "A", "A",
        "A", "A", "A", "A", "A",
        "A", "A", "A", "A", "A",
        "A", "A", "A", "A", "A",
        "A", "A", "A", "A", "A",
        "A", "A", "A", "A", "A"
    )
)

chrt <- ms_barchart(
    test_data,
    x = "categories",
    y = "values",
    group = "groups"
)

chrt <- chart_settings(
    chrt,
    dir = "horizontal",
    vary_colors = TRUE,
    grouping = "standard"
)

chrt <- chart_data_labels(
    chrt,
    position = "outEnd",
    show_val = T
)

chrt <- chart_data_fill(
    chrt,
    values = group_colors
) %>%
    chart_data_stroke(
        "transparent"
    )

chrt <- chart_labels(
    x = chrt,
    title = "Chart Title",
    xlab = "",
    ylab = ""
)

chrt <- chart_labels_text(
    chrt,
    values = fp_text(
        color = "#372E2C",
        font.size = 12,
        font.family = "Arial"
    )
)

my_chart_theme <- mschart_theme(
    legend_position = "n",
    main_title = fp_text(
        color = "#372E2C",
        font.size = 12,
        font.family = "Arial"
    ),
    axis_text_y = fp_text(
        color = "#878280",
        font.size = 12,
        font.family = "Arial"
    ),
    axis_text_x = fp_text(
        color = "#372E2C",
        font.size = 12,
        font.family = "Arial"
    ),
    grid_major_line_x = fp_border(
        width = 0
    ),
    grid_major_line_y = fp_border(
        color = "#E7E6E6",
        style = "solid",
        width = 0.5
    ),
    axis_ticks_x = fp_border(
        color = "#372E2C",
        style = "solid",
        width = 2.5
    ),
    axis_ticks_y = fp_border(
        color = "#F5F4F4",
        width = .001
    )
)

chrt <- set_theme(chrt, my_chart_theme)

chrt <- chart_ax_x(
    chrt,
    major_tick_mark = "none",
    minor_tick_mark = "none"
)

chrt <- chart_ax_y(
    x = chrt,
    major_tick_mark = "none",
    minor_tick_mark = "none"
)

doc <- read_pptx(path = "minimal.pptx")

default_location <- ph_location(
    left = 6.72,
    top = 0.47,
    width = 6.14,
    height = 6.721
)

doc <- on_slide(x = doc, index = 1)
doc <- ph_with(
    doc,
    value = chrt,
    location = default_location
)

print(doc, target = "output.pptx")

我有很多分类,比如说35个在导出的条形图X轴上,只会每隔一个类别打印一次X轴标签。它会自动将间隔设置为2,因为有许多标签。在Powerpoint中,我可以直接将轴上的“标签之间的间隔”设置为1,然后显示所有标签。但我不在R中找不到MsChart的导出间隔设置。有人能告诉我在哪里可以进行此设置吗?或者根本没有?我在chart_settings、chart_theme和chart_ax_x中找不到任何有用的设置。
我得到的结果是:

我想要的是:

当然,我可以减小轴的标签字体大小,但这只是一种变通方法,真实的的解决方案应该是手动定义这个间隔。

sgtfey8w

sgtfey8w1#

根据我对mschart:::axis_content_xml中底层代码的检查(输入chart_ax_x/chart_ax_y的参数在此转换为PowerPoint的xml),指定标签之间的间隔并不是该函数预期用途的一部分。
也就是说,它相对简单(在执行上,也就是说;我花了一段时间才找到正确的术语)来破解这个:

original.function <- mschart:::axis_content_xml

new.function <- function(...) {
  paste0(original.function(...),
         "<c:tickLblSkip val=\"1\"/>")
}

assignInNamespace("axis_content_xml",
                  new.function,
                  ns = "mschart")

为方便起见,我们将mschart:::axis_content_xml的当前版本赋给一个命名对象(original.function),然后通过在末尾粘贴"<c:tickLblSkip val=\"1\"/>"来定义我们自己的修改版本(new.function),由于在rpptx对象中添加图表时mschart:::axis_content_xml会被ph_with调用,因此添加此行会将指定单位间隔为1的xml指令插入到rpptx对象中。
在console中运行assignInNamespace()指令将导致修改后的版本被用于当前R会话的其余部分(或者直到您改变主意)。请注意,这不会在会话之间持续存在,尽管在每个会话中重新运行上述代码块应该不难,如果这是一个重复的需要。
如果你真的改变主意了

assignInNamespace("axis_content_xml",
                  original.function,
                  ns = "mschart")

对于此后添加到rpptx对象的任何新图表,都应该恢复正常。

相关问题