R scatterplot3d:自定义轴步和刻度

时间:2014-01-31 23:08:34

标签: r axis-labels

向所有人致意。

我正在努力使用scatterplot3d绘图 - 三个变量的data.frame的图形表示,其中一个是响应变量,其中我有一个错误的轴步骤表示。这是代码(“temp”是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")

这是我得到的情节的图片: enter image description here

麻烦的是我在x轴和y轴上的刻度比需要的多两倍。它的目的是在那些x,y轴的每一个上只有一组因子1..2和AntiFactor1..3。如果我在不使用scatterplot3d选项的情况下运行x.ticklabs,则会在轴上给出“0,0.5,1,1.5,2.0,... 3.0”刻度等。将x,y轴设置为强整数“1”的步骤是什么,这样我所有的离散刻度都显示在正确的位置?

1 个答案:

答案 0 :(得分:0)

似乎scatterplot3d强制你的离散解释变量'Factor'和'AntiFactor'从因子到数字。参见例如:

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

您创建的标签将被回收,以便在每个(默认)刻度标记处获得标签。另请注意'xtickmarks'中的拼写错误 - 我假设第二个'AntiFactor1'应该是'AntiFactor2'。

您可以考虑使用其他方式可视化您的数据,例如:像这样的东西:

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

enter image description here