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

时间:2014-11-25 06:08:28

标签: r svg ggplot2 cairo

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

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

以下是我用来创建svg的代码:

dput

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

使用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周围有一个边框!我该如何摆脱它?

2 个答案:

答案 0 :(得分:1)

这是一个挑战,Alex!遗憾的是,没有任何理由可以传递给grDevices来控制边框,所以你必须在尝试时设置主题的透明度。我已尝试将element_blank()用于以下选项并认为它有效:

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

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

答案 1 :(得分:0)

我一直在使用黑客。

首先,您需要在class Foo { public $name = self::class; } $Foo = new Foo; echo $Foo->name; // Foo 文件中标识出绘制边框的有害线条。使用文本文件打开输出svg文件。通常位于前20行左右,格式为:

svg

数字可能会有所不同,但这告诉它在宽度355.47和高度216的坐标(2.26,0)处绘制一个白色边框的矩形。

您可以手动删除此行。如果要自动删除,请说出有问题的行是第15行。然后在绘制图形后添加以下行。

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

这将读取文件,将有问题的行替换为空行并覆盖现有文件。