开始使用免费开始使用

润色您的图形

下面的代码块绘制了一个交互式图表:横轴为社会支持指数,纵轴为各国幸福度,点的形状表示该国家的收入分类。

happy %>%
  plot_ly(x = ~social.support, y = ~happiness,
  hoverinfo = "text",
  text = ~paste("Country: ", country)) %>%
  add_markers(symbol = ~income, symbols = c("circle-open", "square-open", "star-open", "x-thin-open"))

您的任务是编辑悬停信息和坐标轴标签,让读者更容易理解图中的信息。

已为您加载 plotlyhappy 数据集。

本练习是课程的一部分

R 中级交互式数据可视化(plotly)

查看课程

练习说明

  • income 分组、happiness 分数,以及 social.support 指数添加悬停信息。
  • 使用 round(<variable>, 2) 将悬停信息中的所有数值变量四舍五入到小数点后 2 位。
  • 将 x 轴标签改为 "Social support index",将 y 轴标签改为 "National happiness score"

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Complete the following code to polish the plot
happy %>%
  plot_ly(x = ~social.support, y = ~happiness,
  hoverinfo = "text",
  text = ~paste("Country: ", country,
  "
Income: ", ___, "
Happiness: ", ___, "
Social support: ", ___)) %>% add_markers(symbol = ~income, symbols = c("circle-open", "square-open", "star-open", "x-thin-open")) %>% layout(xaxis = ___, yaxis = ___)
编辑并运行代码