简体   繁体   English

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

[英]Remove border when ggsave svg is embedded in html file

I have created a svg using ggplot2::ggsave() . 我使用ggplot2::ggsave()创建了一个svg。 I embed the svg inline in a html file. 我将svg嵌入到html文件中。 However, I find that there is a border around the svg. 但是,我发现svg周围有一个边框。 How do I remove this border? 如何删除此边框?

tl;dr version: download this html , how do I remove the border around the inline svg? tl; dr版: 下载这个html ,如何删除内联svg周围的边框?

Here is the code I used to create the svg: 这是我用来创建svg的代码:

dput of statistics_data : dputstatistics_data

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")

Create a pie chart using ggplot2 : 使用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)

Use ggsave to save the pie chart: 使用ggsave保存饼图:

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

Then embed the svg in a html file, which can be downloaded here . 然后将svg嵌入到html文件中, 可以在此处下载

There is a border around the svg! svg周围有一个边框! How do I get rid of it? 我怎么摆脱它?

That was a challenge, Alex! 亚历克斯,这是一个挑战! Unfortunately there's no argument to pass to grDevices to control the border, so you have to set the transparency in the theme as you've tried. 不幸的是,传递给grDevices来控制边框是没有争议的,所以你必须按照你的尝试设置主题的透明度。 I've tried element_blank() for the following options and think it works: 我已经尝试了以下选项的element_blank()并认为它有效:

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

Perhaps you could try it and confirm this works as intended? 也许你可以尝试一下并确认这是按预期工作的?

I have a hack that I've been using. 我有一个我一直在使用的黑客。

First you need to identify in your svg file where the offending line that draws the border is. 首先,您需要在svg文件中标识绘制边框的违规行所在的位置。 Open your output svg file using a text file. 使用文本文件打开输出svg文件。 It would be usually located the first 20 or so lines and have the format: 它通常位于前20行左右,格式如下:

<rect x='2.26' y='0.00' width='355.47' height='216.00' style='stroke-width: 1.07; stroke: #FFFFFF;'/>

The number will likely be different but this tells it to draw a rectangle of white border at the coordinate (2.26, 0) of width 355.47 and height 216. 数字可能会有所不同,但这告诉它在宽度355.47和高度216的坐标(2.26,0)处绘制一个白色边框矩形。

You can manually delete this line. 您可以手动删除此行。 If you want to delete it automatically, say that the offending line is line 15. Then add the following line after you make the plot. 如果要自动删除它,请说明违规行是第15行。然后在创建绘图后添加以下行。

afile <- "image.svg"
ggsave(p, filename=afile, bg="transparent")
graph <- readLines(afile, -1)
graph[15] <- ""
writeLines(graph, afile)

This will read the file, replace the offending line with an empty line and overwrite the existing file. 这将读取文件,用空行替换违规行并覆盖现有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM