R/Officer/MsChart 导出到 Powerpoint:如何设置条形图标签之间的间隔 [关闭]

问题描述 投票:0回答:0

我目前出口,例如使用 officer- 和 mschart-package 的带 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")

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

我得到的是:

我要的是这个:

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

r mschart officer
© www.soinside.com 2019 - 2024. All rights reserved.