R 使用ggplot排列三元绘图时出现意外输出
我正在尝试(通过R 使用ggplot排列三元绘图时出现意外输出,r,plot,ggplot2,gridextra,R,Plot,Ggplot2,Gridextra,我正在尝试(通过网格。在gridExtra中安排)一个来自ggtern-包的三元绘图和一个常规ggplot2并排绘图。但是,三元图的美学和标签位置被删除 我非常感谢任何能绕过这个问题并产生我想要的结果的人 一个可重复的例子: library(ggplot2) library(ggtern) library(reshape2) library(gridExtra) # Some faux data dat <- replicate(3, runif(5)) dat <- as.dat
网格。在gridExtra
中安排)一个来自ggtern
-包的三元绘图和一个常规ggplot2
并排绘图。但是,三元图的美学和标签位置被删除
我非常感谢任何能绕过这个问题并产生我想要的结果的人
一个可重复的例子:
library(ggplot2)
library(ggtern)
library(reshape2)
library(gridExtra)
# Some faux data
dat <- replicate(3, runif(5))
dat <- as.data.frame(dat/rowSums(dat))
colnames(dat) <- LETTERS[seq_len(ncol(dat)) + 23]
dat$var <- factor(LETTERS[seq_len(nrow(dat))])
# Make a ternary plot
tern.plot <-
ggplot(data = dat, aes(y=Y, x=X, z=Z, color = var, fill = var)) +
coord_tern() +
geom_point(size = 3)
# Make a stacked barplot
dat.melt <- melt(dat, id.vars = "var", variable.name = "dim")
stacked.plot <-
ggplot(data = dat.melt, aes(x = var, y = value, fill = dim)) +
geom_bar(stat = "identity")
# Arrange the two plots:
grid.arrange(tern.plot, stacked.plot, ncol = 2)
#Warning messages:
#1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
#2: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
# ...
#9: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
我用ggtern.multi
得到了想要的结果,但显然我完全没有得到
ggtern.multi(tern.plot, stacked.plot, cols = 2)
正如David Arenburg在评论中所建议的那样,multiplot
功能也能完美工作
library(devtools)
source_gist("https://gist.github.com/AEBilgrau/2a78a9ebda2226b6b988")
multiplot(tern.plot, stacked.plot, cols = 2)
试试看,我已经在你的绘图上测试过了,它工作得很好。问题可以说是在ggtern上,它定义了一个新的ggplot\u build
,但不是ggplotGrob
,因此函数是从ggplot2中提取的,它有自己的ggplot\u build
multiplot没有同样的问题,因为它限制了对ggplots的输入,而不是一般的grob,并使用ggtern中重新定义的打印方法绘制它们。我认为在ggtern中使用不同的函数名会更干净,而不是用新的、有时不兼容的功能覆盖它们。@Davidernburg,谢谢!你能把它写下来作为答案吗?我将为子孙后代选择它。看来,ggtern.multi
也起到了作用。@AEBilgrau,我不认为复制粘贴别人的答案被认为是一个答案:)这就是为什么我把它放在第一位的原因。。。因此,我认为你应该选择自己的答案,因为它可能对其他没有听说过ggtern.multi的人有用
library(devtools)
source_gist("https://gist.github.com/AEBilgrau/2a78a9ebda2226b6b988")
multiplot(tern.plot, stacked.plot, cols = 2)