2017-12-18 29 views
0

ggplotly에서 폭과 높이를 변경하는 방법은 무엇입니까?ggplotly에서 너비와 높이를 변경하는 방법은 무엇입니까? 너비와 높이를 변경하는 간단한 방법이 있습니까?

우리가 ggplot를 사용하는 경우, 그것이 내가 그래프를 인쇄하고 HTML을 저장

htmltools::browsable(gg[[i]]) 

htmltools::save_html(gg[[i]],path) 

또는

htmlwidgets::saveWidget(print(gg[[i]]), path) 

을 사용하고 ggsave

ggsave(filename = "foo300.png", ggplot(mtcars, aes(x=wt, y=mpg)) + 
      geom_point(size=2, shape=23) + theme_bw(base_size = 10), 
     width = 5, height = 4, dpi = 300, units = "in", device='png') 

를 사용할 수 있지만 그래프가 너무 작 일부 단어는 겹쳐 지지만 큰 글꼴이 필요하므로 그래프의 크기를 변경하길 바랍니다.

폭과 높이를 변경하는 데 사용할 수있는 글꼴을 알고 있습니다.

library(plotly) 
m <- list(
    l = 50, 
    r = 50, 
    b = 100, 
    t = 100, 
    pad = 4 
) 
p <- plot_ly(x = seq(0, 8), y = seq(0, 8)) %>% 
    layout(autosize = F, width = 500, height = 500, margin = m) 

이것은 내 샘플 데이터 세트입니다.

d1 <- 
    data.frame(col_one = c(1, 2, 3, 3), 
      col_two = c("aa", "bb", "aa", "bb")) 
d2 <- 
    data.frame(col_one = c(1, 1, 1, 6), 
      col_two = c("bb", "aa", "aa", "bb")) 
d3 <- 
    data.frame(col_one = c(7, 1, 1, 4), 
      col_two = c("cc", "aa", "bb", "bb")) 
my.list <- list(d1, d2, d3) 

f <- function(table) { 
    table <- mapply(function(data, count) { 
    sql <- 
     sqldf(
     paste0(
      "select *,count(col_one) as count from data where col_one = ", 
      count, 
      " group by col_one,col_two" 
     ) 
    ) 
    }, my.list, 1, 
    SIMPLIFY = FALSE) 
    print(table) 

    return (table) 
} 


f(table = my.list) 

f2 <- function(table) { 
    num <- length(table) 
    chart <- vector(num, mode = "list") 
    plotly <- vector(num, mode = "list") 

    for (i in 1:length(table)) { 
    chart[[i]] <- ggplot(data = table[[i]], 
         aes(x = col_two, 
          y = count, 
          fill = col_two)) + 
     geom_bar(stat = "identity", width = 0.3) 

    #print(chart[[2]]) 

    plotly[[i]] <- ggplotly(chart[[i]]) 

    } 
    return(plotly) 
} 
f2(f(table = my.list)) 

save <- function (gg) { 
    num <- length(gg) 
    print_gg <- vector(num, mode = "list") 
    dir <- "~/test/aa" 

    for (i in 1:length(gg)) { 
    path <- paste0(dir, i) 

    htmltools::browsable(gg[[i]]) 
    htmltools::save_html(gg[[i]],path) 

    #htmlwidgets::saveWidget(print(gg[[i]]), path) 
    } 
} 

save(f2(f(table = my.list))) 

또한이 코드를 검색했지만 폭과 높이를 변경하려고했습니다. 위젯을 먼저 상자에 넣어야합니까? 너비와 높이를 변경하는 간단한 방법이 있습니까?

htmlwidgets::createWidget(
    "sigma", 
    x, 
    width = width, 
    height = height, 
    sizingPolicy = htmlwidgets::sizingPolicy(
    viewer.padding = 0, 
    viewer.paneHeight = 500, 
    browser.fill = TRUE 
) 
) 

너비 또는 높이를 추가해야하는 그래프에서 ggplotly를 ggplotly로 변경하면 어떻게됩니까? 너비와 높이를 변경하는 간단한 방법이 있습니까? 당신이 ggplot2 그래픽을 만든 다음 plotly 객체로 돌려

귀하의 예제를 사용 :이 여기에 당신이 원하는,하지만 정확히 잘 모르겠습니다

답변

0

내 걸릴 것입니다.

gg <- ggplot(mtcars, aes(x=wt, y=mpg)) + 
      geom_point(size=2, shape=23) + theme_bw(base_size = 10) 

g <- ggplotly(gg) 

는 그래픽의 크기를 변경하려면 일반적으로 plotly과 함께 할 것으로 수행합니다.

htmltools::save_html(h,"index.html") 
:

m <- list(
    l = 50, 
    r = 50, 
    b = 100, 
    t = 100, 
    pad = 4 
) 

h <- g %>% 
layout(autosize = F, width = 500, height = 500, margin = m) 

다음은 HTML 문서로 저장할 수 있습니다