带R的字母图中的注解(带有data.frame)的倍数 [英] Multiples annotations (with a data.frame) in dygraphs with R
问题描述
R中有一个用于刻度的程序包,可以添加注释: https ://rstudio.github.io/dygraphs/gallery-annotations.html
There is a package in R for dygraphs, and it is possible to add annotations: https://rstudio.github.io/dygraphs/gallery-annotations.html
dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>%
dyAxis("y", valueRange = c(0, 100)) %>%
dyAnnotation("1950-7-1", text = "A", tooltip = "Korea") %>%
dyAnnotation("1965-1-1", text = "B", tooltip = "Vietnam")
我想知道如何创建一个data.frame以便组织所有注释并使用一个dyAnnotation选项添加.我试过了:
I would like to how it is possible to create a data.frame in order to organise all annotations and add with a single dyAnnotation option. I tried:
dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>%
dyAxis("y", valueRange = c(0, 100)) %>%
dyAnnotation(c("1950-7-1","1965-1-1"), text = c("A","B"), tooltip = c("Korea","Vietnam"))
它不起作用.
推荐答案
您可以执行以下操作:
第1步.创建一个没有注释的基本dygraph
并将其保存为对象(不是必需的,但在第2步中创建较短的字符串):
Step 1. Create a basic dygraph
without annotations and save it as object (not really required but creates shorter strings in Step 2):
dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>%
dyAxis("y", valueRange = c(0, 100)) -> graph
第2步.用dates
和texts
作为注释创建两个(如果需要tooltips
还要创建三个)向量.然后在string
下创建:
Step 2. Create two (if you want tooltips
also create three) vectors with dates
and texts
for your annotations. Then create below string
:
dates<-c("1950-7-1","1965-1-1","1972-1-1")
texts<-c("a","bb","cc")
my_code<-paste("graph %>%",
paste0("dyAnnotation('",dates,"',text='",texts,"')",collapse = " %>% "))
您将获得以下内容:
"graph %>% dyAnnotation('1950-7-1',text='a') %>% dyAnnotation('1965-1-1',text='bb') %>% dyAnnotation('1972-1-1',text='cc')"
第3步.使用eval
和parse
函数:
eval(parse(text = my_code))
如果要创建包含此图形的对象,请在步骤2中进行string
If you want to create an object containing this graph chcnage the string
in Step 2:
my_code<-paste("graph2<- graph %>%",
paste0("dyAnnotation('",dates,"',text='",texts,"')",collapse = " %>% "))
这篇关于带R的字母图中的注解(带有data.frame)的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!