更新时间:2023-11-22 09:30:04
我尝试对ggplotly
输出进行一些手动更正:
I tried some manual corrections to the ggplotly
output:
library(plotly)
library(tidyverse)
mpg2 <- mpg %>%
filter(manufacturer %>% as.factor %>% as.numeric<6) %>%
group_by(manufacturer,model) %>% summarize(hwy=mean(hwy)) %>%
mutate(model=reorder(model,hwy))
myggplot <- mpg2 %>%
ggplot(aes(x=model,y=hwy)) + geom_col() +
coord_flip() + facet_grid(manufacturer~.,scales="free",space="free") +
theme(legend.position="none", axis.text.y=element_text(),
axis.title.y=element_blank(), axis.text.x=element_text(size=12),
strip.text.y = element_text(angle = 0))
# Set dimensions and margins
g <- ggplotly(myggplot, height=800, width=1200) %>%
layout(autosize=F, margin=list(l=170,r=70,b=50,t=50,pad=10))
# Modify tick labels of Honda bar
g$x$layout$yaxis5$tickvals <- 1:2
g$x$layout$yaxis5$ticktext <- c("civic","")
# Modify widths of Honda and Audi bars
g$x$data[[5]]$width <- g$x$data[[5]]$width/4
g$x$data[[1]]$width <- 3*g$x$data[[1]]$width/4
# Modify positions of facet labels
for (k in seq(2,10,2)) {
g$x$layout$shapes[[k]]$x1 <- 1.15
}
for (k in 1:6) {
g$x$layout$annotations[[k]]$xanchor <- "center"
g$x$layout$annotations[[k]]$x <- 1.0375
}
print(g)
结果比以前更好,但并不完美.
希望它能对您有所帮助.
The result is better than before, but not perfect.
Hope it can help you.