Gum*_*meo 5 r ggplot2 plotly ggvis
我一直在研究R中的交互式图.我知道有几个包可以创建交互式图,特别是散点图,但我正在寻找某种功能.
例如这个情节.可以将鼠标悬停在按钮上以获得该点后面数据的小数字摘要,即工具提示.
当您拥有包含更多变量的数据集时,通常可以从PCA中探索/可视化分数,或进行多维缩放(MDS).但是如果人们以交互方式绘制数据,就像上面的例子那样,当一个人在这一点上盘旋时,总结并没有给出如此多的信息,即只是一长串的数字.能够生成要显示的自定义绘图会更好,而不仅仅是显示数值.
所以我的问题是:
当一个人在一个散点图中的一个点上盘旋时,是否有可能(在R中可用的某些包中)生成一个可视摘要.这可能是一个条形图,或者只是一些用户指定的绘图函数,它从data.frame中取一行作为参数.
如果可行的话,那将有助于快速了解MDS的结果.
编辑:
以下是在虹膜数据集上执行MDS的一些代码:
library(ggplot2)
library(plotly)
d <- dist(iris[,-5]) # euclidean distances between the rows
fit <- cmdscale(d,eig=TRUE, k=2) # k is the number of dim
# Put coordinates and original data in one data.frame
x <- fit$points[,1]
y <- fit$points[,2]
pDat <- data.frame(x=x,y=y)
pDat <- cbind(pDat,iris)
p <- ggplot(pDat) + geom_point(aes(x,y))
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
首先,现在工具提示仅包括x,y坐标.我想工具提示包含原始4个变量的值.然后,我想将工具提示显示为条形图,而不是数据点后面的原始4个变量.MDS保留了数据点之间的距离,因此可以用鼠标逐渐悬停,并且看到条形图几乎连续变化,因为距离得以保留.在我的使用案例中,每个点后面有30个变量,因此条形图摘要提供的视觉信息比30个数值更多.
Ada*_*uek 11
如果您正在使用RStudio,那么情节包应该足够友好以供使用.例如:
library(ggplot2)
library(plotly)
p <- ggplot(iris, aes(Sepal.Length, Petal.Length, colour=Species)) + geom_point()
ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
悬停在一个点上时显示的信息如下所示:
小智 4
如果您使用 rbokeh 的最新开发版本,您可以通过以下方式实现您所追求的目标:
devtools::install_github("bokeh/rbokeh@v0.6.3")
library(rbokeh)
iris$sw <- paste0(iris$Sepal.Width * 20, "px")
iris$pw <- paste0(iris$Petal.Width * 20, "px")
iris$sl <- paste0(iris$Sepal.Length * 20, "px")
iris$pl <- paste0(iris$Petal.Width * 20, "px")
style_str <- "white-space: nowrap; border: 1px solid white; background: steelblue; height: 15px;"
figure() %>%
ly_points(x = Sepal.Width, y = Sepal.Length, color = Species,
data = iris, hover = glue::glue("
<div>
<div style='{style_str} width: @sw'>Sepal width</div>
<div style='{style_str} width: @pw'>Petal Width</div>
<div style='{style_str} width: @sl'>Sepal width</div>
<div style='{style_str} width: @pl'>Petal Length</div>
</div>
"))
Run Code Online (Sandbox Code Playgroud)
这里发生的事情是 rbokeh 允许您指定任意 html 作为工具提示,因此这里我们根据数据值(引用@sw等)创建具有指定宽度的 div 来创建条形图。
上面的示例适用于简单的条形图,但如果您希望能够在工具提示中显示任意图像,一种方法是为每个数据点预先生成光栅图像,并将其嵌入为 html 工具提示(img以 base64 编码的图像作为 src 的标记)。