R:ggplotly 中的长刻度标记标签未正确显示

R: Long tick mark labels in ggplotly are not properly shown

这些刻度线标签太长,它们总是显示在一行中。我怎么能让他们每个人都有一个换行符?这样,图像可以显示得更大,并且可以看到 x 轴刻度标记标签。

library(ggplot2)

set.seed(69)

df <- data.frame(City= rep(c("Boston", "Caracas", "Madrid", "Tokio"), 4),
                 Val = sample(c("Yes", "No"), 16, TRUE),
                 Sport = rep(c("Ok let's see if today is the day, I am not sure we will be able to make it, Ok let's see if today is the day, I am not sure we will be able to make it","having a long tick is not helpful but sometimes it is unavoidable. So if that happens, what do we do?. having a long tick is not helpful but sometimes it is unavoidable. So if that happens, what do we do?", "this is a test to see what happens today in Stack Overflow", "here we go, let's make it happen today. I know this is long but I have to do it in order to better show my problem"), each = 4))

test <- ggplot(df, aes(City, Sport, fill = Val)) + 
  geom_tile(color = "#00000022") +
  scale_fill_manual(values = c("red", "forestgreen")) +
  coord_equal() +
  theme_bw() +
  labs(fill = "Sport popular?") +theme(axis.title.y = element_blank())


ggplotly (test)

您可以在每个逗号和点之后插入一个换行符,然后明确设置绘图的大小:

library(ggplot2)
library(dplyr)
library(purrr)

set.seed(69)

df <- data.frame(City= rep(c("Boston", "Caracas", "Madrid", "Tokio"), 4),
                 Val = sample(c("Yes", "No"), 16, TRUE),
                 Sport = rep(c("Ok let's see if today is the day, I am not sure we will be able to make it, Ok let's see if today is the day, I am not sure we will be able to make it","having a long tick is not helpful but sometimes it is unavoidable. So if that happens, what do we do?. having a long tick is not helpful but sometimes it is unavoidable. So if that happens, what do we do?", "this is a test to see what happens today in Stack Overflow", "here we go, let's make it happen today. I know this is long but I have to do it in order to better show my problem"), each = 4))

df <- df %>% 
  mutate(Sport = map_chr(Sport, ~paste0(strsplit(.x, "\.|,")[[1]], collapse = "\n")))

test <- ggplot(df, aes(City, Sport, fill = Val)) + 
  geom_tile(color = "#00000022") +
  scale_fill_manual(values = c("red", "forestgreen")) +
  coord_equal() +
  theme_bw() +
  labs(fill = "Sport popular?") +theme(axis.title.y = element_blank())
test
ggsave("soplot.png", width = 20, height = 14, units = "cm")

reprex package (v0.3.0)

于 2020-10-25 创建

当您使用 ggplotly 时,查看器窗格中的默认图可能太小,但如果您放大 it/zoom,您会正确地看到图。