How to plot 3D scatter diagram using ggplot?

巧了我就是萌 提交于 2019-12-02 23:14:42

Since you tagged your question with plotly and said that you've tried to use it with plotly, I think it would be helpful to give you a working code solution in plotly:

Creating some data to plot with:

set.seed(417)
library(plotly)
temp <- rnorm(100, mean=30, sd=5)
pressure <- rnorm(100)
dtime <- 1:100

Graphing your 3d scatterplot using plotly's scatter3d type:

plot_ly(x=temp, y=pressure, z=dtime, type="scatter3d", mode="markers", color=temp)

Renders the following:

ggplot as others have note, by itself does not support 3d graphics rendering.

A possible solutions is gg3D.

gg3D is a package created to extend ggplot2 to produce 3D plots. It does exactly what you are asking for: it adds a third axis to a ggplot. I find it quite good and easy to use and that is what I use for my limited needs.

An example taken from the vignette to produce a basic plot

devtools::install_github("AckerDWM/gg3D")

library("gg3D")

## An empty plot with 3 axes
qplot(x=0, y=0, z=0, geom="blank") + 
  theme_void() +
  axes_3D()

## Axes can be populated with points using the function stat_3D.

data(iris)
ggplot(iris, aes(x=Petal.Width, y=Sepal.Width, z=Petal.Length, color=Species)) + 
  theme_void() +
  axes_3D() +
  stat_3D()

There are other options not involving ggplot. For example the excellent plot3D package with its extension plot3Drgl to plot in openGL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!