如何解决R:ggplotly中的长刻度标记标签未正确显示
这些刻度标记标签太长,它们始终显示在一行中。我如何让他们每个人都换行?这样,可以将图像放大显示,并且可以看到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
时,“查看器”窗格中的默认图可能太小,但是如果放大/缩放,则可以正确看到该图。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。