如何在ggplotly中更改宽度和高度?有没有简单的方法来改变宽度和高度?
How to change width and height in ggplotly? Are there simple methods to change the width and height?
如何在 ggplotly 中更改宽度和高度?
如果我们使用ggplot,它可以使用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')
我正在使用
htmltools::browsable(gg[[i]])
htmltools::save_html(gg[[i]],path)
或
htmlwidgets::saveWidget(print(gg[[i]]), path)
要打印图并保存HTML但是图太小了,有些字重叠了,但是我想要大字体,所以希望把图的大小改一下。
我知道可以用它来改变宽度和高度的 plolty。
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
)
)
如果我将图表中的 ggplot 更改为 ggplotly,我应该在其中添加宽度或高度?有没有简单的方法改变宽高?
我不确定这是你想要的,但这是我的看法:
使用您的示例创建一个 ggplot2
图形,然后将其转换为 plotly
对象。
gg <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) + theme_bw(base_size = 10)
g <- ggplotly(gg)
要更改图形的大小,请按照您通常对 plotly
所做的操作进行操作。
m <- list(
l = 50,
r = 50,
b = 100,
t = 100,
pad = 4
)
h <- g %>%
layout(autosize = F, width = 500, height = 500, margin = m)
然后您可以另存为 html 文档:
htmltools::save_html(h,"index.html")
如何在 ggplotly 中更改宽度和高度?
如果我们使用ggplot,它可以使用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')
我正在使用
htmltools::browsable(gg[[i]])
htmltools::save_html(gg[[i]],path)
或
htmlwidgets::saveWidget(print(gg[[i]]), path)
要打印图并保存HTML但是图太小了,有些字重叠了,但是我想要大字体,所以希望把图的大小改一下。
我知道可以用它来改变宽度和高度的 plolty。
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
)
)
如果我将图表中的 ggplot 更改为 ggplotly,我应该在其中添加宽度或高度?有没有简单的方法改变宽高?
我不确定这是你想要的,但这是我的看法:
使用您的示例创建一个 ggplot2
图形,然后将其转换为 plotly
对象。
gg <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23) + theme_bw(base_size = 10)
g <- ggplotly(gg)
要更改图形的大小,请按照您通常对 plotly
所做的操作进行操作。
m <- list(
l = 50,
r = 50,
b = 100,
t = 100,
pad = 4
)
h <- g %>%
layout(autosize = F, width = 500, height = 500, margin = m)
然后您可以另存为 html 文档:
htmltools::save_html(h,"index.html")