gpt4 book ai didi

当 ggsave svg 嵌入 html 文件时删除边框

转载 作者:行者123 更新时间:2023-12-01 10:58:45 24 4
gpt4 key购买 nike

我使用 ggplot2::ggsave() 创建了一个 svg。我将 svg 嵌入到一个 html 文件中。但是,我发现 svg 周围有一个边框。如何删除此边框?

tl;dr 版本:download this html ,如何删除内联 svg 周围的边框?

这是我用来创建 svg 的代码:

statistics_datadput:

statistics_data <-
structure(list(Category = structure(c(5L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 2L, 2L, 3L, 3L, 3L, 3L,
4L, 5L, 3L, 5L, 5L, 5L, 1L, 1L, 1L), .Label = c("Online Presence",
"Social Presence", "Web Design", "Web Development", "Website Content"
), class = "factor"), Category_count = c(9L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 9L, 9L, 9L, 9L, 2L, 2L, 5L,
5L, 5L, 5L, 1L, 9L, 5L, 9L, 9L, 9L, 14L, 14L, 14L), Category_name = c("Website Content (9)",
"Online Presence (14)", "Online Presence (14)", "Online Presence (14)",
"Online Presence (14)", "Online Presence (14)", "Online Presence (14)",
"Online Presence (14)", "Online Presence (14)", "Online Presence (14)",
"Online Presence (14)", "Online Presence (14)", "Website Content (9)",
"Website Content (9)", "Website Content (9)", "Website Content (9)",
"Social Presence (2)", "Social Presence (2)", "Web Design (5)",
"Web Design (5)", "Web Design (5)", "Web Design (5)", "Web Development (1)",
"Website Content (9)", "Web Design (5)", "Website Content (9)",
"Website Content (9)", "Website Content (9)", "Online Presence (14)",
"Online Presence (14)", "Online Presence (14)")), .Names = c("Category",
"Category_count", "Category_name"), row.names = c(NA, -31L), class = "data.frame")

使用 ggplot2 创建饼图:

   p <- ggplot(data = statistics_data,
aes(x = factor(1), fill = factor(Category))
) +
geom_bar(width = .2, stat = "bin") +
xlab('') +
ylab('') +
theme(axis.ticks = element_blank(),
axis.text.y = element_blank(),
panel.grid.major=element_blank(),
panel.background = element_rect(fill = 'transparent'),
plot.background = element_rect(fill = 'transparent'),
legend.background = element_rect(fill = 'transparent'),
panel.border = element_rect(colour = NA, fill = NA)) +
scale_fill_manual(values = c("Online Presence" = "#4b67b9", "Social Presence" = "#d85341", "Web Design" = "#ff8b24", "Web Development" = "#aad32e", "Website Content" = "#fec52e")
, breaks = sort(unique(statistics_data$Category))
, labels = sort(unique(statistics_data$Category_name))
) +
scale_y_continuous(breaks = NULL) +
coord_polar(theta="y") +
labs(fill = 'Ranking Factor Category', x = NULL, y = NULL)

使用ggsave 保存饼图:

ggsave("test_pie_chart.svg", width = 5, height = 3, dpi = 300, bg = "transparent")

然后将 svg 嵌入到 html 文件中,which can be downloaded here .

svg 周围有边框!我该如何摆脱它?

最佳答案

亚历克斯,这是一个挑战!不幸的是,没有参数可以传递给 grDevices 来控制边框,因此您必须在尝试时设置主题中的透明度。我已经为以下选项尝试了 element_blank() 并认为它有效:

panel.background = element_blank(),
plot.background = element_blank(),
legend.background = element_rect(fill = 'transparent'),
panel.border = element_blank()) +

也许您可以尝试一下并确认它是否按预期工作?

关于当 ggsave svg 嵌入 html 文件时删除边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27119869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com