本文介绍了如何在同一图表上合并一条线和散点图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用R plotly包创建时,由data.frame创建的两个单独的图表可以正常工作.
然而,我不确定如何将它们组合为一个(大概是通过add_trace函数)
The two separate charts created from data.frame work correctly when created using the R plotly package.
However,I am not sure how to combine them into one (presumably with the add_trace function)
df <- data.frame(season=c("2000","2000","2001","2001"), game=c(1,2,1,2),value=c(1:4))
plot_ly(df, x = game, y = value, mode = "markers", color = season)
plot_ly(subset(df,season=="2001"), x = game, y = value, mode = "line")
预先感谢
推荐答案
您可以通过两种主要方法来进行此操作,如所建议的那样,制作一个ggplot
并转换为plotly
对象,如您所怀疑的那样通过在现有的plotly
对象上使用add_trace
(请参见下文).
There are two main ways you can do this with plotly, make a ggplot
and convert to a plotly
object as @MLavoie suggests OR as you suspected by using add_trace
on an existing plotly
object (see below).
library(plotly)
#data
df <- data.frame(season=c("2000","2000","2001","2001"), game=c(1,2,1,2),value=c(1:4))
#Initial scatter plot
p <- plot_ly(df, x = game, y = value, mode = "markers", color = season)
#subset of data
df1 <- subset(df,season=="2001")
#add line
p %>% add_trace(x = df1$game, y = df1$value, mode = "line")
这篇关于如何在同一图表上合并一条线和散点图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!