2014-11-25 63 views
2

我使用ggplot2::ggsave()創建了一個svg。我將svg嵌入到html文件中。但是,我發現svg周圍有一個邊框。我如何刪除此邊框?將ggsave svg嵌入到html文件中時刪除邊框

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周圍有邊框!我如何擺脫它?

回答

1

這是一個挑戰,亞歷克斯!不幸的是,沒有任何參數傳遞給grDevices來控制邊框,所以你必須在你嘗試的主題中設置透明度。我試過以下選項element_blank(),並認爲它的工作原理:

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

也許你可以嘗試一下,確認該工程按預期?