这是我昨天在回答其中一个问题时遇到的问题。这对我来说是一个非常令人困惑的问题。我花了一天时间,现在我觉得我的想法很清楚。由于问题的性质,我的帖子会很长。我的道歉。
foo <- data.frame(split = rep(c("0", "1"), each = 5),
a = rep(1:5,2),
b = c(7,8,9,10,11,6,8,9,10,12),
x = c(1:5, 1:5),
y = c(1:3,5,6,1.1,2.1,4.1,5.1,7.1),
stringsAsFactors=F)
这是我处理的问题的样本数据。在这个问题中,我不得不画线和点。然后,我经历了一些我不理解的事情。让我解释一下。
故事
在此数据框架中,OP处理拆分为因子并绘制了类似这样的数字。我检查了以下内容。在$ split中,零unclass()
中有1,unclass()
中有1。
foo$split <- as.factor(foo$split)
#> str(foo)
#'data.frame': 10 obs. of 5 variables:
# $ split: Factor w/ 2 levels "0","1": 1 1 1 1 1 2 2 2 2 2
# $ a : int 1 2 3 4 5 1 2 3 4 5
# $ b : num 7 8 9 10 11 6 8 9 10 12
# $ x : int 1 2 3 4 5 1 2 3 4 5
# $ y : num 1 2 3 5 6 1.1 2.1 4.1 5.1 7.1
然后,我画了数字。 OP想要的是看到1(在拆分)组中的点和线出现在0(在拆分)组中的点和线之上。但是,以下尝试失败了。红点和线应位于绿点和线的顶部。
p <- ggplot(data = foo, aes(x = x, y = y, colour = split))+
geom_point(size = 6) +
ggtitle("p")
p2 <- ggplot(data = foo, aes(x = a, y = b, colour = split))+
geom_line() +
ggtitle("p2")
png(filename = "pic1.png", width = 900, height = 600)
grid.arrange(p, p2, ncol = 1)
dev.off()
我还查看ggplot_build()$data[[1]]
以查看ggplot使用的数据。 0组在group
中的值为1,我认为ggplot使用这些值作为绘制点和线的顺序,给出p和p1中的结果。
ana <- ggplot_build(p)$data[[1]]
# colour x y PANEL group
#1 #F8766D 1 1.0 1 1
#2 #F8766D 2 2.0 1 1
#3 #F8766D 3 3.0 1 1
#4 #F8766D 4 5.0 1 1
#5 #F8766D 5 6.0 1 1
#6 #00BFC4 1 1.1 1 2
#7 #00BFC4 2 2.1 1 2
#8 #00BFC4 3 4.1 1 2
#9 #00BFC4 4 5.1 1 2
#10 #00BFC4 5 7.1 1 2
bob <- ggplot_build(p2)$data[[1]]
# colour x y PANEL group
#1 #F8766D 1 7 1 1
#2 #F8766D 2 8 1 1
#3 #F8766D 3 9 1 1
#4 #F8766D 4 10 1 1
#5 #F8766D 5 11 1 1
#6 #00BFC4 1 6 1 2
#7 #00BFC4 2 8 1 2
#8 #00BFC4 3 9 1 2
#9 #00BFC4 4 10 1 2
#10 #00BFC4 5 12 1 2
因此,OP的选择是重新排序分裂中的因子水平。我认为这是正确的方向。请注意,0和1是重新排序的。但unclass()
中的值仍然是原始值。
foo$split <- ordered(foo$split, rev(levels(foo$split)))
#> str(foo)
#'data.frame': 10 obs. of 5 variables:
# $ split: Ord.factor w/ 2 levels "1"<"0": 2 2 2 2 2 1 1 1 1 1
# $ a : int 1 2 3 4 5 1 2 3 4 5
# $ b : num 7 8 9 10 11 6 8 9 10 12
# $ x : int 1 2 3 4 5 1 2 3 4 5
# $ y : num 1 2 3 5 6 1.1 2.1 4.1 5.1 7.1
p3 <- ggplot(data = foo, aes(x = x, y = y, colour = split))+
geom_point(size = 6) +
ggtitle("p3")
p4 <- ggplot(data = foo, aes(x = a, y = b, colour = split))+
geom_line() +
ggtitle("p4")
png(filename = "pic2.png", width = 900, height = 600)
grid.arrange(p3, p4, ncol = 1)
dev.off()
请注意重新排序因子(拆分)后颜色会反转。如您所见,这种尝试对于折线图(即p4)是成功的,因为您看到绿色的颜色位于红色线的顶部。但是,p3的情况并非如此。这就是我的难题。我也调查了ggplot_build()$data[[1]]
。不同之处在于重新排序因子分割水平后颜色和组值被反转。由于0组(分割中的0)的值为2,我认为ggplot
会在第2位绘制0组的线和点。但这种预测并不一定正确。
cathy <- ggplot_build(p3)$data[[1]]
# colour x y PANEL group
#1 #00BFC4 1 1.0 1 2
#2 #00BFC4 2 2.0 1 2
#3 #00BFC4 3 3.0 1 2
#4 #00BFC4 4 5.0 1 2
#5 #00BFC4 5 6.0 1 2
#6 #F8766D 1 1.1 1 1
#7 #F8766D 2 2.1 1 1
#8 #F8766D 3 4.1 1 1
#9 #F8766D 4 5.1 1 1
#10 #F8766D 5 7.1 1 1
dan <- ggplot_build(p4)$data[[1]]
# colour x y PANEL group
#1 #00BFC4 1 7 1 2
#2 #00BFC4 2 8 1 2
#3 #00BFC4 3 9 1 2
#4 #00BFC4 4 10 1 2
#5 #00BFC4 5 11 1 2
#6 #F8766D 1 6 1 1
#7 #F8766D 2 8 1 1
#8 #F8766D 3 9 1 1
#9 #F8766D 4 10 1 1
#10 #F8766D 5 12 1 1
此时,我的猜测是ggplot使用ggolot_build()$data[[1]]
中的组值作为折线图,而它使用unclass()
中的值绘制点图。为了验证这个假设,我做了以下几点。我特意给了2(在unclass()
)到0(在分裂中)。我再次调用了原始数据框foo,并执行了以下操作。
foo <- arrange(foo, desc(split))
foo$split <- as.factor(foo$split)
#> str(foo)
#'data.frame': 10 obs. of 5 variables:
# $ split: Factor w/ 2 levels "0","1": 2 2 2 2 2 1 1 1 1 1
# $ a : int 1 2 3 4 5 1 2 3 4 5
# $ b : num 6 8 9 10 12 7 8 9 10 11
# $ x : int 1 2 3 4 5 1 2 3 4 5
# $ y : num 1.1 2.1 4.1 5.1 7.1 1 2 3 5 6
q <- ggplot(data = foo, aes(x = x, y = y, colour = split))+
geom_point(size = 6)
q2 <- ggplot(data = foo, aes(x = a, y = b, colour = split))+
geom_line()
> emo <- ggplot_build(q)$data[[1]]
> emo
colour x y PANEL group
1 #00BFC4 1 1.1 1 2
2 #00BFC4 2 2.1 1 2
3 #00BFC4 3 4.1 1 2
4 #00BFC4 4 5.1 1 2
5 #00BFC4 5 7.1 1 2
6 #F8766D 1 1.0 1 1
7 #F8766D 2 2.0 1 1
8 #F8766D 3 3.0 1 1
9 #F8766D 4 5.0 1 1
10 #F8766D 5 6.0 1 1
> fred <- ggplot_build(q2)$data[[1]]
> fred
colour x y PANEL group
1 #00BFC4 1 6 1 2
2 #00BFC4 2 8 1 2
3 #00BFC4 3 9 1 2
4 #00BFC4 4 10 1 2
5 #00BFC4 5 12 1 2
6 #F8766D 1 7 1 1
7 #F8766D 2 8 1 1
8 #F8766D 3 9 1 1
9 #F8766D 4 10 1 1
10 #F8766D 5 11 1 1
此对象(emo
)与上面的ana
非常相似。例如,分配给0和1组的值与ana
中的值相同。但是,如果你比较两者,那么0和1组的行是相反的。重要的一点是0组在ggolot_build()$data[[1]]
中有1个组。但是该组的积分(红点)位于1组的痘痘之上。这不是组中的值表示的。然后,在我看来,当绘制数字时,ggplot依赖于unclass()
中的值。此外,q2显示结果,这表明ggplot使用了组中的值。
我很抱歉很久。我想我需要描述这个问题,以便我的观点尽可能清晰。问题是ggplot
如何绘制线条和点?只要我从上面的观察中看到,它对点使用unclass
值,在ggolot_build()$data[[1]]
中对行使用值组。有谁知道这个问题?或者任何人都可以说出这个观察中出了什么问题?非常感谢你花时间到现在。