如何用plotly在stackead面积图中的线尾绘制标签或图例?

使用此:

library(plotly)
library(ggplot2)
library(reshape2)
library(grid)


data <- read.table(header = T,text = "Date Tes_tTes_tTe_st_TEstTe_stT_st1  Tes_tTes_tTe_st_TEstTe_stT_st2  Tes_tTes_tTe_st_TEstTe_stT_st3  Tes_tTes_tTe_st_TEstTe_stT_st4  Tes_tTes_tTe_st_TEstTe_stT_st5  Tes_tTes_tTe_st_TEstTe_stT_st6  Tes_tTes_tTe_st_TEstTe_stT_st7  Tes_tTes_tTe_st_TEstTe_stT_st8  Tes_tTes_tTe_st_TEstTe_stT_st9
29.10.2018  12  6   12  5   4   1   10  5   3
31.10.2018  3   4   6   11  12  8   7   7   3
30.11.2018  4   4   12  7   2   6   1   6   11
29.12.2018  8   9   6   7   8   11  9   5   10
31.01.2019  0   5   10  12  3   7   7   7   4
28.02.2019  9   10  12  2   5   11  9   10  0
29.03.2019  7   5   4   6   8   0   4   6   0
26.04.2019  9   7   5   1   7   3   10  0   8
31.05.2019  4   6   12  0   11  10  7   8   10
27.06.2019  4   9   11  9   2   11  12  4   2
31.07.2019  8   2   5   9   0   8   7   8   0
30.08.2019  2   5   3   10  11  7   11  10  7
30.09.2019  12  9   6   4   2   8   8   8   10
29.10.2019  1   2   7   7   5   10  1   6   10
")

data<-as.data.frame(data)

date2<-data[1:14,1]
data[,1]<-seq(1,14,by=1) 

data <- melt(data,id.vars = 1)

colnames(data) <- c("time","group","value")

data$group <- as.character(data$group)
date2<- as.character(date2)

p<-ggplot(data,aes(x=time,y=value,fill=group)) + 
  geom_area()+
  geom_text(aes(x = time,y = value,label=ifelse(time == max(time),group,NA),color = group),position = position_stack(vjust = 0.5),check_overlap = TRUE,vjust =  "outward",hjust = "left")+
  guides(color = FALSE) + 
  scale_x_continuous(breaks=seq(0,by=1),labels=as.character(seq(0,by=1)))+
  geom_text(aes(label=ifelse(time != min(time) & time != max(time),value,NA)),check_overlap = TRUE)+
  geom_text(aes(x = time + 0.25,label=ifelse(time == min(time),check_overlap = TRUE)+
  geom_text(aes(x = time - 0.25,check_overlap = TRUE)+ 
  theme(legend.position = "none")+
  annotate("text",x = 1:14,y = -5,label = date2,size = 3)+
  theme(plot.margin = unit(c(1,11,1,1),"lines")) 

ggplotly(p)

gt <- ggplotGrob(p)
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

我们得到:

如何用plotly在stackead面积图中的线尾绘制标签或图例?

魔术来自这行:

 gt <- ggplotGrob(p)
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

如果我想获得该剧情的剧情变体,我就不能使用“ grob”对象

我只能使用ggplotly(p)并获取

如何用plotly在stackead面积图中的线尾绘制标签或图例?

此处link 1的所有变体均以失败告终。有什么解决办法吗?

P.S。我需要plotly变体以减少降价仪表板中的空间。 Here他们建议使用plotly来自动调整图的大小。

q178455231 回答:如何用plotly在stackead面积图中的线尾绘制标签或图例?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3144314.html

大家都在问