简体   繁体   English

在R中为3个具有2个维度的变量绘制3D图表

[英]Plot a 3D chart in R for 3 variables with 2 dimensions

I have a dataset that I want to plot them in R. I know there are many packages such as ploty or some other 3D plots . 我有一个要在R中绘制它们的数据集。我知道有很多软件包,例如ploty其他3D图 My problem is that my dataset includes 5 different types, for example 5 different cars and I want to compare the speed and power of each of types in 5 different classes. 我的问题是我的数据集包括5种不同的类型,例如5种不同的汽车,我想比较5种不同类别中每种类型的速度和功率。 So in total there are 10 columns (excluding the first column of classes) but I would like to have 5 charts, each one with 2 dimensions. 因此,总共有10列(不包括类的第一列),但是我想有5个图表,每个图表都有2个维度。 It looks like below in which for each of a,b,c,d,e, the column with index 1 is speed and the column with index 2 is power and the chart plots for different classes; 如下图所示,对于a,b,c,d,e中的每个,索引为1的列为速度,索引为2的列为幂,并且图表针对不同的类别进行绘制; therefore, a1 is speed of a and a2 is power of 2. There are five classes and two variables for each of 5 types of cars: 因此,a1是a的速度,a2是2的乘方。对于5种类型的汽车,有五类和两个变量:

照片

The result after command dput is below so you can use data: 命令dput之后的结果如下,因此您可以使用数据:

  structure(list(X = structure(1:5, .Label = c("class1", "class2", 
  "class3", "class4", "class5"), class = "factor"), a1 = c(489.4, 
  505.8, 525.8, 550.2, 576.6), a2 = c(197.8, 301, 389.8, 502, 571.2
  ), b1 = c(546.8, 552.6, 558.4, 566.4, 575), b2 = c(287.2, 305.8, 
  305.2, 334.4, 348.6), c1 = c(599.6, 611.4, 623.6, 658, 657.4), 
 c2 = c(318.8, 423.2, 510.8, 662.4, 656), d1 = c(616, 606.8, 
 600.2, 595.6, 595), d2 = c(242.4, 292.8, 329.2, 378, 397.2
  ), e1 = c(582.4, 580, 579, 579, 579), e2 = c(214, 255.4, 
 281.8, 303.8, 353.8)), .Names = c("X", "a1", "a2", "b1", 
  "b2", "c1", "c2", "d1", "d2", "e1", "e2"), class = "data.frame", row.names       
 = c(NA,-5L))

So I want one for example blue for a1, one blue for a2 (it can be a plane) and one grean for b1 and one green for b2, even bubble or other types might be useful. 因此,我想要一个例如a1的蓝色,一个a2的蓝色(可以是一个平面),一个b1的灰色和b2的绿色,甚至气泡或其他类型也可能有用。 I think it would be nice to have a plane for 1 and 2 in y and z axis and classes in x axis. 我认为在y和z轴上有1和2的平面,在x轴上有类的平面是很好的。 Class is a categorical variable. 类是一个分类变量。 How can I format it and by what tools I can have what I need? 我该如何格式化它以及通过哪些工具可以满足需要? PS: Actually, I have 5 groups and for each group, 2 charts. PS:实际上,我有5个小组,每个小组有2个图表。 I want to combine them into 5 different 3D chart. 我想将它们组合成5种不同的3D图表。

df <- data.frame(a1 = c(489.4,  505.8,  525.8,  550.2,  576.6),
                 a2 = c(197.8,  301,    389.8,  502,    571.2),
                 b1 = c(546.8,  552.6,  558.4,  566.4,  575),
                 b2 = c(287.2,  305.8,  305.2,  334.4,  348.6),
                 c1 = c(599.6,  611.4,  623.6,  658,    657.4),
                 c2 = c(318.8,  423.2,  510.8,  662.4,  656),
                 d1 = c(616,    606.8,  600.2,  595.6,  595),
                 d2 = c(242.4,  292.8,  329.2,  378,    397.2),
                 e1 = c(582.4,  580,    579,    579,    579),
                 e2 = c(214,    255.4,  281.8,  303.8,  353.8))

I took advantage of excel. 我利用了excel。 It would be better to provide data this way 最好以这种方式提供数据

Not sure if I understood your question correctly. 不知道我是否正确理解了您的问题。 I guess you don't need a 3D plot. 我猜您不需要3D图。 Using color as the third dimension might be good for your purpose. 将颜色用作三维尺寸可能对您的目的有益。 I assume you want to compare "speed" vs. "power" in different "cars". 我假设您想比较不同“汽车”中的“速度”与“功率”。

I re-shape your data manually to create a new data frame, so that "speed" and "power" are in the same column. 我手动调整数据的形状以创建新的数据框,以便“速度”和“功率”在同一列中。 I also added a new column "Group" to indicate what type of "car" they are. 我还添加了一个新列“ Group”以指示它们是什么类型的“汽车”。

colnames(df) <- rep(c("V1", "V2"), 5)
df.new <- rbind(df[, c(1, 2)], 
                df[, c(3, 4)],
                df[, c(5, 6)],
                df[, c(7, 8)],
                df[, c(9, 10)])
df.new$Group <- factor(rep(c("a","b","c","d","e"), each = 5))
df.new$Class <- rep(c(1:5), 5) # make it factor if you want, then add as.numeric() for plotting

It might show like this (I hid some rows): 它可能像这样显示(我隐藏了一些行):

      V1    V2 Group Class
1  489.4 197.8     a     1
2  505.8 301.0     a
3  525.8 389.8     a
4  550.2 502.0     a
5  576.6 571.2     a
6  546.8 287.2     b
7  552.6 305.8     b
11 599.6 318.8     c
16 616.0 242.4     d
21 582.4 214.0     e
25 579.0 353.8     e

To use basic R plot 使用基本R图

plot(V2 ~ V1, data = df.new, pch = as.numeric(Class),col = Group)
legend("topleft", legend = c("a","b","c","d","e"), pch = 1, col = 1:5)

To use ggplot 使用ggplot

library(ggplot2)
ggplot(data = df.new, aes(x = V1, y = V2, color = Group)) +
    geom_point(aes(shape = as.factor(Class)))

You would be able to visualize the difference of these types of "cars", as well as different classes. 您将可以看到这些类型的“汽车”以及不同类别的区别。 Feel free to exchange the shape and the color. 随时交换形状和颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM