"二元运算符的非数字参数" facely facet_grid错误

时间:2017-05-08 16:20:16

标签: r plotly

我想创建一个包含4个散点图的构面图,但下面的代码返回错误"Error in p + facet_grid(party ~ state) : non-numeric argument to binary operator"。有什么想法吗?

# plotly minimum reproducible example
library(plotly)

# create dummy data set
state <- c("MA", "MA", "CA", "CA")
party <- c("Republican", "Democrat", "Republican", "Democrat")
age <- c(45,32,28,76)
FICO <- c(675, 789, 425, 690)
df <- data.frame(state, party, age , FICO)

# Create 4 scatter plots
p <- plot_ly(data = df, x = ~age, y = ~FICO)
p <- p + facet_grid(party ~ state)
print(p)

谢谢!

1 个答案:

答案 0 :(得分:1)

错误是从尝试+ facet_grid到情节对象。据我所知,这不是一个在绘图对象上执行的有效操作。如果仅使用ggplot2创建构面网格图,则可以在图上调用ggplotly以应用plotly的交互性。

# plotly minimum reproducible example
library(plotly)

# create dummy data set
state <- c("MA", "MA", "CA", "CA")
party <- c("Republican", "Democrat", "Republican", "Democrat")
age <- c(45,32,28,76)
FICO <- c(675, 789, 425, 690)
df <- data.frame(state, party, age , FICO)

# Create 4 scatter plots
p <- ggplot(df, aes(x=age, y=FICO)) +
  geom_point() +
  facet_grid(party ~ state)

ggplotly(p)