0

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. 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. 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. 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; 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:

photo

The result after command dput is below so you can use data:

  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. I think it would be nice to have a plane for 1 and 2 in y and z axis and classes in x axis. Class is a categorical variable. How can I format it and by what tools I can have what I need? P.S: Actually, I have 5 groups and for each group, 2 charts. I want to combine them into 5 different 3D chart.

Community
  • 1
  • 1
  • this is my data: a1 a2 b1 b2 c1 c2 d1 d2 e1 e2 489.4 197.8 546.8 287.2 599.6 318.8 616 242.4 582.4 214 505.8 301 552.6 305.8 611.4 423.2 606.8 292.8 580 255.4 525.8 389.8 558.4 305.2 623.6 510.8 600.2 329.2 579 281.8 550.2 502 566.4 334.4 658 662.4 595.6 378 579 303.8 576.6 571.2 575 348.6 657.4 656 595 397.2 579 353.8 – questionaskerprogrammer Aug 22 '18 at 17:27
  • Make it easy for anyone trying to help you: provide your data in a format that can be easily reproduced. Run `dput(mydata)` and paste the result in the question body. – Carlos Eduardo Lagosta Aug 22 '18 at 17:59
  • Also: what is speed and power in your data? a1 is the speed for car a and a2 the power, is that it? Please, make your question more clear. – Carlos Eduardo Lagosta Aug 22 '18 at 18:10
  • @CarlosEduardoLagosta I did it, thanks for the suggestion. – questionaskerprogrammer Aug 22 '18 at 18:15

1 Answers1

1
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. 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. 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.

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

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

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.

wxxyyyzz
  • 241
  • 1
  • 7
  • @wxxyyzz thanks for your response. Actually, the observations are not 5 random observations. Each one is related to one category. Each one is related to one category of a class. I edited the post. – questionaskerprogrammer Aug 22 '18 at 18:39
  • @questionaskerprogrammer I updated my answer. Basically, I use the shape of the points as the additional dimension. Hope this helps. To me, it's easier to look at and show your colleague this way. You can always try the 3D plot if you want more. – wxxyyyzz Aug 22 '18 at 19:03
  • @wxxyyzz thanks for the update. Is there anyway to plot this in a 3D plot. I want one of the plots to be classes? – questionaskerprogrammer Aug 22 '18 at 19:06
  • @questionaskerprogrammer Check `plotly` then. https://plot.ly/r/3d-scatter-plots/ The issue is that is your Class numeric or categorical? If it's categorical, may be you can plot on different stratum. For example, plot for type a and type b separately, then compare the slopes of class 1-5 between a and b. – wxxyyyzz Aug 22 '18 at 19:08
  • @wxxyyzz can you answer my other question: https://stackoverflow.com/questions/52030865/change-direction-of-axis-title-and-label-in-3dplots-in-r – questionaskerprogrammer Aug 26 '18 at 23:03