【发布时间】:2018-06-10 17:23:55
【问题描述】:
可能很简单。
我有一个 xy 数据集,我想使用R 的plotly 进行绘图。以下是数据:
set.seed(1)
df <- data.frame(x=1:10,y=runif(10,1,10),group=c(rep("A",9),"B"),group.size=as.integer(runif(10,1,10)))
我想用df$group 为数据着色,并让点的大小跟随df$group.size(即气泡图)。另外,我想同时添加这两个图例。
这是我天真的尝试:
require(plotly)
require(dplyr)
main.plot <-
plot_ly(type='scatter',mode="markers",color=~df$group,x=~df$x,y=~df$y,size=~df$group.size,marker=list(sizeref=0.1,sizemode="area",opacity=0.5),data=df,showlegend=T) %>%
layout(title="Title",xaxis=list(title="X",zeroline=F),yaxis=list(title="Y",zeroline=F))
不幸的是,这个图例搞砸了,至少我希望它是这样的:每个组的一个点具有相同的大小但颜色不同。
然后为我关注this的group.size添加图例,也得到了aocall的回答:
legend.plot <- plot_ly() %>% add_markers(x = 1, y = unique(df$group.size),
size = unique(df$group.size),
showlegend = T,
marker = list(sizeref=0.1,sizemode="area")) %>%
layout(title="TITLE",xaxis = list(zeroline=F,showline=F,showticklabels=F,showgrid=F),
yaxis=list(showgrid=F))
我的问题是图例包含我的数据中不存在的值。
然后我使用subplot 组合它们:
subplot(legend.plot, main.plot, widths = c(0.1, 0.9))
图例标题被删除的地方
所以我会提供一些帮助。
【问题讨论】:
-
试过并相应地更新了我的问题