1
votes

Greeting to all.

I am striving with a scatterplot3d plot -- a graphical representation of a data.frame of three variables where one of them is a response variable, where I have a wrong representation of the axis steps. Here is the code ("temp" is a data.frame):

library(scatterplot3d)
temp[,1] <- as.numeric(levels(temp[,1]))[temp[,1]]

for (m in temp[,2])  m <-  as.factor(as.numeric(m))

for (m in temp[,3])  m <-  as.factor(as.numeric(m))

colnames(temp) = c("Values", "Factors", "AntiFactors") # "Values" is that responce variable
xtickmarks<-c("AntiFactor1","AntiFactor1", "AntiFactor3")
ytickmarks<-c("Factor1","Factor2")

plot3d <- scatterplot3d(temp[,3], temp[,2], temp[,1], color = "blue",
                        pch = 19, type = "h", box = T, xaxt = "n", 
                        x.ticklabs=xtickmarks, y.ticklabs=ytickmarks,
                        zlab = "Time, min.")

dput(temp)

structure(list(Values = c(395, 310, 235, 290, 240, 490, 270, 
225, 430, 385, 170, 55, 295, 320, 270, 130, 300, 285, 130, 200, 
225, 90, 205, 340, 3, 8, 1, 0, 0, 0, 3, 2, 5, 2, 3, 5, 2, 3, 
200, 5, 5, 10, 5, 5, 5, 10, 10, 130, 5, 200, 80, 10, 360, 10, 
5, 8, 30, 8, 10, 10, 10, 5, 240, 120, 3, 10, 25, 5, 5, 10, 190, 
30, 115, 1, 1, 1, 2, 3, 5, 2, 5, 3, 3, 3, 2, 3, 2, 3, 0, 0, 195, 
150, 2, 2, 0, 2, 1, 1, 2, 1, 2, 1, 1, 1, 3, 2, 2, 1, 2, 2, 1, 
1, 2, 3, 2, 2, 1, 3, 1, 1), Factors = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("Factor1", "Factor2"), class = "factor"), 
    AntiFactors = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("AntiFactor1", "AntiFactor2", 
    "AntiFactor3"), class = "factor")), .Names = c("Values", 
"Factors", "AntiFactors"), row.names = c(NA, -120L), class = "data.frame")

Here is the picture of that plot I got: enter image description here

The trouble is what I got twice more ticks at the x and y axis than it is needed. It is intended to have just one set of those Factor1..2 and AntiFactor1..3 ticks at each of those x, y axis. If I run that scatterplot3d without using x.ticklabs option, it gives "0, 0.5, 1, 1.5, 2.0, ...3.0" ticks etc at the axis. What is the way to set my step in x, y axis to be just a strong integer "1", so that all my discrete ticks to be displayed in their right place?

1
+1 for dputing a small example data set, showing the code you have tried and clearly describing what 'went wrong'! Just remember to add the name of any non-standard package next time ;)Henrik
Did my answer solve your problem?Henrik
Thank you for your answer. I needed just a 3D plot, so I had to find a solution. Happily, I was able to find that. I added just two graphical options to scatterplot3d command: lab = c(2,1,2), mar = c(3,3,2,2) + 1user3202750

1 Answers

-1
votes

It seems that scatterplot3d coerces your discrete explanatory variables 'Factor' and 'AntiFactor' from factor to numeric. See e.g.:

levels(df$Factors)
# [1] "Factor1" "Factor2"
unique(as.numeric(df$Factors))
# [1] 1 2

levels(df$AntiFactors)
# [1] "AntiFactor1" "AntiFactor2" "AntiFactor3"
unique(as.numeric(df$AntiFactors))
# [1] 1 2 3

The labels you have created are recycled to get a label at each (default) tick mark. Also note your typo in 'xtickmarks' - I assume the second 'AntiFactor1' should be 'AntiFactor2'.

You may consider alternative ways to visualize your data, e.g. something like this:

library(ggplot2)
ggplot(data = temp, aes(x = AntiFactors, y = Values, fill = Factors)) +
  geom_boxplot()

enter image description here